2020-10-07 10:53:19 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2020-11-19 12:40:18 +01:00
|
|
|
import { CoreConstants } from '@/core/constants';
|
2020-10-14 23:30:15 +02:00
|
|
|
import { LangChangeEvent } from '@ngx-translate/core';
|
2020-10-08 11:50:14 +02:00
|
|
|
import { CoreAppProvider } from '@services/app';
|
2020-10-07 10:53:19 +02:00
|
|
|
import { CoreConfig } from '@services/config';
|
2021-02-02 18:41:58 +01:00
|
|
|
import { CoreSubscriptions } from '@singletons/subscriptions';
|
2022-06-20 18:19:18 +02:00
|
|
|
import { makeSingleton, Translate, Http } from '@singletons';
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2022-07-27 08:45:51 +02:00
|
|
|
import moment from 'moment-timezone';
|
2023-11-08 12:15:39 +01:00
|
|
|
import { CoreSite } from '../classes/sites/site';
|
2022-06-20 18:19:18 +02:00
|
|
|
import { CorePlatform } from '@services/platform';
|
2023-04-20 12:22:41 +02:00
|
|
|
import { AddonFilterMultilangHandler } from '@addons/filter/multilang/services/handlers/multilang';
|
|
|
|
import { AddonFilterMultilang2Handler } from '@addons/filter/multilang2/services/handlers/multilang2';
|
2023-11-23 15:25:36 +01:00
|
|
|
import { firstValueFrom } from 'rxjs';
|
2020-10-07 10:53:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Service to handle language features, like changing the current language.
|
|
|
|
*/
|
2020-11-19 16:35:17 +01:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 10:53:19 +02:00
|
|
|
export class CoreLangProvider {
|
2020-10-08 11:50:14 +02:00
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
protected fallbackLanguage = 'en'; // Always use English as fallback language since it contains all strings.
|
2020-10-21 17:56:01 +02:00
|
|
|
protected defaultLanguage = CoreConstants.CONFIG.default_lang || 'en'; // Lang to use if device lang not valid or is forced.
|
2020-10-16 13:08:19 +02:00
|
|
|
protected currentLanguage?: string; // Save current language in a variable to speed up the get function.
|
2020-10-14 16:38:24 +02:00
|
|
|
protected customStrings: CoreLanguageObject = {}; // Strings defined using the admin tool.
|
2020-10-16 13:08:19 +02:00
|
|
|
protected customStringsRaw?: string;
|
2020-10-14 16:38:24 +02:00
|
|
|
protected sitePluginsStrings: CoreLanguageObject = {}; // Strings defined by site plugins.
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2021-08-12 13:26:46 +02:00
|
|
|
async initialize(): Promise<void> {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Set fallback language and language to use until the app determines the right language to use.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.setDefaultLang(this.fallbackLanguage);
|
|
|
|
Translate.use(this.defaultLanguage);
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.onLangChange.subscribe((event: LangChangeEvent) => {
|
2020-10-14 23:30:15 +02:00
|
|
|
document.documentElement.setAttribute('lang', event.lang);
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
let dir = Translate.instant('core.thisdirection');
|
2020-10-14 23:30:15 +02:00
|
|
|
dir = dir.indexOf('rtl') != -1 ? 'rtl' : 'ltr';
|
|
|
|
document.documentElement.setAttribute('dir', dir);
|
2020-10-07 10:53:19 +02:00
|
|
|
});
|
2021-08-12 13:26:46 +02:00
|
|
|
|
2021-08-31 17:40:12 +02:00
|
|
|
this.initializeCurrentLanguage();
|
2020-10-14 23:30:15 +02:00
|
|
|
}
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2020-10-14 23:30:15 +02:00
|
|
|
/**
|
|
|
|
* Init language.
|
|
|
|
*/
|
2021-08-12 13:26:46 +02:00
|
|
|
protected async initializeCurrentLanguage(): Promise<void> {
|
2022-06-20 18:19:18 +02:00
|
|
|
await CorePlatform.ready();
|
2020-10-14 23:30:15 +02:00
|
|
|
|
|
|
|
let language: string;
|
|
|
|
|
|
|
|
if (CoreAppProvider.isAutomated()) {
|
|
|
|
// Force current language to English when Behat is running.
|
|
|
|
language = 'en';
|
|
|
|
} else {
|
|
|
|
language = await this.getCurrentLanguage();
|
|
|
|
}
|
|
|
|
|
2021-08-12 13:26:46 +02:00
|
|
|
await this.changeCurrentLanguage(language);
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a set of site plugins strings for a certain language.
|
|
|
|
*
|
|
|
|
* @param lang The language where to add the strings.
|
|
|
|
* @param strings Object with the strings to add.
|
|
|
|
* @param prefix A prefix to add to all keys.
|
|
|
|
*/
|
2020-10-08 11:50:14 +02:00
|
|
|
addSitePluginsStrings(lang: string, strings: string[], prefix?: string): void {
|
2020-10-07 10:53:19 +02:00
|
|
|
lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format.
|
|
|
|
|
|
|
|
// Initialize structure if it doesn't exist.
|
|
|
|
if (!this.sitePluginsStrings[lang]) {
|
|
|
|
this.sitePluginsStrings[lang] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in strings) {
|
|
|
|
const prefixedKey = prefix + key;
|
|
|
|
let value = strings[key];
|
|
|
|
|
|
|
|
if (this.customStrings[lang] && this.customStrings[lang][prefixedKey]) {
|
|
|
|
// This string is overridden by a custom string, ignore it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the way to access subproperties.
|
|
|
|
value = value.replace(/\$a->/gm, '$a.');
|
|
|
|
// Add another curly bracket to string params ({$a} -> {{$a}}).
|
|
|
|
value = value.replace(/{([^ ]+)}/gm, '{{$1}}');
|
|
|
|
// Make sure we didn't add to many brackets in some case.
|
|
|
|
value = value.replace(/{{{([^ ]+)}}}/gm, '{{$1}}');
|
|
|
|
|
|
|
|
// Load the string.
|
|
|
|
this.loadString(this.sitePluginsStrings, lang, prefixedKey, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Capitalize a string (make the first letter uppercase).
|
|
|
|
* We cannot use a function from text utils because it would cause a circular dependency.
|
|
|
|
*
|
|
|
|
* @param value String to capitalize.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Capitalized string.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
|
|
|
protected capitalize(value: string): string {
|
|
|
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
|
|
}
|
|
|
|
|
2022-11-22 17:54:01 +01:00
|
|
|
/**
|
|
|
|
* Get message for the given language.
|
|
|
|
*
|
|
|
|
* @param key Message key.
|
|
|
|
* @param lang Language.
|
|
|
|
* @returns Message if found, null otherwise.
|
|
|
|
*/
|
|
|
|
async getMessage(key: string, lang: string): Promise<string | null> {
|
|
|
|
const messages = await this.getMessages(lang);
|
|
|
|
|
|
|
|
return messages[key] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get messages for the given language.
|
|
|
|
*
|
|
|
|
* @param lang Language.
|
|
|
|
* @returns Messages.
|
|
|
|
*/
|
|
|
|
getMessages(lang: string): Promise<Record<string, string>> {
|
|
|
|
return new Promise(resolve => CoreSubscriptions.once(
|
|
|
|
Translate.getTranslation(lang),
|
|
|
|
messages => resolve(messages),
|
|
|
|
() => resolve({}),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:03:57 +02:00
|
|
|
/**
|
|
|
|
* Get the parent language defined on the language strings.
|
|
|
|
*
|
|
|
|
* @returns If a parent language is set, return the index name.
|
|
|
|
*/
|
2023-04-26 10:20:18 +02:00
|
|
|
getParentLanguage(): string | undefined {
|
2021-05-13 17:03:57 +02:00
|
|
|
const parentLang = Translate.instant('core.parentlanguage');
|
2023-04-26 10:20:18 +02:00
|
|
|
if (parentLang !== '' && parentLang !== 'core.parentlanguage' && parentLang !== this.currentLanguage) {
|
2021-05-13 17:03:57 +02:00
|
|
|
return parentLang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
/**
|
|
|
|
* Change current language.
|
|
|
|
*
|
|
|
|
* @param language New language to use.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Promise resolved when the change is finished.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2020-10-14 16:38:24 +02:00
|
|
|
async changeCurrentLanguage(language: string): Promise<void> {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Use british english when parent english is loaded.
|
|
|
|
moment.locale(language == 'en' ? 'en-gb' : language);
|
|
|
|
|
|
|
|
this.currentLanguage = language;
|
|
|
|
|
2020-10-14 16:38:24 +02:00
|
|
|
try {
|
2023-04-26 10:20:18 +02:00
|
|
|
await Promise.all([
|
|
|
|
this.reloadLanguageStrings(),
|
|
|
|
CoreConfig.set('current_language', language),
|
|
|
|
]);
|
2020-10-14 16:38:24 +02:00
|
|
|
} finally {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Load the custom and site plugins strings for the language.
|
|
|
|
if (this.loadLangStrings(this.customStrings, language) || this.loadLangStrings(this.sitePluginsStrings, language)) {
|
|
|
|
// Some lang strings have changed, emit an event to update the pipes.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.onLangChange.emit({ lang: language, translations: Translate.translations[language] });
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
2020-10-14 16:38:24 +02:00
|
|
|
}
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear current custom strings.
|
|
|
|
*/
|
|
|
|
clearCustomStrings(): void {
|
|
|
|
this.unloadStrings(this.customStrings);
|
|
|
|
this.customStrings = {};
|
|
|
|
this.customStringsRaw = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear current site plugins strings.
|
|
|
|
*/
|
|
|
|
clearSitePluginsStrings(): void {
|
|
|
|
this.unloadStrings(this.sitePluginsStrings);
|
|
|
|
this.sitePluginsStrings = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all current custom strings.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Custom strings.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2020-10-14 16:38:24 +02:00
|
|
|
getAllCustomStrings(): CoreLanguageObject {
|
2020-10-07 10:53:19 +02:00
|
|
|
return this.customStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all current site plugins strings.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Site plugins strings.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2020-10-14 16:38:24 +02:00
|
|
|
getAllSitePluginsStrings(): CoreLanguageObject {
|
2020-10-07 10:53:19 +02:00
|
|
|
return this.sitePluginsStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current language.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Promise resolved with the current language.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2023-07-25 10:42:08 +09:00
|
|
|
async getCurrentLanguage(format?: CoreLangFormat): Promise<string> {
|
|
|
|
if (this.currentLanguage === undefined) {
|
|
|
|
this.currentLanguage = await this.detectLanguage();
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 10:42:08 +09:00
|
|
|
return format ? this.formatLanguage(this.currentLanguage, format) : this.currentLanguage;
|
|
|
|
}
|
2020-10-14 23:30:15 +02:00
|
|
|
|
2023-07-25 10:42:08 +09:00
|
|
|
/**
|
|
|
|
* Update a language code to the given format.
|
|
|
|
*
|
|
|
|
* @param lang Language code.
|
|
|
|
* @param format Format to use.
|
|
|
|
* @returns Formatted language code.
|
|
|
|
*/
|
|
|
|
formatLanguage(lang: string, format: CoreLangFormat): string {
|
|
|
|
switch (format) {
|
|
|
|
case CoreLangFormat.App:
|
|
|
|
return lang.replace('_', '-');
|
|
|
|
case CoreLangFormat.LMS:
|
|
|
|
return lang.replace('-', '_');
|
|
|
|
}
|
2020-10-14 23:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current language from settings, or detect the browser one.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Promise resolved with the selected language.
|
2020-10-14 23:30:15 +02:00
|
|
|
*/
|
|
|
|
protected async detectLanguage(): Promise<string> {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Get current language from config (user might have changed it).
|
2020-10-14 23:30:15 +02:00
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
return await CoreConfig.get<string>('current_language');
|
2022-08-31 16:49:31 +02:00
|
|
|
} catch {
|
2020-10-14 23:30:15 +02:00
|
|
|
// Try will return, ignore errors here to avoid nesting.
|
|
|
|
}
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2020-10-14 23:30:15 +02:00
|
|
|
// User hasn't defined a language. If default language is forced, use it.
|
2020-10-21 17:56:01 +02:00
|
|
|
if (CoreConstants.CONFIG.default_lang && CoreConstants.CONFIG.forcedefaultlanguage) {
|
|
|
|
return CoreConstants.CONFIG.default_lang;
|
2020-10-14 23:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-12-16 10:46:40 +01:00
|
|
|
if (CoreConstants.CONFIG.languages && CoreConstants.CONFIG.languages[preferredLanguage] === undefined) {
|
2020-10-14 23:30:15 +02:00
|
|
|
// Code is NOT supported. Fallback to language without dash. E.g. 'en-US' would fallback to 'en'.
|
2022-01-19 14:52:40 +01:00
|
|
|
preferredLanguage = preferredLanguage.substring(0, preferredLanguage.indexOf('-'));
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
2020-10-14 23:30:15 +02:00
|
|
|
}
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2021-12-16 10:46:40 +01:00
|
|
|
if (CoreConstants.CONFIG.languages[preferredLanguage] === undefined) {
|
2020-10-14 23:30:15 +02:00
|
|
|
// Language not supported, use default language.
|
|
|
|
return this.defaultLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return preferredLanguage;
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default language.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Default language.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
|
|
|
getDefaultLanguage(): string {
|
|
|
|
return this.defaultLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the fallback language.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Fallback language.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
|
|
|
getFallbackLanguage(): string {
|
|
|
|
return this.fallbackLanguage;
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:29:17 +01:00
|
|
|
/**
|
|
|
|
* Get translated month names.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Translated month names.
|
2021-02-23 15:29:17 +01:00
|
|
|
*/
|
|
|
|
getMonthNames(): string[] {
|
2022-10-04 15:06:14 +02:00
|
|
|
return moment.months().map(month => this.capitalize(month));
|
2021-02-23 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get translated month short names.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Translated month short names.
|
2021-02-23 15:29:17 +01:00
|
|
|
*/
|
|
|
|
getMonthShortNames(): string[] {
|
2022-10-04 15:06:14 +02:00
|
|
|
return moment.monthsShort().map(month => this.capitalize(month));
|
2021-02-23 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get translated day names.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Translated day names.
|
2021-02-23 15:29:17 +01:00
|
|
|
*/
|
|
|
|
getDayNames(): string[] {
|
2022-10-04 15:06:14 +02:00
|
|
|
return moment.weekdays().map(weekDay => this.capitalize(weekDay));
|
2021-02-23 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get translated day short names.
|
|
|
|
*
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Translated day short names.
|
2021-02-23 15:29:17 +01:00
|
|
|
*/
|
|
|
|
getDayShortNames(): string[] {
|
2022-10-04 15:06:14 +02:00
|
|
|
return moment.weekdaysShort().map(weekDay => this.capitalize(weekDay));
|
2021-02-23 15:29:17 +01:00
|
|
|
}
|
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
/**
|
|
|
|
* Get the full list of translations for a certain language.
|
|
|
|
*
|
|
|
|
* @param lang The language to check.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Promise resolved when done.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2020-10-14 16:38:24 +02:00
|
|
|
getTranslationTable(lang: string): Promise<Record<string, unknown>> {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Create a promise to convert the observable into a promise.
|
|
|
|
return new Promise((resolve, reject): void => {
|
2023-11-07 16:16:51 +01:00
|
|
|
const observer = Translate.getTranslation(lang).subscribe({
|
|
|
|
next: (table) => {
|
|
|
|
resolve(table);
|
|
|
|
observer.unsubscribe();
|
|
|
|
},
|
|
|
|
error: (err) => {
|
|
|
|
reject(err);
|
|
|
|
observer.unsubscribe();
|
|
|
|
},
|
2020-10-07 10:53:19 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:29:56 +01:00
|
|
|
/**
|
|
|
|
* Loads custom strings obtained from site.
|
|
|
|
*
|
|
|
|
* @param currentSite Current site object.
|
|
|
|
*/
|
|
|
|
loadCustomStringsFromSite(currentSite: CoreSite): void {
|
|
|
|
const customStrings = currentSite.getStoredConfig('tool_mobile_customlangstrings');
|
|
|
|
|
2021-12-16 10:46:40 +01:00
|
|
|
if (customStrings !== undefined) {
|
2020-11-23 12:29:56 +01:00
|
|
|
this.loadCustomStrings(customStrings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
/**
|
|
|
|
* Load certain custom strings.
|
|
|
|
*
|
|
|
|
* @param strings Custom strings to load (tool_mobile_customlangstrings).
|
|
|
|
*/
|
|
|
|
loadCustomStrings(strings: string): void {
|
2023-11-17 13:54:42 +01:00
|
|
|
if (strings === this.customStringsRaw) {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Strings haven't changed, stop.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset current values.
|
|
|
|
this.clearCustomStrings();
|
|
|
|
|
|
|
|
if (!strings) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let currentLangChanged = false;
|
|
|
|
|
|
|
|
const list: string[] = strings.split(/(?:\r\n|\r|\n)/);
|
|
|
|
list.forEach((entry: string) => {
|
2023-06-12 13:40:18 +02:00
|
|
|
const values: string[] = entry.split('|').map(value => value.trim());
|
2020-10-07 10:53:19 +02:00
|
|
|
|
|
|
|
if (values.length < 3) {
|
|
|
|
// Not enough data, ignore the entry.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:54:42 +01:00
|
|
|
const lang = this.formatLanguage(values[2], CoreLangFormat.App); // Use the app format instead of Moodle format.
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2023-11-17 13:54:42 +01:00
|
|
|
if (lang === this.currentLanguage) {
|
2020-10-07 10:53:19 +02:00
|
|
|
currentLangChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.customStrings[lang]) {
|
|
|
|
this.customStrings[lang] = {};
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:54:42 +01:00
|
|
|
this.loadString(this.customStrings, lang, values[0], values[1]);
|
2020-10-07 10:53:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.customStringsRaw = strings;
|
|
|
|
|
2021-10-14 15:03:13 +02:00
|
|
|
if (currentLangChanged && this.currentLanguage) {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Some lang strings have changed, emit an event to update the pipes.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.onLangChange.emit({
|
2021-10-14 15:03:13 +02:00
|
|
|
lang: this.currentLanguage,
|
|
|
|
translations: Translate.translations[this.currentLanguage],
|
2020-10-07 10:53:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load custom strings for a certain language that weren't loaded because the language wasn't active.
|
|
|
|
*
|
|
|
|
* @param langObject The object with the strings to load.
|
|
|
|
* @param lang Language to load.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Whether the translation table was modified.
|
2020-10-07 10:53:19 +02:00
|
|
|
*/
|
2020-10-08 11:50:14 +02:00
|
|
|
loadLangStrings(langObject: CoreLanguageObject, lang: string): boolean {
|
2020-10-07 10:53:19 +02:00
|
|
|
let langApplied = false;
|
|
|
|
|
|
|
|
if (langObject[lang]) {
|
|
|
|
for (const key in langObject[lang]) {
|
|
|
|
const entry = langObject[lang][key];
|
|
|
|
|
|
|
|
if (!entry.applied) {
|
|
|
|
// Store the original value of the string.
|
2021-03-02 11:41:04 +01:00
|
|
|
entry.original = Translate.translations[lang][key];
|
2020-10-07 10:53:19 +02:00
|
|
|
|
|
|
|
// Store the string in the translations table.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.translations[lang][key] = entry.value;
|
2020-10-07 10:53:19 +02:00
|
|
|
|
|
|
|
entry.applied = true;
|
|
|
|
langApplied = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return langApplied;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a string in a certain lang object and in the translate table if the lang is loaded.
|
|
|
|
*
|
|
|
|
* @param langObject The object where to store the lang.
|
|
|
|
* @param lang Language code.
|
|
|
|
* @param key String key.
|
|
|
|
* @param value String value.
|
|
|
|
*/
|
2020-10-08 11:50:14 +02:00
|
|
|
loadString(langObject: CoreLanguageObject, lang: string, key: string, value: string): void {
|
2020-10-07 10:53:19 +02:00
|
|
|
lang = lang.replace(/_/g, '-'); // Use the app format instead of Moodle format.
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
if (Translate.translations[lang]) {
|
2020-10-07 10:53:19 +02:00
|
|
|
// The language is loaded.
|
|
|
|
// Store the original value of the string.
|
|
|
|
langObject[lang][key] = {
|
2021-03-02 11:41:04 +01:00
|
|
|
original: Translate.translations[lang][key],
|
2020-10-07 10:53:19 +02:00
|
|
|
value,
|
|
|
|
applied: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Store the string in the translations table.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.translations[lang][key] = value;
|
2020-10-07 10:53:19 +02:00
|
|
|
} else {
|
|
|
|
// The language isn't loaded.
|
|
|
|
// Save it in our object but not in the translations table, it will be loaded when the lang is loaded.
|
|
|
|
langObject[lang][key] = {
|
|
|
|
value,
|
|
|
|
applied: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 07:36:06 +02:00
|
|
|
/**
|
|
|
|
* Read a language file.
|
|
|
|
*
|
|
|
|
* @param lang Language code.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Promise resolved with the file contents.
|
2021-05-27 07:36:06 +02:00
|
|
|
*/
|
2021-06-01 12:34:51 +02:00
|
|
|
async readLangFile(lang: CoreLangLanguage): Promise<Record<string, string>> {
|
2021-05-27 07:36:06 +02:00
|
|
|
const observable = Http.get(`assets/lang/${lang}.json`, {
|
|
|
|
responseType: 'json',
|
|
|
|
});
|
|
|
|
|
2023-11-23 15:25:36 +01:00
|
|
|
return <Record<string, string>> await firstValueFrom(observable);
|
2021-05-27 07:36:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:22:41 +02:00
|
|
|
/**
|
|
|
|
* Filter a multilang string.
|
|
|
|
*
|
|
|
|
* @param text Multilang string.
|
|
|
|
* @returns Filtered string.
|
|
|
|
*/
|
2023-05-03 11:53:23 +02:00
|
|
|
async filterMultilang(text: string): Promise<string> {
|
|
|
|
return Promise.resolve(text)
|
2023-04-20 12:22:41 +02:00
|
|
|
.then(text => AddonFilterMultilangHandler.filter(text))
|
|
|
|
.then(text => AddonFilterMultilang2Handler.filter(text));
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
/**
|
|
|
|
* Unload custom or site plugin strings, removing them from the translations table.
|
|
|
|
*
|
|
|
|
* @param strings Strings to unload.
|
|
|
|
*/
|
2020-10-08 11:50:14 +02:00
|
|
|
protected unloadStrings(strings: CoreLanguageObject): void {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Iterate over all languages and strings.
|
|
|
|
for (const lang in strings) {
|
2021-03-02 11:41:04 +01:00
|
|
|
if (!Translate.translations[lang]) {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Language isn't loaded, nothing to unload.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const langStrings = strings[lang];
|
|
|
|
for (const key in langStrings) {
|
|
|
|
const entry = langStrings[key];
|
|
|
|
if (entry.original) {
|
|
|
|
// The string had a value, restore it.
|
2021-03-02 11:41:04 +01:00
|
|
|
Translate.translations[lang][key] = entry.original;
|
2020-10-07 10:53:19 +02:00
|
|
|
} else {
|
|
|
|
// The string didn't exist, delete it.
|
2021-03-02 11:41:04 +01:00
|
|
|
delete Translate.translations[lang][key];
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 11:50:14 +02:00
|
|
|
|
2023-04-26 10:20:18 +02:00
|
|
|
/**
|
|
|
|
* Reload language strings for the current language.
|
|
|
|
*/
|
|
|
|
protected async reloadLanguageStrings(): Promise<void> {
|
|
|
|
const currentLanguage = this.currentLanguage;
|
|
|
|
|
|
|
|
if (!currentLanguage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
CoreSubscriptions.once(Translate.use(currentLanguage), async data => {
|
|
|
|
// Check if it has a parent language.
|
|
|
|
const fallbackLang = this.getParentLanguage();
|
|
|
|
|
|
|
|
if (fallbackLang) {
|
|
|
|
try {
|
|
|
|
// Merge parent translations with the child ones.
|
|
|
|
const parentTranslations = Translate.translations[fallbackLang] ?? await this.readLangFile(fallbackLang);
|
|
|
|
|
|
|
|
const mergedData = Object.assign(parentTranslations, data);
|
|
|
|
|
|
|
|
Object.assign(data, mergedData);
|
|
|
|
} catch {
|
|
|
|
// Ignore errors.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
export const CoreLang = makeSingleton(CoreLangProvider);
|
2020-10-08 11:50:14 +02:00
|
|
|
|
2023-07-25 10:42:08 +09:00
|
|
|
export const enum CoreLangFormat {
|
|
|
|
LMS = 'lms',
|
|
|
|
App = 'app'
|
|
|
|
}
|
|
|
|
|
2021-06-01 12:34:51 +02:00
|
|
|
/**
|
|
|
|
* Language code. E.g. 'au', 'es', etc.
|
|
|
|
*/
|
|
|
|
export type CoreLangLanguage = string;
|
|
|
|
|
2020-10-08 11:50:14 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|