From 6f4ca030ea285db5a586c16e997da8c3be0bbfdd Mon Sep 17 00:00:00 2001 From: rgc99 Date: Sun, 28 Jul 2024 07:51:57 +0000 Subject: [PATCH] Add translations --- dist/irrigation-unlimited-card.js | 255 ++++++++++++++++++++---------- src/editor.ts | 22 ++- src/irrigation-unlimited-card.ts | 54 ++++--- src/localize.ts | 44 ++++++ src/localize/languages/en.json | 8 - src/localize/languages/nb.json | 7 - src/localize/localize.ts | 27 ---- src/translations/en.json | 39 +++++ src/translations/nb.json | 6 + 9 files changed, 305 insertions(+), 157 deletions(-) create mode 100644 src/localize.ts delete mode 100644 src/localize/languages/en.json delete mode 100644 src/localize/languages/nb.json delete mode 100644 src/localize/localize.ts create mode 100644 src/translations/en.json create mode 100644 src/translations/nb.json diff --git a/dist/irrigation-unlimited-card.js b/dist/irrigation-unlimited-card.js index 4d16d50..279d67f 100644 --- a/dist/irrigation-unlimited-card.js +++ b/dist/irrigation-unlimited-card.js @@ -137,6 +137,139 @@ const fireEvent = (node, type, detail, options) => { return event; }; +var common$1 = { + version: "Version", + invalidConfiguration: "Invalid configuration" +}; +var 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" + } +}; +var controller = { + zones: { + name: "Zones", + buttonHint: "Show/hide zones" + }, + sequences: { + name: "Sequences", + buttonHint: "Show/hide sequences" + } +}; +var menu = { + enable: { + name: "Enable" + }, + suspend: { + name: "Suspend", + hint: "Duration\n===============\nh:mm:ss\n = 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: " + } +}; +var en = { + common: common$1, + editor: editor, + controller: controller, + menu: menu +}; + +var en$1 = /*#__PURE__*/Object.freeze({ + __proto__: null, + common: common$1, + controller: controller, + default: en, + editor: editor, + menu: menu +}); + +var common = { + version: "Versjon", + invalid_configuration: "Ikke gyldig konfiguration" +}; +var nb = { + common: common +}; + +var nb$1 = /*#__PURE__*/Object.freeze({ + __proto__: null, + common: common, + default: nb +}); + +const languages = { + en: en$1, + nb: nb$1, +}; +class localise { + constructor(preferred) { + if (!(preferred in languages)) { + preferred = preferred.substring(0, 2); + if (!(preferred in languages)) { + preferred = "en"; + } + } + this.lang = preferred; + } + find(language, keys) { + let d = languages[language]; + for (const k of keys) { + d = d[k]; + if (d === undefined) + throw new Error(); + } + return d; + } + t(key) { + 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 ""; + } + } + } +} + +const loc$1 = new localise(window.navigator.language); let IrrigationUnlimitedCardEditor = class IrrigationUnlimitedCardEditor extends s { setConfig(config) { this._config = config; @@ -172,7 +305,7 @@ let IrrigationUnlimitedCardEditor = class IrrigationUnlimitedCardEditor extends return x `
- +
- +
${loc$1.t("editor.showTimelineScheduled.name")}
@@ -222,7 +359,9 @@ let IrrigationUnlimitedCardEditor = class IrrigationUnlimitedCardEditor extends .configValue=${"show_timeline_history"} @change=${this._valueChanged} > - +
`; } @@ -811,61 +950,9 @@ const styles = i$2 ` const CARD_VERSION = '2024.5.0'; -var common$1 = { - version: "Version", - invalid_configuration: "Invalid configuration", - show_warning: "Show Warning", - show_error: "Show Error" -}; -var en = { - common: common$1 -}; - -var en$1 = /*#__PURE__*/Object.freeze({ - __proto__: null, - common: common$1, - default: en -}); - -var common = { - version: "Versjon", - invalid_configuration: "Ikke gyldig konfiguration", - show_warning: "Vis advarsel" -}; -var nb = { - common: common -}; - -var nb$1 = /*#__PURE__*/Object.freeze({ - __proto__: null, - common: common, - default: nb -}); - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -const languages = { - en: en$1, - nb: nb$1, -}; -function localize(string, search = '', replace = '') { - const lang = (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_'); - let translated; - try { - translated = string.split('.').reduce((o, i) => o[i], languages[lang]); - } - catch (e) { - translated = string.split('.').reduce((o, i) => o[i], languages['en']); - } - if (translated === undefined) - translated = string.split('.').reduce((o, i) => o[i], languages['en']); - if (search !== '' && replace !== '') { - translated = translated.replace(search, replace); - } - return translated; -} - +const loc = new localise(window.navigator.language); /* eslint no-console: 0 */ -console.info(`%c IRRIGATION-UNLIMITED-CARD \n%c ${localize("common.version")} ${CARD_VERSION} `, "color: orange; font-weight: bold; background: black", "color: white; font-weight: bold; background: dimgray"); +console.info(`%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"); window.customCards = window.customCards || []; window.customCards.push({ type: "irrigation-unlimited-card", @@ -882,7 +969,7 @@ let IrrigationUnlimitedCard = class IrrigationUnlimitedCard extends s { } setConfig(config) { if (!config) { - throw new Error(localize("common.invalid_configuration")); + throw new Error(loc.t("common.invalidConfiguration")); } this.config = config; } @@ -1006,19 +1093,21 @@ let IrrigationUnlimitedCard = class IrrigationUnlimitedCard extends s {
- +
- + @@ -1403,7 +1492,7 @@ let IrrigationUnlimitedCard = class IrrigationUnlimitedCard extends s { >
-
Enable
+
${loc.t("menu.enable.name")}
${this._renderEnabled(isEnabled, isBlocked)} @@ -1412,16 +1501,13 @@ let IrrigationUnlimitedCard = class IrrigationUnlimitedCard extends s {
-
Suspend
+
${loc.t("menu.suspend.name")}
@@ -1438,13 +1525,13 @@ h:mm:ss
-
Manual
+
${loc.t("menu.manual.name")}
@@ -1463,11 +1551,12 @@ h:mm:ss
-
Pause
+
${loc.t("menu.pause.name")}
0} + title=${loc.t("menu.pause.buttonHint")} @click="${this._servicePause}" > @@ -1477,11 +1566,12 @@ h:mm:ss
-
Resume
+
${loc.t("menu.resume.name")}
0} + title=${loc.t("menu.resume.buttonHint")} @click="${this._serviceResume}" > @@ -1489,11 +1579,12 @@ h:mm:ss
-
Cancel
+
${loc.t("menu.cancel.name")}
@@ -1503,26 +1594,23 @@ h:mm:ss
-
Adjust
+
${loc.t("menu.adjust.name")}
- +
@@ -1536,6 +1624,7 @@ Reset: " `; diff --git a/src/editor.ts b/src/editor.ts index e98a130..ba2d333 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -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 { @@ -53,7 +55,7 @@ export class IrrigationUnlimitedCardEditor extends LitElement { return html`
- +
- +
${loc.t("editor.showTimelineScheduled.name")}
@@ -103,7 +109,9 @@ export class IrrigationUnlimitedCardEditor extends LitElement { .configValue=${"show_timeline_history"} @change=${this._valueChanged} > - +
`; } diff --git a/src/irrigation-unlimited-card.ts b/src/irrigation-unlimited-card.ts index e63c32d..b6f07c4 100644 --- a/src/irrigation-unlimited-card.ts +++ b/src/irrigation-unlimited-card.ts @@ -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" ); @@ -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; } @@ -191,19 +193,21 @@ export class IrrigationUnlimitedCard extends LitElement {
- +
- + @@ -628,7 +632,7 @@ export class IrrigationUnlimitedCard extends LitElement { >
-
Enable
+
${loc.t("menu.enable.name")}
${this._renderEnabled(isEnabled, isBlocked)} @@ -637,16 +641,13 @@ export class IrrigationUnlimitedCard extends LitElement {
-
Suspend
+
${loc.t("menu.suspend.name")}
@@ -663,13 +665,13 @@ h:mm:ss
-
Manual
+
${loc.t("menu.manual.name")}
@@ -688,11 +691,12 @@ h:mm:ss
-
Pause
+
${loc.t("menu.pause.name")}
0} + title=${loc.t("menu.pause.buttonHint")} @click="${this._servicePause}" > @@ -702,11 +706,12 @@ h:mm:ss
-
Resume
+
${loc.t("menu.resume.name")}
0} + title=${loc.t("menu.resume.buttonHint")} @click="${this._serviceResume}" > @@ -714,11 +719,12 @@ h:mm:ss
-
Cancel
+
${loc.t("menu.cancel.name")}
@@ -728,26 +734,23 @@ h:mm:ss
-
Adjust
+
${loc.t("menu.adjust.name")}
- +
@@ -765,6 +768,7 @@ Reset: " `; diff --git a/src/localize.ts b/src/localize.ts new file mode 100644 index 0000000..5b6c562 --- /dev/null +++ b/src/localize.ts @@ -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 ""; + } + } + } +} diff --git a/src/localize/languages/en.json b/src/localize/languages/en.json deleted file mode 100644 index e5a4b4d..0000000 --- a/src/localize/languages/en.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "common": { - "version": "Version", - "invalid_configuration": "Invalid configuration", - "show_warning": "Show Warning", - "show_error": "Show Error" - } -} \ No newline at end of file diff --git a/src/localize/languages/nb.json b/src/localize/languages/nb.json deleted file mode 100644 index dfe46b0..0000000 --- a/src/localize/languages/nb.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "common": { - "version": "Versjon", - "invalid_configuration": "Ikke gyldig konfiguration", - "show_warning": "Vis advarsel" - } -} \ No newline at end of file diff --git a/src/localize/localize.ts b/src/localize/localize.ts deleted file mode 100644 index 4d17345..0000000 --- a/src/localize/localize.ts +++ /dev/null @@ -1,27 +0,0 @@ -import * as en from './languages/en.json'; -import * as nb from './languages/nb.json'; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -const languages: any = { - en: en, - nb: nb, -}; - -export function localize(string: string, search = '', replace = ''): string { - const lang = (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_'); - - let translated: string; - - try { - translated = string.split('.').reduce((o, i) => o[i], languages[lang]); - } catch (e) { - translated = string.split('.').reduce((o, i) => o[i], languages['en']); - } - - if (translated === undefined) translated = string.split('.').reduce((o, i) => o[i], languages['en']); - - if (search !== '' && replace !== '') { - translated = translated.replace(search, replace); - } - return translated; -} diff --git a/src/translations/en.json b/src/translations/en.json new file mode 100644 index 0000000..0e12788 --- /dev/null +++ b/src/translations/en.json @@ -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 = 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: " + } + } +} diff --git a/src/translations/nb.json b/src/translations/nb.json new file mode 100644 index 0000000..573ac09 --- /dev/null +++ b/src/translations/nb.json @@ -0,0 +1,6 @@ +{ + "common": { + "version": "Versjon", + "invalid_configuration": "Ikke gyldig konfiguration" + } +}