summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/localization.js36
-rw-r--r--app/ui.js18
2 files changed, 35 insertions, 19 deletions
diff --git a/app/localization.js b/app/localization.js
index 73f66c5..7d7e6e6 100644
--- a/app/localization.js
+++ b/app/localization.js
@@ -16,13 +16,19 @@ export class Localizer {
this.language = 'en';
// Current dictionary of translations
- this.dictionary = undefined;
+ this._dictionary = undefined;
}
// Configure suitable language based on user preferences
- setup(supportedLanguages) {
+ async setup(supportedLanguages, baseURL) {
this.language = 'en'; // Default: US English
+ this._dictionary = undefined;
+ this._setupLanguage(supportedLanguages);
+ await this._setupDictionary(baseURL);
+ }
+
+ _setupLanguage(supportedLanguages) {
/*
* Navigator.languages only available in Chrome (32+) and FireFox (32+)
* Fall back to navigator.language for other browsers
@@ -83,10 +89,32 @@ export class Localizer {
}
}
+ async _setupDictionary(baseURL) {
+ if (baseURL) {
+ if (!baseURL.endsWith("/")) {
+ baseURL = baseURL + "/";
+ }
+ } else {
+ baseURL = "";
+ }
+
+ if (this.language === "en") {
+ return;
+ }
+
+ let response = await fetch(baseURL + this.language + ".json");
+ if (!response.ok) {
+ throw Error("" + response.status + " " + response.statusText);
+ }
+
+ this._dictionary = await response.json();
+ }
+
// Retrieve localised text
get(id) {
- if (typeof this.dictionary !== 'undefined' && this.dictionary[id]) {
- return this.dictionary[id];
+ if (typeof this._dictionary !== 'undefined' &&
+ this._dictionary[id]) {
+ return this._dictionary[id];
} else {
return id;
}
diff --git a/app/ui.js b/app/ui.js
index c1f6776..85695ca 100644
--- a/app/ui.js
+++ b/app/ui.js
@@ -1763,20 +1763,8 @@ const UI = {
// Set up translations
const LINGUAS = ["cs", "de", "el", "es", "fr", "it", "ja", "ko", "nl", "pl", "pt_BR", "ru", "sv", "tr", "zh_CN", "zh_TW"];
-l10n.setup(LINGUAS);
-if (l10n.language === "en" || l10n.dictionary !== undefined) {
- UI.prime();
-} else {
- fetch('app/locale/' + l10n.language + '.json')
- .then((response) => {
- if (!response.ok) {
- throw Error("" + response.status + " " + response.statusText);
- }
- return response.json();
- })
- .then((translations) => { l10n.dictionary = translations; })
- .catch(err => Log.Error("Failed to load translations: " + err))
- .then(UI.prime);
-}
+l10n.setup(LINGUAS, "app/locale/")
+ .catch(err => Log.Error("Failed to load translations: " + err))
+ .then(UI.prime);
export default UI;