From bb23a319c882726aefee05cc0fef1cd3f3ffd8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 2 Oct 2018 15:26:19 +0200 Subject: [PATCH] MOBILE-2383 lang: Load parent lang if found --- scripts/langindex.json | 1 + src/lang/en.json | 1 + src/providers/lang.ts | 28 +++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index e9a449623..f10a008d0 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1412,6 +1412,7 @@ "core.openinbrowser": "local_moodlemobileapp", "core.othergroups": "group", "core.pagea": "moodle", + "core.parentlanguage": "langconfig", "core.paymentinstant": "moodle", "core.percentagenumber": "local_moodlemobileapp", "core.phone": "moodle", diff --git a/src/lang/en.json b/src/lang/en.json index 50b27e26b..0ead70cf3 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -174,6 +174,7 @@ "openinbrowser": "Open in browser", "othergroups": "Other groups", "pagea": "Page {{$a}}", + "parentlanguage": "", "paymentinstant": "Use the button below to pay and be enrolled within minutes!", "percentagenumber": "{{$a}}%", "phone": "Phone", diff --git a/src/providers/lang.ts b/src/providers/lang.ts index 95dffb104..cff4cd632 100644 --- a/src/providers/lang.ts +++ b/src/providers/lang.ts @@ -40,8 +40,7 @@ export class CoreLangProvider { platform.ready().then(() => { this.getCurrentLanguage().then((language) => { - translate.use(language); - moment.locale(language); + this.changeCurrentLanguage(language); }); }); @@ -97,7 +96,30 @@ export class CoreLangProvider { // Change the language, resolving the promise when we receive the first value. promises.push(new Promise((resolve, reject): void => { const subscription = this.translate.use(language).subscribe((data) => { - resolve(data); + // It's a language override, load the original one first. + const fallbackLang = this.translate.instant('core.parentlanguage'); + + if (fallbackLang != '' && fallbackLang != language) { + const fallbackSubs = this.translate.use(fallbackLang).subscribe((fallbackData) => { + data = Object.assign(fallbackData, data); + resolve(data); + + // Data received, unsubscribe. Use a timeout because we can receive a value immediately. + setTimeout(() => { + fallbackSubs.unsubscribe(); + }); + }, (error) => { + // Resolve with the original language. + resolve(data); + + // Error received, unsubscribe. Use a timeout because we can receive a value immediately. + setTimeout(() => { + fallbackSubs.unsubscribe(); + }); + }); + } else { + resolve(data); + } // Data received, unsubscribe. Use a timeout because we can receive a value immediately. setTimeout(() => {