diff --git a/src/addons/filter/multilang/services/handlers/multilang.ts b/src/addons/filter/multilang/services/handlers/multilang.ts index af0014f74..657ba27b6 100644 --- a/src/addons/filter/multilang/services/handlers/multilang.ts +++ b/src/addons/filter/multilang/services/handlers/multilang.ts @@ -58,15 +58,13 @@ export class AddonFilterMultilangHandlerService extends CoreFilterDefaultHandler } // Find language to use. - let language: string | undefined = await CoreLang.getCurrentLanguage(); - - if (!languages.has(language)) { - language = CoreLang.getParentLanguage(); - } - - if (!language) { - language = firstLanguage; - } + const language = [ + await CoreLang.getCurrentLanguage(), + CoreLang.getParentLanguage(), + CoreLang.getFallbackLanguage(), + firstLanguage, + ] + .find(candidate => candidate && languages.has(candidate)); if (!language) { return text; diff --git a/src/core/services/tests/lang.test.ts b/src/core/services/tests/lang.test.ts index c82e06ce5..9cd8281d0 100644 --- a/src/core/services/tests/lang.test.ts +++ b/src/core/services/tests/lang.test.ts @@ -122,7 +122,7 @@ describe('Lang', () => { `, 'Japanese text'); }); - it('filters multilang text using unknown language', async () => { + it('filters multilang text using the fallback language', async () => { currentLanguage = 'ca'; parentLanguage = undefined; @@ -131,7 +131,7 @@ describe('Lang', () => { English Japanese text - `, 'Spanish text'); + `, 'English text'); await expectMultilangFilter(` {mlang es}Spanish{mlang} @@ -141,6 +141,23 @@ describe('Lang', () => { `, 'text'); }); + it('filters multilang text using the first language', async () => { + currentLanguage = 'ca'; + parentLanguage = undefined; + + await expectMultilangFilter(` + Spanish + Japanese + text + `, 'Spanish text'); + + await expectMultilangFilter(` + {mlang es}Spanish{mlang} + {mlang ja}Japanese{mlang} + text + `, 'text'); + }); + /** * Test multilang filter (normalizing whitespace). */