1
0
Fork 0
mirror of https://gitlab.com/Alamantus/Readlebee.git synced 2025-03-24 12:29:24 +01:00
Readlebee/app/i18n/index.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-09-09 16:19:39 -06:00
import en from './locales/en.json';
export class I18n {
constructor(appState) {
// Available languages should be kept up to date with the available locales.
this.availableLanguages = {
default: en,
en,
};
2019-10-17 22:15:31 -06:00
this.appState = appState;
}
get language () {
return this.appState.language;
2019-09-09 16:19:39 -06:00
}
translate (section, phrase) {
let result;
let language = this.availableLanguages.default;
if (typeof this.availableLanguages[this.language] == 'undefined') {
console.warn(`The target language (${this.language}) does not exist. Defaulting to ${this.availableLanguages.default.name} (${this.availableLanguages.default.locale}).`);
} else if (typeof this.availableLanguages[this.language][section] == 'undefined' || typeof this.availableLanguages[this.language][section][phrase] == 'undefined') {
console.warn(`The translation for "${section}.${phrase}" is not set in the ${this.language} locale. Using ${this.availableLanguages.default.name} (${this.availableLanguages.default.locale}) instead.`);
} else {
language = this.availableLanguages[this.language];
}
if (typeof language[section] !== 'undefined' && typeof language[section][phrase] !== 'undefined') {
result = language[section][phrase];
} else {
console.error(`The translation for "${section}.${phrase}" is set up in neither the target nor default locale.`);
2019-09-10 17:36:49 -06:00
result = `${section}.${phrase}`;
2019-09-09 16:19:39 -06:00
}
return result;
}
__ (translation) {
const pieces = translation.split('.');
const result = this.translate(pieces[0], pieces[1]);
return result;
}
}