1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-13 01:58:44 +02:00

abstract away loading messages file json

This commit is contained in:
Kyle Spearrin 2018-04-11 15:16:17 -04:00
parent 97be728b31
commit 44a9d17dba

View File

@ -11,7 +11,8 @@ export class I18nService implements I18nServiceAbstraction {
protected defaultMessages: any = {}; protected defaultMessages: any = {};
protected localeMessages: any = {}; protected localeMessages: any = {};
constructor(protected systemLanguage: string, protected localesDirectory: string) { } constructor(protected systemLanguage: string, protected localesDirectory: string,
protected getLocalesJson: (formattedLocale: string) => Promise<any>) { }
async init(locale?: string) { async init(locale?: string) {
if (this.inited) { if (this.inited) {
@ -34,10 +35,9 @@ export class I18nService implements I18nServiceAbstraction {
} }
if (this.localesDirectory != null) { if (this.localesDirectory != null) {
await this.loadMessages(this.localesDirectory, this.translationLocale, this.localeMessages); await this.loadMessages(this.translationLocale, this.localeMessages);
if (this.translationLocale !== this.supportedTranslationLocales[0]) { if (this.translationLocale !== this.supportedTranslationLocales[0]) {
await this.loadMessages(this.localesDirectory, this.supportedTranslationLocales[0], await this.loadMessages(this.supportedTranslationLocales[0], this.defaultMessages);
this.defaultMessages);
} }
} }
} }
@ -71,10 +71,9 @@ export class I18nService implements I18nServiceAbstraction {
return result; return result;
} }
private async loadMessages(localesDir: string, locale: string, messagesObj: any): Promise<any> { private async loadMessages(locale: string, messagesObj: any): Promise<any> {
const formattedLocale = locale.replace('-', '_'); const formattedLocale = locale.replace('-', '_');
const file = await fetch(localesDir + formattedLocale + '/messages.json'); const locales = await this.getLocalesJson(formattedLocale);
const locales = await file.json();
for (const prop in locales) { for (const prop in locales) {
if (!locales.hasOwnProperty(prop)) { if (!locales.hasOwnProperty(prop)) {
continue; continue;