From dd636e61fd5034ab88c9df287729118b3f29728e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 14 Oct 2020 23:30:15 +0200 Subject: [PATCH] MOBILE-3565 core: Remove globalization service from lang service --- package-lock.json | 13 --- package.json | 2 - src/app/core/emulator/emulator.module.ts | 2 - src/app/services/lang.ts | 106 +++++++++++++---------- src/app/singletons/core.singletons.ts | 2 - 5 files changed, 60 insertions(+), 65 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4c2bf21f4..60cadcaa4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2193,14 +2193,6 @@ "@types/cordova": "^0.0.34" } }, - "@ionic-native/globalization": { - "version": "5.28.0", - "resolved": "https://registry.npmjs.org/@ionic-native/globalization/-/globalization-5.28.0.tgz", - "integrity": "sha512-+L/svbUNnJgbeUs70G7vdRUjqWPIB2gOtUiCo7jr6BWsPqfw7iNjBF15ZjGLcbkzRIRd3wezTgPInbpqHDIWow==", - "requires": { - "@types/cordova": "^0.0.34" - } - }, "@ionic-native/http": { "version": "5.28.0", "resolved": "https://registry.npmjs.org/@ionic-native/http/-/http-5.28.0.tgz", @@ -6119,11 +6111,6 @@ "version": "git+https://github.com/apache/cordova-plugin-geolocation.git#89cf51d222e8f225bdfb661965b3007d669c40ff", "from": "git+https://github.com/apache/cordova-plugin-geolocation.git#89cf51d222e8f225bdfb661965b3007d669c40ff" }, - "cordova-plugin-globalization": { - "version": "1.11.0", - "resolved": "https://registry.npmjs.org/cordova-plugin-globalization/-/cordova-plugin-globalization-1.11.0.tgz", - "integrity": "sha1-6sMVgQAphJOvowvolA5pj2HvvP4=" - }, "cordova-plugin-inappbrowser": { "version": "git+https://github.com/moodlemobile/cordova-plugin-inappbrowser.git#715c858975cc1cb5d140afaa7973938511d38509", "from": "git+https://github.com/moodlemobile/cordova-plugin-inappbrowser.git#moodle" diff --git a/package.json b/package.json index 14fd1b867..e42b50605 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "@ionic-native/file-opener": "^5.28.0", "@ionic-native/file-transfer": "^5.28.0", "@ionic-native/geolocation": "^5.28.0", - "@ionic-native/globalization": "^5.28.0", "@ionic-native/http": "^5.28.0", "@ionic-native/in-app-browser": "^5.28.0", "@ionic-native/ionic-webview": "^5.28.0", @@ -79,7 +78,6 @@ "cordova-plugin-file-opener2": "^3.0.5", "cordova-plugin-file-transfer": "1.7.1", "cordova-plugin-geolocation": "git+https://github.com/apache/cordova-plugin-geolocation.git#89cf51d222e8f225bdfb661965b3007d669c40ff", - "cordova-plugin-globalization": "1.11.0", "cordova-plugin-inappbrowser": "git+https://github.com/moodlemobile/cordova-plugin-inappbrowser.git#moodle", "cordova-plugin-ionic-keyboard": "2.1.3", "cordova-plugin-ionic-webview": "git+https://github.com/moodlemobile/cordova-plugin-ionic-webview.git#500-moodle", diff --git a/src/app/core/emulator/emulator.module.ts b/src/app/core/emulator/emulator.module.ts index 2b73dbc3f..dc81075e1 100644 --- a/src/app/core/emulator/emulator.module.ts +++ b/src/app/core/emulator/emulator.module.ts @@ -22,7 +22,6 @@ import { File } from '@ionic-native/file/ngx'; import { FileOpener } from '@ionic-native/file-opener/ngx'; import { FileTransfer } from '@ionic-native/file-transfer/ngx'; import { Geolocation } from '@ionic-native/geolocation/ngx'; -import { Globalization } from '@ionic-native/globalization/ngx'; import { HTTP } from '@ionic-native/http/ngx'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; import { WebView } from '@ionic-native/ionic-webview/ngx'; @@ -59,7 +58,6 @@ import { Zip } from '@ionic-native/zip/ngx'; FileOpener, FileTransfer, Geolocation, - Globalization, HTTP, InAppBrowser, Keyboard, diff --git a/src/app/services/lang.ts b/src/app/services/lang.ts index 2b89a0fd1..5a763614d 100644 --- a/src/app/services/lang.ts +++ b/src/app/services/lang.ts @@ -15,9 +15,10 @@ import { Injectable } from '@angular/core'; import CoreConfigConstants from '@app/config.json'; +import { LangChangeEvent } from '@ngx-translate/core'; import { CoreAppProvider } from '@services/app'; import { CoreConfig } from '@services/config'; -import { makeSingleton, Translate, Platform, Globalization } from '@singletons/core.singletons'; +import { makeSingleton, Translate, Platform } from '@singletons/core.singletons'; import * as moment from 'moment'; @@ -39,22 +40,33 @@ export class CoreLangProvider { Translate.instance.setDefaultLang(this.fallbackLanguage); Translate.instance.use(this.defaultLanguage); - Platform.instance.ready().then(() => { - if (CoreAppProvider.isAutomated()) { - // Force current language to English when Behat is running. - this.changeCurrentLanguage('en'); + this.initLanguage(); - return; - } + Translate.instance.onLangChange.subscribe((event: LangChangeEvent) => { + document.documentElement.setAttribute('lang', event.lang); - this.getCurrentLanguage().then((language) => { - this.changeCurrentLanguage(language); - }); + let dir = Translate.instance.instant('core.thisdirection'); + dir = dir.indexOf('rtl') != -1 ? 'rtl' : 'ltr'; + document.documentElement.setAttribute('dir', dir); }); + } - Translate.instance.onLangChange.subscribe(() => { - // @todo: Set platform lang and dir. - }); + /** + * Init language. + */ + protected async initLanguage(): Promise { + await Platform.instance.ready(); + + let language: string; + + if (CoreAppProvider.isAutomated()) { + // Force current language to English when Behat is running. + language = 'en'; + } else { + language = await this.getCurrentLanguage(); + } + + return this.changeCurrentLanguage(language); } /** @@ -221,43 +233,45 @@ export class CoreLangProvider { return this.currentLanguage; } + this.currentLanguage = await this.detectLanguage(); + + return this.currentLanguage; + } + + /** + * Get the current language from settings, or detect the browser one. + * + * @return Promise resolved with the selected language. + */ + protected async detectLanguage(): Promise { // Get current language from config (user might have changed it). - return CoreConfig.instance.get('current_language').then((language) => language).catch(() => { - // User hasn't defined a language. If default language is forced, use it. - if (CoreConfigConstants.default_lang && CoreConfigConstants.forcedefaultlanguage) { - return CoreConfigConstants.default_lang; + try { + return CoreConfig.instance.get('current_language'); + } catch (e) { + // Try will return, ignore errors here to avoid nesting. + } + + // User hasn't defined a language. If default language is forced, use it. + if (CoreConfigConstants.default_lang && CoreConfigConstants.forcedefaultlanguage) { + return CoreConfigConstants.default_lang; + } + + // No forced language, try to get current language from browser. + let preferredLanguage = navigator.language.toLowerCase(); + if (preferredLanguage.indexOf('-') > -1) { + // Language code defined by locale has a dash, like en-US or es-ES. Check if it's supported. + if (CoreConfigConstants.languages && typeof CoreConfigConstants.languages[preferredLanguage] == 'undefined') { + // Code is NOT supported. Fallback to language without dash. E.g. 'en-US' would fallback to 'en'. + preferredLanguage = preferredLanguage.substr(0, preferredLanguage.indexOf('-')); } + } - try { - // No forced language, try to get current language from cordova globalization. - return Globalization.instance.getPreferredLanguage().then((result) => { - let language = result.value.toLowerCase(); - if (language.indexOf('-') > -1) { - // Language code defined by locale has a dash, like en-US or es-ES. Check if it's supported. - if (CoreConfigConstants.languages && typeof CoreConfigConstants.languages[language] == 'undefined') { - // Code is NOT supported. Fallback to language without dash. E.g. 'en-US' would fallback to 'en'. - language = language.substr(0, language.indexOf('-')); - } - } + if (typeof CoreConfigConstants.languages[preferredLanguage] == 'undefined') { + // Language not supported, use default language. + return this.defaultLanguage; + } - if (typeof CoreConfigConstants.languages[language] == 'undefined') { - // Language not supported, use default language. - return this.defaultLanguage; - } - - return language; - }).catch(() => - // Error getting locale. Use default language. - this.defaultLanguage); - } catch (err) { - // Error getting locale. Use default language. - return Promise.resolve(this.defaultLanguage); - } - }).then((language) => { - this.currentLanguage = language; // Save it for later. - - return language; - }); + return preferredLanguage; } /** diff --git a/src/app/singletons/core.singletons.ts b/src/app/singletons/core.singletons.ts index 441573fa5..45fcba1a6 100644 --- a/src/app/singletons/core.singletons.ts +++ b/src/app/singletons/core.singletons.ts @@ -30,7 +30,6 @@ import { File as FileService } from '@ionic-native/file/ngx'; import { FileOpener as FileOpenerService } from '@ionic-native/file-opener/ngx'; import { FileTransfer as FileTransferService } from '@ionic-native/file-transfer/ngx'; import { Geolocation as GeolocationService } from '@ionic-native/geolocation/ngx'; -import { Globalization as GlobalizationService } from '@ionic-native/globalization/ngx'; import { HTTP } from '@ionic-native/http/ngx'; import { InAppBrowser as InAppBrowserService } from '@ionic-native/in-app-browser/ngx'; import { WebView as WebViewService } from '@ionic-native/ionic-webview/ngx'; @@ -78,7 +77,6 @@ export class File extends makeSingleton(FileService) {} export class FileOpener extends makeSingleton(FileOpenerService) {} export class FileTransfer extends makeSingleton(FileTransferService) {} export class Geolocation extends makeSingleton(GeolocationService) {} -export class Globalization extends makeSingleton(GlobalizationService) {} export class InAppBrowser extends makeSingleton(InAppBrowserService) {} export class Keyboard extends makeSingleton(KeyboardService) {} export class LocalNotifications extends makeSingleton(LocalNotificationsService) {}