MOBILE-3565 core: Remove globalization service from lang service

main
Pau Ferrer Ocaña 2020-10-14 23:30:15 +02:00
parent 0b40fd0baa
commit dd636e61fd
5 changed files with 60 additions and 65 deletions

13
package-lock.json generated
View File

@ -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"

View File

@ -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",

View File

@ -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,

View File

@ -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<void> {
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<string> {
// Get current language from config (user might have changed it).
return CoreConfig.instance.get<string>('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<string>('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;
}
/**

View File

@ -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) {}