Skip to content

Commit c160de9

Browse files
committed
MOBILE-4901 lang: Sort deprecated code and improve types
1 parent 858258e commit c160de9

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

src/core/features/siteplugins/services/siteplugins.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { CoreSite } from '@classes/sites/site';
1919
import { CoreCourseAnyModuleData } from '@features/course/services/course';
2020
import { CoreCourses } from '@features/courses/services/courses';
2121
import { CoreFilepool } from '@services/filepool';
22-
import { CoreLang, CoreLangFormat } from '@services/lang';
22+
import { CoreLang, CoreLangFormat, CoreLangTranslationByLanguage } from '@services/lang';
2323
import { CoreSites } from '@services/sites';
2424
import { CoreText } from '@singletons/text';
2525
import { CoreUtils } from '@singletons/utils';
@@ -812,7 +812,7 @@ export type CoreSitePluginsWSPlugin = {
812812
*/
813813
export type CoreSitePluginsPlugin = CoreSitePluginsWSPlugin & {
814814
parsedHandlers?: Record<string, CoreSitePluginsHandlerData> | null;
815-
parsedLang?: Record<string, string[]> | null;
815+
parsedLang?: CoreLangTranslationByLanguage | null;
816816
};
817817

818818
/**

src/core/services/lang.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ export class CoreLangProvider {
3737
protected defaultLanguage = CoreConstants.CONFIG.default_lang || 'en'; // Lang to use if device lang not valid or is forced.
3838
protected currentLanguage?: string; // Save current language in a variable to speed up the get function.
3939
protected customStringsRaw?: string;
40-
protected logger: CoreLogger;
41-
42-
constructor() {
43-
this.logger = CoreLogger.getInstance('CoreLang');
44-
}
40+
protected logger: CoreLogger = CoreLogger.getInstance('CoreLang');
4541

4642
async initialize(): Promise<void> {
4743
// Set fallback language and language to use until the app determines the right language to use.
@@ -78,12 +74,36 @@ export class CoreLangProvider {
7874
}
7975

8076
/**
81-
* Add a set of site plugins strings for a certain language.
77+
* Add a set of site plugins strings in a certain language.
78+
*
79+
* @param lang Language to add the strings to.
80+
* @param strings Strings to add.
81+
* @param prefix A prefix to add to all keys.
82+
* @deprecated since 5.2. Use the overload accepting langStrings object.
83+
*/
84+
async addSitePluginsStrings(lang: string, strings: CoreLangTranslationObject, prefix?: string): Promise<void>;
85+
/**
86+
* Add a set of site plugins strings.
8287
*
8388
* @param langStrings Object with the strings to add in every language.
8489
* @param prefix A prefix to add to all keys.
8590
*/
86-
async addSitePluginsStrings(langStrings: Record<string, string[]>, prefix?: string): Promise<void> {
91+
async addSitePluginsStrings(langStrings: CoreLangTranslationByLanguage, prefix?: string): Promise<void>;
92+
async addSitePluginsStrings(
93+
langStringsOrLang: string | CoreLangTranslationByLanguage,
94+
stringsOrPrefix?: CoreLangTranslationObject | string,
95+
prefix?: string,
96+
): Promise<void> {
97+
if (typeof langStringsOrLang === 'string') {
98+
const lang = langStringsOrLang;
99+
const strings = stringsOrPrefix as CoreLangTranslationObject;
100+
await this.addSitePluginsStrings({ [lang]: strings }, prefix);
101+
102+
return;
103+
}
104+
const langStrings = langStringsOrLang;
105+
prefix = stringsOrPrefix as string ?? '';
106+
87107
const loadedStrings: { [lang: string]: TranslationObject } = {};
88108

89109
for (let lang in langStrings) {
@@ -130,7 +150,7 @@ export class CoreLangProvider {
130150
* @returns Message if found, null otherwise.
131151
*/
132152
async getMessage(key: string, lang: string): Promise<string | undefined> {
133-
const messages = await this.getTranslationTable(lang);
153+
const messages = await this.getMessages(lang);
134154

135155
return messages[key] as string | undefined;
136156
}
@@ -143,7 +163,16 @@ export class CoreLangProvider {
143163
* @returns Messages.
144164
*/
145165
async getMessages(lang: string, keyPrefix = ''): Promise<TranslationObject> {
146-
const table = this.getTranslationTable(lang);
166+
// Create a promise to convert the observable into a promise.
167+
const promise = new Promise<TranslationObject>((resolve, reject): void => {
168+
CoreSubscriptions.once(
169+
Translate.currentLoader.getTranslation(lang),
170+
(table) => resolve(table),
171+
reject,
172+
);
173+
});
174+
175+
const table = await promise;
147176

148177
if (!keyPrefix) {
149178
return table;
@@ -277,7 +306,7 @@ export class CoreLangProvider {
277306
* @returns Custom strings.
278307
* @deprecated since 5.2. Not used anymore.
279308
*/
280-
getAllCustomStrings(): CoreLanguageObject {
309+
getAllCustomStrings(): unknown {
281310
return {};
282311
}
283312

@@ -287,7 +316,7 @@ export class CoreLangProvider {
287316
* @returns Site plugins strings.
288317
* @deprecated since 5.2. Not used anymore.
289318
*/
290-
getAllSitePluginsStrings(): CoreLanguageObject {
319+
getAllSitePluginsStrings(): unknown {
291320
return {};
292321
}
293322

@@ -428,16 +457,11 @@ export class CoreLangProvider {
428457
*
429458
* @param lang The language to check.
430459
* @returns Promise resolved when done.
460+
*
461+
* @deprecated since 5.2. Use getMessages instead.
431462
*/
432463
getTranslationTable(lang: string): Promise<TranslationObject> {
433-
// Create a promise to convert the observable into a promise.
434-
return new Promise((resolve, reject): void => {
435-
CoreSubscriptions.once(
436-
Translate.currentLoader.getTranslation(lang),
437-
(table) => resolve(table),
438-
reject,
439-
);
440-
});
464+
return this.getMessages(lang);
441465
}
442466

443467
/**
@@ -518,10 +542,10 @@ export class CoreLangProvider {
518542
*
519543
* @param lang Language code.
520544
* @returns Promise resolved with the file contents.
521-
* @deprecated since 5.0. Use getTranslationTable instead.
545+
* @deprecated since 5.0. Use getMessages instead.
522546
*/
523547
async readLangFile(lang: CoreLangLanguage): Promise<TranslationObject> {
524-
return this.getTranslationTable(lang);
548+
return this.getMessages(lang);
525549
}
526550

527551
/**
@@ -547,7 +571,8 @@ export class CoreLangProvider {
547571
* @returns Whether the translation table was modified.
548572
* @deprecated since 5.2. Not used anymore.
549573
*/
550-
async loadLangStrings(langObject: CoreLanguageObject, lang: string): Promise<boolean> {
574+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
575+
async loadLangStrings(langObject: unknown, lang: string): Promise<boolean> {
551576
return false;
552577
}
553578

@@ -565,13 +590,5 @@ export const enum CoreLangFormat {
565590
*/
566591
export type CoreLangLanguage = string;
567592

568-
/**
569-
* Language object has two leves, first per language and second per string key.
570-
*/
571-
type CoreLanguageObject = {
572-
[lang: string]: { // Lang name.
573-
[key: string]: { // String key.
574-
value: string; // Value with replacings done.
575-
};
576-
};
577-
};
593+
export type CoreLangTranslationObject = Record<string, string>;
594+
export type CoreLangTranslationByLanguage = { [lang: string]: CoreLangTranslationObject };

0 commit comments

Comments
 (0)