MOBILE-4288 multilang: Use fallback language

main
Noel De Martin 2023-05-09 15:50:24 +02:00
parent a0b4c8259d
commit 46f3da7d8f
2 changed files with 26 additions and 11 deletions

View File

@ -58,15 +58,13 @@ export class AddonFilterMultilangHandlerService extends CoreFilterDefaultHandler
} }
// Find language to use. // Find language to use.
let language: string | undefined = await CoreLang.getCurrentLanguage(); const language = [
await CoreLang.getCurrentLanguage(),
if (!languages.has(language)) { CoreLang.getParentLanguage(),
language = CoreLang.getParentLanguage(); CoreLang.getFallbackLanguage(),
} firstLanguage,
]
if (!language) { .find(candidate => candidate && languages.has(candidate));
language = firstLanguage;
}
if (!language) { if (!language) {
return text; return text;

View File

@ -122,7 +122,7 @@ describe('Lang', () => {
`, 'Japanese text'); `, 'Japanese text');
}); });
it('filters multilang text using unknown language', async () => { it('filters multilang text using the fallback language', async () => {
currentLanguage = 'ca'; currentLanguage = 'ca';
parentLanguage = undefined; parentLanguage = undefined;
@ -131,7 +131,7 @@ describe('Lang', () => {
<span class="multilang" lang="en">English</span> <span class="multilang" lang="en">English</span>
<span class="multilang" lang="ja">Japanese</span> <span class="multilang" lang="ja">Japanese</span>
text text
`, 'Spanish text'); `, 'English text');
await expectMultilangFilter(` await expectMultilangFilter(`
{mlang es}Spanish{mlang} {mlang es}Spanish{mlang}
@ -141,6 +141,23 @@ describe('Lang', () => {
`, 'text'); `, 'text');
}); });
it('filters multilang text using the first language', async () => {
currentLanguage = 'ca';
parentLanguage = undefined;
await expectMultilangFilter(`
<span class="multilang" lang="es">Spanish</span>
<span class="multilang" lang="ja">Japanese</span>
text
`, 'Spanish text');
await expectMultilangFilter(`
{mlang es}Spanish{mlang}
{mlang ja}Japanese{mlang}
text
`, 'text');
});
/** /**
* Test multilang filter (normalizing whitespace). * Test multilang filter (normalizing whitespace).
*/ */