Skip to content

Commit

Permalink
Accept fuzzy matches for resource fallback language
Browse files Browse the repository at this point in the history
Change-Id: I847c712a77feb2ce8a6ed65538d2bb1a7d42a6d1
  • Loading branch information
tbuschto committed May 13, 2020
1 parent cc565bb commit 896c964
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions snippets/resources/texts.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "../node_modules/tabris/schema/texts.json",
"$fallbackLanguage": "en-us",
"$fallbackLanguage": "en-US",
"emphasisFont": {
"en-us": "Emphasis Font",
"en": "Emphasis Font",
"de": "Hervorgehobene Schrift"
},
"tintColor": {
Expand Down
17 changes: 7 additions & 10 deletions src/tabris/util-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function isLangSelector(value, fallback) {
if (langLikeKeys.length === 0) {
return false;
}
if (!value[fallback]) {
if (!value[fallback.toLowerCase()] && !value[fallback.toLowerCase().split('-')[0]]) {
throw new Error(
`Missing entry for fallback language (currently "${fallback}") `
+ 'in selector '
Expand Down Expand Up @@ -271,15 +271,12 @@ function getNearbyValue(selectable, approx, strategy) {
* @param {string} fallback
*/
function getLocalValue(source, language, fallback) {
const lowerLang = language.toLowerCase();
let bestMatch = fallback;
for (const key in source) {
const lowerKey = key.toLowerCase();
if ((lowerLang.indexOf(lowerKey) === 0) && (lowerKey.length >= bestMatch.length)) {
return bestMatch = source[key];
}
}
return source[bestMatch];
const map = {};
Object.keys(source).forEach(key => map[key.toLowerCase()] = source[key]);
return map[language.toLowerCase()]
|| map[language.toLowerCase().split('-')[0]]
|| map[fallback.toLowerCase()]
|| map[fallback.toLowerCase().split('-')[0]];
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/tabris/Resources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ describe('Resources', function() {
expect(create({foo: {'de-DE': 1, 'de-AT': 2, 'en': 3}}).foo).to.equal(1);
});

it('selects for more specific match', function() {
expect(create({foo: {'de-DE': 2, 'de': 1, 'en': 3}}).foo).to.equal(2);
expect(create({foo: {'de': 2, 'de-DE': 1, 'en': 3}}).foo).to.equal(1);
});

it('falls back to default (en)', function() {
expect(create({foo: {es: 1, aaq: 2, en: 3}}).foo).to.equal(3);
});
Expand All @@ -258,6 +263,16 @@ describe('Resources', function() {
expect(create({foo: {es: 1, aaq: 2, en: 3}}).foo).to.equal(1);
});

it('falls back to longer version of configured default', function() {
options.config = {fallbackLanguage: 'en-US'};
expect(create({foo: {'en': 1, 'en-US': 2, 'es': 3}}).foo).to.equal(2);
});

it('falls back to shorter version of configured default', function() {
options.config = {fallbackLanguage: 'en-us'};
expect(create({foo: {es: 1, en: 3}}).foo).to.equal(3);
});

it('selects case-insensitive', function() {
expect(create({foo: {en: 1, DE: 2, aaq: 3}}).foo).to.equal(2);
});
Expand Down

0 comments on commit 896c964

Please sign in to comment.