MOBILE-4288 multilang: Fix unknown language filter

main
Noel De Martin 2023-05-03 12:25:31 +02:00
parent 335a758350
commit 1f588bba85
2 changed files with 25 additions and 0 deletions

View File

@ -47,10 +47,12 @@ export class AddonFilterMultilangHandlerService extends CoreFilterDefaultHandler
// Get available languages.
const regex = /<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>.*?<\/(?:lang|span)>/img;
const languages: Set<string> = new Set();
let firstLanguage: string | undefined;
let match: RegExpExecArray | null;
while ((match = regex.exec(text))) {
const language = match[1].toLowerCase().replace(/_/g, '-');
firstLanguage = firstLanguage ?? language;
languages.add(language);
}
@ -62,6 +64,10 @@ export class AddonFilterMultilangHandlerService extends CoreFilterDefaultHandler
language = CoreLang.getParentLanguage();
}
if (!language) {
language = firstLanguage;
}
if (!language) {
return text;
}

View File

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