MOBILE-3320 lang: Fix plugin strings not loaded if child lang

Before this change, the last language used was the parent one. This means the Translate library searched the strings in the parent language instead of the child one.
main
Dani Palou 2021-05-27 07:36:06 +02:00
parent 997d6185fd
commit 1cd937961f
1 changed files with 28 additions and 14 deletions

View File

@ -19,7 +19,7 @@ import { LangChangeEvent } from '@ngx-translate/core';
import { CoreAppProvider } from '@services/app';
import { CoreConfig } from '@services/config';
import { CoreSubscriptions } from '@singletons/subscriptions';
import { makeSingleton, Translate, Platform } from '@singletons';
import { makeSingleton, Translate, Platform, Http } from '@singletons';
import * as moment from 'moment';
import { CoreSite } from '../classes/site';
@ -142,24 +142,24 @@ export class CoreLangProvider {
// Change the language, resolving the promise when we receive the first value.
promises.push(new Promise((resolve, reject) => {
CoreSubscriptions.once(Translate.use(language), data => {
// It's a language override, load the original one first.
CoreSubscriptions.once(Translate.use(language), async data => {
// Check if it has a parent language.
const fallbackLang = this.getParentLanguage(language);
if (fallbackLang) {
CoreSubscriptions.once(
Translate.use(fallbackLang),
fallbackData => {
data = Object.assign(fallbackData, data);
try {
// Merge parent translations with the child ones.
const parentTranslations = Translate.translations[fallbackLang] ?? await this.readLangFile(fallbackLang);
resolve(data);
},
// Resolve with the original language.
() => resolve(data),
);
} else {
resolve(data);
const mergedData = Object.assign(parentTranslations, data);
Object.assign(data, mergedData);
} catch {
// Ignore errors.
}
}
resolve(data);
}, reject);
}));
@ -474,6 +474,20 @@ export class CoreLangProvider {
}
}
/**
* Read a language file.
*
* @param lang Language code.
* @return Promise resolved with the file contents.
*/
async readLangFile(lang: string): Promise<Record<string, string>> {
const observable = Http.get(`assets/lang/${lang}.json`, {
responseType: 'json',
});
return <Record<string, string>> await observable.toPromise();
}
/**
* Unload custom or site plugin strings, removing them from the translations table.
*