Skip to content

Commit

Permalink
Add translations
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Jul 28, 2024
1 parent 35e36c9 commit 6f4ca03
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 157 deletions.
255 changes: 172 additions & 83 deletions dist/irrigation-unlimited-card.js

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
import { customElement, property, state } from "lit/decorators.js";
import { HomeAssistant } from "./ha-types";
import { fireEvent } from "./fire_event";

import { IrrigationUnlimitedCardConfig } from "./types";
import { localise } from "./localize";

const loc = new localise(window.navigator.language);

@customElement("irrigation-unlimited-card-editor")
export class IrrigationUnlimitedCardEditor extends LitElement {
Expand Down Expand Up @@ -53,15 +55,15 @@ export class IrrigationUnlimitedCardEditor extends LitElement {
return html`
<div class="iu-editor-row">
<ha-textfield
label="Title (optional)"
label=${loc.t("editor.title.name")}
.value=${this._name}
.configValue=${"name"}
@input="${this._valueChanged}"
></ha-textfield>
</div>
<div class="iu-editor-row">
<ha-textfield
label="Show controllers (CSV list)"
label=${loc.t("editor.showControllers.name")}
.value=${this._show_controllers}
.configValue=${"show_controllers"}
@input="${this._valueChanged}"
Expand All @@ -74,7 +76,9 @@ export class IrrigationUnlimitedCardEditor extends LitElement {
.configValue=${"always_show_zones"}
@change=${this._valueChanged}
></ha-switch>
<label for=${this._always_show_zones}>Always show zones</label>
<label for=${this._always_show_zones}
>${loc.t("editor.alwaysShowZones.name")}</label
>
</div>
<div class="iu-editor-row">
<ha-switch
Expand All @@ -83,7 +87,9 @@ export class IrrigationUnlimitedCardEditor extends LitElement {
.configValue=${"always_show_sequences"}
@change=${this._valueChanged}
></ha-switch>
<label for=${this._always_show_sequences}>Always show sequences</label>
<label for=${this._always_show_sequences}
>${loc.t("editor.alwaysShowSequences.name")}</label
>
</div>
<div class="iu-editor-row">
<ha-switch
Expand All @@ -93,7 +99,7 @@ export class IrrigationUnlimitedCardEditor extends LitElement {
@change=${this._valueChanged}
></ha-switch>
<label for=${this._show_timeline_scheduled}
>Show timeline scheduled</label
>${loc.t("editor.showTimelineScheduled.name")}</label
>
</div>
<div class="iu-editor-row">
Expand All @@ -103,7 +109,9 @@ export class IrrigationUnlimitedCardEditor extends LitElement {
.configValue=${"show_timeline_history"}
@change=${this._valueChanged}
></ha-switch>
<label for=${this._show_timeline_history}>Show timeline history</label>
<label for=${this._show_timeline_history}
>${loc.t("editor.showTimelineHistory.name")}</label
>
</div>
`;
}
Expand Down
54 changes: 29 additions & 25 deletions src/irrigation-unlimited-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import type {
LovelaceCardEditor,
} from "./types";
import { CARD_VERSION } from "./const";
import { localize } from "./localize/localize";
import { date_to_str } from "./util";
import { localise } from "./localize";

const loc = new localise(window.navigator.language);

/* eslint no-console: 0 */
console.info(
`%c IRRIGATION-UNLIMITED-CARD \n%c ${localize("common.version")} ${CARD_VERSION} `,
`%c IRRIGATION-UNLIMITED-CARD \n%c ${loc.t("common.version")} ${CARD_VERSION} `,
"color: orange; font-weight: bold; background: black",
"color: white; font-weight: bold; background: dimgray"
);
Expand All @@ -47,7 +49,7 @@ export class IrrigationUnlimitedCard extends LitElement {

public setConfig(config: IrrigationUnlimitedCardConfig): void {
if (!config) {
throw new Error(localize("common.invalid_configuration"));
throw new Error(loc.t("common.invalidConfiguration"));
}
this.config = config;
}
Expand Down Expand Up @@ -191,19 +193,21 @@ export class IrrigationUnlimitedCard extends LitElement {
</div>
<div class="iu-control-panel">
<div class="iu-control-panel-item">
<label>Zones&nbsp;</label>
<label>${loc.t("controller.zones.name")}&nbsp;</label>
<ha-switch
.checked="${!zonesHidden}"
.disabled="${this.config.always_show_zones}"
title=${loc.t("controller.zones.buttonHint")}
@change="${this._toggleZones}"
>
</ha-switch>
</div>
<div class="iu-control-panel-item">
<label>Sequences&nbsp;</label>
<label>${loc.t("controller.sequences.name")}&nbsp;</label>
<ha-switch
.checked="${!sequencesHidden}"
.disabled="${this.config.always_show_sequences}"
title=${loc.t("controller.sequences.buttonHint")}
@change="${this._toggleSequences}"
>
</ha-switch>
Expand Down Expand Up @@ -628,7 +632,7 @@ export class IrrigationUnlimitedCard extends LitElement {
></ha-icon>
<div class="iu-menu-content iu-hidden">
<div class="iu-menu-item">
<div class="iu-mc1">Enable</div>
<div class="iu-mc1">${loc.t("menu.enable.name")}</div>
<div class="iu-mc2"></div>
<div class="iu-mc3">
${this._renderEnabled(isEnabled, isBlocked)}
Expand All @@ -637,16 +641,13 @@ export class IrrigationUnlimitedCard extends LitElement {
<div
class="iu-menu-item ${suspended === undefined ? "iu-hidden" : ""}"
>
<div class="iu-mc1">Suspend</div>
<div class="iu-mc1">${loc.t("menu.suspend.name")}</div>
<div class="iu-mc2">
<input
type="text"
class="iu-time-input"
placeholder="h:mm:ss"
title="Duration
===============
h:mm:ss
<blank> = reset"
title=${loc.t("menu.suspend.hint")}
size="8"
maxlength="8"
required
Expand All @@ -656,20 +657,21 @@ h:mm:ss
<div class="iu-mc3">
<ha-icon-button
icon="mdi:timer-outline"
title=${loc.t("menu.suspend.buttonHint")}
@click="${this._serviceSuspend}"
>
<ha-icon icon="mdi:timer-outline"></ha-icon>
</ha-icon-button>
</div>
</div>
<div class="iu-menu-item ${!allowManual ? "iu-hidden" : ""}">
<div class="iu-mc1">Manual</div>
<div class="iu-mc1">${loc.t("menu.manual.name")}</div>
<div class="iu-mc2">
<input
type="text"
class="iu-time-input"
placeholder="0:00:00"
title="Duration"
title=${loc.t("menu.manual.hint")}
size="8"
maxlength="8"
required
Expand All @@ -679,6 +681,7 @@ h:mm:ss
<div class="iu-mc3">
<ha-icon-button
icon="mdi:play"
title=${loc.t("menu.manual.buttonHint")}
@click="${this._serviceManualRun}"
>
<ha-icon icon="mdi:run"></ha-icon>
Expand All @@ -688,11 +691,12 @@ h:mm:ss
<div
class="iu-menu-item ${(~pauseResume & 1) > 0 ? "iu-hidden" : ""}"
>
<div class="iu-mc1">Pause</div>
<div class="iu-mc1">${loc.t("menu.pause.name")}</div>
<div class="iu-mc2"></div>
<div class="iu-mc3">
<ha-icon-button
.disabled=${(~pauseResume & 1) > 0}
title=${loc.t("menu.pause.buttonHint")}
@click="${this._servicePause}"
>
<ha-icon icon="mdi:pause"></ha-icon>
Expand All @@ -702,23 +706,25 @@ h:mm:ss
<div
class="iu-menu-item ${(~pauseResume & 2) > 0 ? "iu-hidden" : ""}"
>
<div class="iu-mc1">Resume</div>
<div class="iu-mc1">${loc.t("menu.resume.name")}</div>
<div class="iu-mc2"></div>
<div class="iu-mc3">
<ha-icon-button
.disabled=${(~pauseResume & 2) > 0}
title=${loc.t("menu.resume.buttonHint")}
@click="${this._serviceResume}"
>
<ha-icon icon="mdi:play"></ha-icon>
</ha-icon-button>
</div>
</div>
<div class="iu-menu-item ${!allowCancel ? "iu-hidden" : ""}">
<div class="iu-mc1">Cancel</div>
<div class="iu-mc1">${loc.t("menu.cancel.name")}</div>
<div class="iu-mc2"></div>
<div class="iu-mc3">
<ha-icon-button
.disabled=${!allowCancel}
title=${loc.t("menu.cancel.buttonHint")}
@click="${this._serviceCancel}"
>
<ha-icon icon="mdi:cancel"></ha-icon>
Expand All @@ -728,26 +734,23 @@ h:mm:ss
<div
class="iu-menu-item ${adjustment === undefined ? "iu-hidden" : ""}"
>
<div class="iu-mc1">Adjust</div>
<div class="iu-mc1">${loc.t("menu.adjust.name")}</div>
<div class="iu-mc2">
<input
type="text"
class="iu-adjust-input"
value=${adjustment ?? ""}
title="Adjustment options
===============
Percentage: %n
Actual: =0:00:00
Increase: +0:00:00
Decrease: -0:00:00
Reset: <blank>"
title=${loc.t("menu.adjust.hint")}
size="9"
maxlength="9"
pattern="^$|^[=+-][0-9]{1,2}:[0-9]{2}:[0-9]{2}$|^%[0-9]*.?[0-9]+$"
/>
</div>
<div class="iu-mc3">
<ha-icon-button icon="mdi:adjust" @click="${this._serviceAdjust}">
<ha-icon-button
title=${loc.t("menu.adjust.buttonHint")}
@click="${this._serviceAdjust}"
>
<ha-icon icon="mdi:adjust"></ha-icon>
</ha-icon-button>
</div>
Expand All @@ -765,6 +768,7 @@ Reset: <blank>"
<ha-switch
.checked=${isEnabled}
.disabled=${isBlocked}
title=${loc.t("menu.enable.buttonHint")}
@change="${this._serviceEnable}"
></ha-switch>
`;
Expand Down
44 changes: 44 additions & 0 deletions src/localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as en from "./translations/en.json";
import * as nb from "./translations/nb.json";

const languages = {
en: en,
nb: nb,
};

export class localise {
private lang: string;

constructor(preferred: string) {
if (!(preferred in languages)) {
preferred = preferred.substring(0, 2);
if (!(preferred in languages)) {
preferred = "en";
}
}
this.lang = preferred;
}

private find(language: string, keys: string[]) {
let d = languages[language];
for (const k of keys) {
d = d[k];
if (d === undefined) throw new Error();
}
return d;
}

public t(key: string): string {
const keys = key.split(".");
try {
return this.find(this.lang, keys);
} catch (e) {
try {
if (this.lang !== "en") return this.find("en", keys);
else return "";
} catch (e) {
return "";
}
}
}
}
8 changes: 0 additions & 8 deletions src/localize/languages/en.json

This file was deleted.

7 changes: 0 additions & 7 deletions src/localize/languages/nb.json

This file was deleted.

27 changes: 0 additions & 27 deletions src/localize/localize.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"common": {
"version": "Version",
"invalidConfiguration": "Invalid configuration"
},
"editor": {
"title": { "name": "Title (optional)" },
"showControllers": { "name": "Show controllers (CSV list)" },
"alwaysShowZones": { "name": "Always show zones" },
"alwaysShowSequences": { "name": "Always show sequences" },
"showTimelineScheduled": { "name": "Show timeline sheduled" },
"showTimelineHistory": { "name": "Show timeline history" }
},
"controller": {
"zones": {
"name": "Zones",
"buttonHint": "Show/hide zones"
},
"sequences": {
"name": "Sequences",
"buttonHint": "Show/hide sequences"
}
},
"menu": {
"enable": { "name": "Enable" },
"suspend": {
"name": "Suspend",
"hint": "Duration\n===============\nh:mm:ss\n<blank> = reset"
},
"manual": { "name": "Manual", "hint": "Duration" },
"pause": { "name": "Pause" },
"resume": { "name": "Resume" },
"cancel": { "name": "Cancel" },
"adjust": {
"name": "Adjust",
"hint": "Adjustment options\n===============\nPercentage: %n\nActual: =0:00:00\nIncrease: +0:00:00\nDecrease: -0:00:00\nReset: <blank>"
}
}
}
Loading

0 comments on commit 6f4ca03

Please sign in to comment.