Skip to content

Commit

Permalink
fix(localization): fallback for language codes with countries (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 authored Oct 22, 2020
1 parent 84bde79 commit e6b770c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/LibraryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,21 @@ export default class LibraryManager {
`language/${language}.json`
);
return streamToString(stream);
} catch (ignored) {
} catch {
log.debug(
`language '${language}' not found for ${LibraryName.toUberName(
library
)}`
);
const languageCodeMatch = /^([a-zA-Z]+)\-([a-zA-Z]+)$/.exec(
language
);
if (languageCodeMatch && languageCodeMatch.length === 3) {
log.debug(
`Language code ${language} seems to contain country code. Trying without it.`
);
return this.getLanguage(library, languageCodeMatch[1]);
}
return null;
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/LibraryManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,44 @@ describe('basic file library manager functionality', () => {
{ keep: false, unsafeCleanup: true }
);
});

it('returns null file for language en', async () => {
const libManager = new LibraryManager(
new FileLibraryStorage(`${path.resolve('')}/test/data/libraries`)
);

const language = await libManager.getLanguage(
{ machineName: 'H5P.Example1', majorVersion: 1, minorVersion: 1 },
'en'
);
expect(language).toBeNull();
});

it('returns the de language file for de-DE', async () => {
const libManager = new LibraryManager(
new FileLibraryStorage(`${path.resolve('')}/test/data/libraries`)
);

const language = await libManager.getLanguage(
{ machineName: 'H5P.Example1', majorVersion: 1, minorVersion: 1 },
'de-DE'
);
expect(JSON.parse(language)).toMatchObject({
semantics: [
{
label: 'Grußbotschaft',
default: 'Hallo, Welt!',
description:
'Die Grußbotschaft, die dem Nutzer gezeigt wird.'
},
{
label: 'Kartenbild',
description:
'Bild, das optional auf der Karte gezeigt wird. Ohne ein solches wird nur Text auf der Karte gezeigt.'
}
]
});
});
});
describe('listLanguages()', () => {
it('returns an empty array if the language folder does not exist', async () => {
Expand Down

0 comments on commit e6b770c

Please sign in to comment.