From 1cd937961fa7974daaedb3bd1608fe389273a71a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 27 May 2021 07:36:06 +0200 Subject: [PATCH] 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. --- src/core/services/lang.ts | 42 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/core/services/lang.ts b/src/core/services/lang.ts index 9f1de3abe..8e64effeb 100644 --- a/src/core/services/lang.ts +++ b/src/core/services/lang.ts @@ -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> { + const observable = Http.get(`assets/lang/${lang}.json`, { + responseType: 'json', + }); + + return > await observable.toPromise(); + } + /** * Unload custom or site plugin strings, removing them from the translations table. *