fradrive/frontend/src/services/i18n/i18n.js
Gregor Kleen 5d8c2af51d feat(frontend): use webpack more extensively
Also include all fonts via npm

BREAKING CHANGE: Major frontend refactor
2019-12-11 15:11:44 +01:00

60 lines
1.5 KiB
JavaScript

import moment from 'moment';
import 'moment/locale/en-ie.js';
import 'moment/locale/de.js';
/**
* I18n
*
* This module stores and serves translated strings, according to the users language settings.
*
* Translations are stored in /messages/frontend/*.msg.
*
* To make additions to any of these files accessible to JavaScrip Utilities
* you need to add them to the respective *.msg file and to the list of FrontendMessages
* in /src/Utils/Frontend/I18n.hs.
*
*/
export class I18n {
_translations = {};
_datetimeLocale = undefined;
add(id, translation) {
if (!this._translations[id]) {
this._translations[id] = translation;
} else {
throw new Error('I18N Error: Attempting to set translation multiple times for »' + id + '«!');
}
}
addMany(manyTranslations) {
Object.keys(manyTranslations).forEach((key) => this.add(key, manyTranslations[key]));
}
get(id) {
if (!this._translations[id]) {
throw new Error('I18N Error: Translation missing for »' + id + '«!');
}
return this._translations[id];
}
setDatetimeLocale(locale) {
if (!this._datetimeLocale) {
moment.locale(locale);
this._datetimeLocale = locale;
} else {
throw new Error('I18N Error: Attempting to set datetime locale multiple times!');
}
}
getDatetimeLocale() {
if (!this._datetimeLocale) {
throw new Error('I18N Error: Attempting to access datetime locale when it has not been set!');
}
return this._datetimeLocale;
}
}