diff --git a/snippets/resources/texts.json b/snippets/resources/texts.json index ae1357976..4d4f8482a 100644 --- a/snippets/resources/texts.json +++ b/snippets/resources/texts.json @@ -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": { diff --git a/src/tabris/util-resources.js b/src/tabris/util-resources.js index f3274e70e..2e7f1e21e 100644 --- a/src/tabris/util-resources.js +++ b/src/tabris/util-resources.js @@ -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 ' @@ -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]]; } /** diff --git a/test/tabris/Resources.test.js b/test/tabris/Resources.test.js index 7c47c67ed..5aedbfe56 100644 --- a/test/tabris/Resources.test.js +++ b/test/tabris/Resources.test.js @@ -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); }); @@ -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); });