MOBILE-3565 services: Fix lang service linting

main
Pau Ferrer Ocaña 2020-10-08 11:50:14 +02:00
parent efb9bad136
commit eb322dd073
1 changed files with 35 additions and 25 deletions

View File

@ -15,7 +15,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import CoreConfigConstants from '@app/config.json'; import CoreConfigConstants from '@app/config.json';
import { CoreApp, CoreAppProvider } from '@services/app'; import { CoreAppProvider } from '@services/app';
import { CoreConfig } from '@services/config'; import { CoreConfig } from '@services/config';
import { makeSingleton, Translate, Platform, Globalization } from '@singletons/core.singletons'; import { makeSingleton, Translate, Platform, Globalization } from '@singletons/core.singletons';
@ -26,6 +26,7 @@ import * as moment from 'moment';
*/ */
@Injectable() @Injectable()
export class CoreLangProvider { export class CoreLangProvider {
protected fallbackLanguage = 'en'; // Always use English as fallback language since it contains all strings. protected fallbackLanguage = 'en'; // Always use English as fallback language since it contains all strings.
protected defaultLanguage = CoreConfigConstants.default_lang || 'en'; // Lang to use if device lang not valid or is forced. protected defaultLanguage = CoreConfigConstants.default_lang || 'en'; // Lang to use if device lang not valid or is forced.
protected currentLanguage: string; // Save current language in a variable to speed up the get function. protected currentLanguage: string; // Save current language in a variable to speed up the get function.
@ -51,7 +52,7 @@ export class CoreLangProvider {
}); });
}); });
Translate.instance.onLangChange.subscribe((event: any) => { Translate.instance.onLangChange.subscribe(() => {
// @todo: Set platform lang and dir. // @todo: Set platform lang and dir.
}); });
} }
@ -63,7 +64,7 @@ export class CoreLangProvider {
* @param strings Object with the strings to add. * @param strings Object with the strings to add.
* @param prefix A prefix to add to all keys. * @param prefix A prefix to add to all keys.
*/ */
addSitePluginsStrings(lang: string, strings: any, prefix?: string): void { addSitePluginsStrings(lang: string, strings: string[], prefix?: string): void {
lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format. lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format.
// Initialize structure if it doesn't exist. // Initialize structure if it doesn't exist.
@ -109,7 +110,7 @@ export class CoreLangProvider {
* @param language New language to use. * @param language New language to use.
* @return Promise resolved when the change is finished. * @return Promise resolved when the change is finished.
*/ */
changeCurrentLanguage(language: string): Promise<any> { changeCurrentLanguage(language: string): Promise<unknown> {
const promises = []; const promises = [];
// Change the language, resolving the promise when we receive the first value. // Change the language, resolving the promise when we receive the first value.
@ -127,7 +128,7 @@ export class CoreLangProvider {
setTimeout(() => { setTimeout(() => {
fallbackSubs.unsubscribe(); fallbackSubs.unsubscribe();
}); });
}, (error) => { }, () => {
// Resolve with the original language. // Resolve with the original language.
resolve(data); resolve(data);
@ -168,7 +169,7 @@ export class CoreLangProvider {
// Load the custom and site plugins strings for the language. // Load the custom and site plugins strings for the language.
if (this.loadLangStrings(this.customStrings, language) || this.loadLangStrings(this.sitePluginsStrings, language)) { if (this.loadLangStrings(this.customStrings, language) || this.loadLangStrings(this.sitePluginsStrings, language)) {
// Some lang strings have changed, emit an event to update the pipes. // Some lang strings have changed, emit an event to update the pipes.
Translate.instance.onLangChange.emit({lang: language, translations: Translate.instance.translations[language]}); Translate.instance.onLangChange.emit({ lang: language, translations: Translate.instance.translations[language] });
} }
}); });
} }
@ -195,7 +196,7 @@ export class CoreLangProvider {
* *
* @return Custom strings. * @return Custom strings.
*/ */
getAllCustomStrings(): any { getAllCustomStrings(): unknown {
return this.customStrings; return this.customStrings;
} }
@ -204,7 +205,7 @@ export class CoreLangProvider {
* *
* @return Site plugins strings. * @return Site plugins strings.
*/ */
getAllSitePluginsStrings(): any { getAllSitePluginsStrings(): unknown {
return this.sitePluginsStrings; return this.sitePluginsStrings;
} }
@ -213,16 +214,13 @@ export class CoreLangProvider {
* *
* @return Promise resolved with the current language. * @return Promise resolved with the current language.
*/ */
getCurrentLanguage(): Promise<string> { async getCurrentLanguage(): Promise<string> {
if (typeof this.currentLanguage != 'undefined') { if (typeof this.currentLanguage != 'undefined') {
return Promise.resolve(this.currentLanguage); return this.currentLanguage;
} }
// Get current language from config (user might have changed it). // Get current language from config (user might have changed it).
return CoreConfig.instance.get('current_language').then((language) => { return CoreConfig.instance.get('current_language').then((language) => language).catch(() => {
return language;
}).catch(() => {
// User hasn't defined a language. If default language is forced, use it. // User hasn't defined a language. If default language is forced, use it.
if (CoreConfigConstants.default_lang && CoreConfigConstants.forcedefaultlanguage) { if (CoreConfigConstants.default_lang && CoreConfigConstants.forcedefaultlanguage) {
return CoreConfigConstants.default_lang; return CoreConfigConstants.default_lang;
@ -237,7 +235,6 @@ export class CoreLangProvider {
if (CoreConfigConstants.languages && typeof CoreConfigConstants.languages[language] == 'undefined') { 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'. // Code is NOT supported. Fallback to language without dash. E.g. 'en-US' would fallback to 'en'.
language = language.substr(0, language.indexOf('-')); language = language.substr(0, language.indexOf('-'));
} }
} }
@ -247,10 +244,10 @@ export class CoreLangProvider {
} }
return language; return language;
}).catch(() => { }).catch(() =>
// Error getting locale. Use default language. // Error getting locale. Use default language.
return this.defaultLanguage; this.defaultLanguage,
}); );
} catch (err) { } catch (err) {
// Error getting locale. Use default language. // Error getting locale. Use default language.
return Promise.resolve(this.defaultLanguage); return Promise.resolve(this.defaultLanguage);
@ -286,7 +283,7 @@ export class CoreLangProvider {
* @param lang The language to check. * @param lang The language to check.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
getTranslationTable(lang: string): Promise<any> { getTranslationTable(lang: string): Promise<unknown> {
// Create a promise to convert the observable into a promise. // Create a promise to convert the observable into a promise.
return new Promise((resolve, reject): void => { return new Promise((resolve, reject): void => {
const observer = Translate.instance.getTranslation(lang).subscribe((table) => { const observer = Translate.instance.getTranslation(lang).subscribe((table) => {
@ -322,14 +319,13 @@ export class CoreLangProvider {
const list: string[] = strings.split(/(?:\r\n|\r|\n)/); const list: string[] = strings.split(/(?:\r\n|\r|\n)/);
list.forEach((entry: string) => { list.forEach((entry: string) => {
const values: string[] = entry.split('|'); const values: string[] = entry.split('|');
let lang: string;
if (values.length < 3) { if (values.length < 3) {
// Not enough data, ignore the entry. // Not enough data, ignore the entry.
return; return;
} }
lang = values[2].replace(/_/g, '-'); // Use the app format instead of Moodle format. const lang = values[2].replace(/_/g, '-'); // Use the app format instead of Moodle format.
if (lang == this.currentLanguage) { if (lang == this.currentLanguage) {
currentLangChanged = true; currentLangChanged = true;
@ -353,7 +349,7 @@ export class CoreLangProvider {
// Some lang strings have changed, emit an event to update the pipes. // Some lang strings have changed, emit an event to update the pipes.
Translate.instance.onLangChange.emit({ Translate.instance.onLangChange.emit({
lang: this.currentLanguage, lang: this.currentLanguage,
translations: Translate.instance.translations[this.currentLanguage] translations: Translate.instance.translations[this.currentLanguage],
}); });
} }
} }
@ -365,7 +361,7 @@ export class CoreLangProvider {
* @param lang Language to load. * @param lang Language to load.
* @return Whether the translation table was modified. * @return Whether the translation table was modified.
*/ */
loadLangStrings(langObject: any, lang: string): boolean { loadLangStrings(langObject: CoreLanguageObject, lang: string): boolean {
let langApplied = false; let langApplied = false;
if (langObject[lang]) { if (langObject[lang]) {
@ -396,7 +392,7 @@ export class CoreLangProvider {
* @param key String key. * @param key String key.
* @param value String value. * @param value String value.
*/ */
loadString(langObject: any, lang: string, key: string, value: string): void { loadString(langObject: CoreLanguageObject, lang: string, key: string, value: string): void {
lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format. lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format.
if (Translate.instance.translations[lang]) { if (Translate.instance.translations[lang]) {
@ -425,7 +421,7 @@ export class CoreLangProvider {
* *
* @param strings Strings to unload. * @param strings Strings to unload.
*/ */
protected unloadStrings(strings: any): void { protected unloadStrings(strings: CoreLanguageObject): void {
// Iterate over all languages and strings. // Iterate over all languages and strings.
for (const lang in strings) { for (const lang in strings) {
if (!Translate.instance.translations[lang]) { if (!Translate.instance.translations[lang]) {
@ -446,6 +442,20 @@ export class CoreLangProvider {
} }
} }
} }
} }
export class CoreLang extends makeSingleton(CoreLangProvider) {} export class CoreLang extends makeSingleton(CoreLangProvider) {}
/**
* Language object has two leves, first per language and second per string key.
*/
type CoreLanguageObject = {
[s: string]: { // Lang name.
[s: string]: { // String key.
value: string; // Value with replacings done.
original?: string; // Original value of the string.
applied?: boolean; // If the key is applied to the translations table or not.
};
};
};