From 5f6d3303866105759a8f86c2c5716cade919d0c9 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 16 May 2024 20:47:10 +0200 Subject: [PATCH 1/2] Fix translations in default locale --- public/scripts/localization.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/localization.js b/public/scripts/localization.js index f230add3..12a9a4c7 100644 --- a/public/scripts/localization.js +++ b/public/scripts/localization.js @@ -78,7 +78,7 @@ class Localization { static async setLocale(newLocale) { if (newLocale === Localization.locale) return false; - Localization.defaultTranslations = await Localization.fetchTranslationsFor(Localization.defaultLocale); + Localization.translationsDefaultLocale = await Localization.fetchTranslationsFor(Localization.localeDefault); const newTranslations = await Localization.fetchTranslationsFor(newLocale); From 51299bcf733be433b240fffa1c9dcf1ff20002df Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 16 May 2024 20:48:02 +0200 Subject: [PATCH 2/2] Refactor static variable names --- public/scripts/localization.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/public/scripts/localization.js b/public/scripts/localization.js index 12a9a4c7..5d5ee576 100644 --- a/public/scripts/localization.js +++ b/public/scripts/localization.js @@ -2,28 +2,28 @@ class Localization { constructor() { Localization.$htmlRoot = document.querySelector('html'); - Localization.defaultLocale = "en"; - Localization.supportedLocales = ["ar", "ca", "de", "en", "es", "fr", "id", "it", "ja", "kn", "nb", "nl", "pt-BR", "ro", "ru", "tr", "zh-CN"]; - Localization.supportedLocalesRtl = ["ar"]; + Localization.localeDefault = "en"; + Localization.localesSupported = ["ar", "ca", "de", "en", "es", "fr", "id", "it", "ja", "kn", "nb", "nl", "pt-BR", "ro", "ru", "tr", "zh-CN"]; + Localization.localesRtl = ["ar"]; Localization.translations = {}; Localization.translationsDefaultLocale = {}; - Localization.systemLocale = Localization.getSupportedOrDefaultLocales(navigator.languages); + Localization.localeSystem = Localization.getSupportedOrDefaultLocales(navigator.languages); let storedLanguageCode = localStorage.getItem('language_code'); - Localization.initialLocale = storedLanguageCode && Localization.localeIsSupported(storedLanguageCode) + Localization.localeInitial = storedLanguageCode && Localization.localeIsSupported(storedLanguageCode) ? storedLanguageCode - : Localization.systemLocale; + : Localization.localeSystem; } static localeIsSupported(locale) { - return Localization.supportedLocales.indexOf(locale) > -1; + return Localization.localesSupported.indexOf(locale) > -1; } static localeIsRtl(locale) { - return Localization.supportedLocalesRtl.indexOf(locale) > -1; + return Localization.localesRtl.indexOf(locale) > -1; } static currentLocaleIsRtl() { @@ -31,7 +31,7 @@ class Localization { } static currentLocaleIsDefault() { - return Localization.locale === Localization.defaultLocale + return Localization.locale === Localization.localeDefault } static getSupportedOrDefaultLocales(locales) { @@ -44,15 +44,15 @@ class Localization { // If there is no perfect match for browser locales, try generic locales first before resorting to the default locale return locales.find(Localization.localeIsSupported) || localesGeneric.find(Localization.localeIsSupported) - || Localization.defaultLocale; + || Localization.localeDefault; } async setInitialTranslation() { - await Localization.setTranslation(Localization.initialLocale) + await Localization.setTranslation(Localization.localeInitial) } static async setTranslation(locale) { - if (!locale) locale = Localization.systemLocale; + if (!locale) locale = Localization.localeSystem; await Localization.setLocale(locale) await Localization.translatePage(); @@ -68,7 +68,7 @@ class Localization { Logger.debug("Page successfully translated", - `System language: ${Localization.systemLocale}`, + `System language: ${Localization.localeSystem}`, `Selected language: ${locale}` ); @@ -192,7 +192,7 @@ class Localization { else { // Is not default locale yet // Get translation for default language with same arguments - Logger.debug(`Using default language ${Localization.defaultLocale.toUpperCase()} instead.`); + Logger.debug(`Using default language ${Localization.localeDefault.toUpperCase()} instead.`); translation = this.getTranslation(key, attr, data, true); } } @@ -202,7 +202,7 @@ class Localization { static logTranslationMissingOrBroken(key, attr, data, useDefault) { let usedLocale = useDefault - ? Localization.defaultLocale.toUpperCase() + ? Localization.localeDefault.toUpperCase() : Localization.locale.toUpperCase(); Logger.warn(`Missing or broken translation for language ${usedLocale}.\n`, 'key:', key, 'attr:', attr, 'data:', data);