@@ -38,11 +38,7 @@ export class CoreLangProvider {
3838 protected defaultLanguage = CoreConstants . CONFIG . default_lang || 'en' ; // Lang to use if device lang not valid or is forced.
3939 protected currentLanguage ?: string ; // Save current language in a variable to speed up the get function.
4040 protected customStringsRaw ?: string ;
41- protected logger : CoreLogger ;
42-
43- constructor ( ) {
44- this . logger = CoreLogger . getInstance ( 'CoreLang' ) ;
45- }
41+ protected logger : CoreLogger = CoreLogger . getInstance ( 'CoreLang' ) ;
4642
4743 async initialize ( ) : Promise < void > {
4844 // Set fallback language and language to use until the app determines the right language to use.
@@ -79,21 +75,48 @@ export class CoreLangProvider {
7975 }
8076
8177 /**
82- * Add a set of site plugins strings for a certain language.
78+ * Add a set of site plugins strings in a certain language.
79+ *
80+ * @param lang Language to add the strings to.
81+ * @param strings Strings to add.
82+ * @param prefix A prefix to add to all keys.
83+ * @deprecated since 5.2. Use the overload accepting langStrings object.
84+ */
85+ async addSitePluginsStrings ( lang : string , strings : CoreLangTranslationObject , prefix ?: string ) : Promise < void > ;
86+ /**
87+ * Add a set of site plugins strings.
8388 *
8489 * @param langStrings Object with the strings to add in every language.
8590 * @param prefix A prefix to add to all keys.
8691 */
87- async addSitePluginsStrings ( langStrings : Record < string , string [ ] > , prefix ?: string ) : Promise < void > {
92+ async addSitePluginsStrings ( langStrings : CoreLangTranslationByLanguage , prefix ?: string ) : Promise < void > ;
93+ async addSitePluginsStrings (
94+ langStringsOrLang : string | CoreLangTranslationByLanguage ,
95+ stringsOrPrefix ?: CoreLangTranslationObject | string ,
96+ prefix ?: string ,
97+ ) : Promise < void > {
98+ if ( typeof langStringsOrLang === 'string' ) {
99+ const lang = langStringsOrLang ;
100+ const strings = stringsOrPrefix as CoreLangTranslationObject ;
101+ await this . addSitePluginsStrings ( { [ lang ] : strings } , prefix ) ;
102+
103+ return ;
104+ }
105+ const langStrings = langStringsOrLang ;
106+ prefix = ( stringsOrPrefix ?? '' ) as string ;
107+
88108 const loadedStrings : { [ lang : string ] : TranslationObject } = { } ;
89109
90- for ( let lang in langStrings ) {
110+ for ( let lang of Object . keys ( langStrings ) ) {
91111 lang = this . formatLanguage ( lang , CoreLangFormat . App ) ; // Use the app format instead of Moodle format.
92112
93113 const strings = langStrings [ lang ] ;
114+ if ( ! strings ) {
115+ continue ;
116+ }
94117
95118 loadedStrings [ lang ] = { } ;
96- for ( const key in strings ) {
119+ for ( const key of Object . keys ( strings ) ) {
97120 const prefixedKey = prefix + key ;
98121
99122 let value = strings [ key ] ;
@@ -131,7 +154,7 @@ export class CoreLangProvider {
131154 * @returns Message if found, null otherwise.
132155 */
133156 async getMessage ( key : string , lang : string ) : Promise < string | undefined > {
134- const messages = await this . getTranslationTable ( lang ) ;
157+ const messages = await this . getMessages ( lang ) ;
135158
136159 return messages [ key ] as string | undefined ;
137160 }
@@ -144,7 +167,16 @@ export class CoreLangProvider {
144167 * @returns Messages.
145168 */
146169 async getMessages ( lang : string , keyPrefix = '' ) : Promise < TranslationObject > {
147- const table = this . getTranslationTable ( lang ) ;
170+ // Create a promise to convert the observable into a promise.
171+ const promise = new Promise < TranslationObject > ( ( resolve , reject ) : void => {
172+ CoreSubscriptions . once (
173+ Translate . currentLoader . getTranslation ( lang ) ,
174+ ( table ) => resolve ( table ) ,
175+ reject ,
176+ ) ;
177+ } ) ;
178+
179+ const table = await promise ;
148180
149181 if ( ! keyPrefix ) {
150182 return table ;
@@ -280,7 +312,7 @@ export class CoreLangProvider {
280312 * @returns Custom strings.
281313 * @deprecated since 5.2. Not used anymore.
282314 */
283- getAllCustomStrings ( ) : CoreLanguageObject {
315+ getAllCustomStrings ( ) : unknown {
284316 return { } ;
285317 }
286318
@@ -290,7 +322,7 @@ export class CoreLangProvider {
290322 * @returns Site plugins strings.
291323 * @deprecated since 5.2. Not used anymore.
292324 */
293- getAllSitePluginsStrings ( ) : CoreLanguageObject {
325+ getAllSitePluginsStrings ( ) : unknown {
294326 return { } ;
295327 }
296328
@@ -469,16 +501,11 @@ export class CoreLangProvider {
469501 *
470502 * @param lang The language to check.
471503 * @returns Promise resolved when done.
504+ *
505+ * @deprecated since 5.2. Use getMessages instead.
472506 */
473507 getTranslationTable ( lang : string ) : Promise < TranslationObject > {
474- // Create a promise to convert the observable into a promise.
475- return new Promise ( ( resolve , reject ) : void => {
476- CoreSubscriptions . once (
477- Translate . currentLoader . getTranslation ( lang ) ,
478- ( table ) => resolve ( table ) ,
479- reject ,
480- ) ;
481- } ) ;
508+ return this . getMessages ( lang ) ;
482509 }
483510
484511 /**
@@ -558,10 +585,10 @@ export class CoreLangProvider {
558585 *
559586 * @param lang Language code.
560587 * @returns Promise resolved with the file contents.
561- * @deprecated since 5.0. Use getTranslationTable instead.
588+ * @deprecated since 5.0. Use getMessages instead.
562589 */
563590 async readLangFile ( lang : CoreLangLanguage ) : Promise < TranslationObject > {
564- return this . getTranslationTable ( lang ) ;
591+ return this . getMessages ( lang ) ;
565592 }
566593
567594 /**
@@ -587,7 +614,8 @@ export class CoreLangProvider {
587614 * @returns Whether the translation table was modified.
588615 * @deprecated since 5.2. Not used anymore.
589616 */
590- async loadLangStrings ( langObject : CoreLanguageObject , lang : string ) : Promise < boolean > {
617+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
618+ async loadLangStrings ( langObject : unknown , lang : string ) : Promise < boolean > {
591619 return false ;
592620 }
593621
@@ -605,13 +633,5 @@ export const enum CoreLangFormat {
605633 */
606634export type CoreLangLanguage = string ;
607635
608- /**
609- * Language object has two leves, first per language and second per string key.
610- */
611- type CoreLanguageObject = {
612- [ lang : string ] : { // Lang name.
613- [ key : string ] : { // String key.
614- value : string ; // Value with replacings done.
615- } ;
616- } ;
617- } ;
636+ export type CoreLangTranslationObject = Record < string , string > ;
637+ export type CoreLangTranslationByLanguage = { [ lang : string ] : CoreLangTranslationObject } ;
0 commit comments