From f04e1bb3fa2ba82bad216481b66ac4ac8fa24e2c Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Mon, 30 Apr 2018 17:12:57 -0700 Subject: [PATCH 1/4] Handle deprecated locales --- index.js | 14 ++++++++++++-- test.js | 14 +++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 2809fe0..1efa3d6 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +var deprecatedLocales = require('./lib/deprecated-locales'); + function bestMatchingLocale (inputLocale, availableLocales) { /// Normalize @@ -65,11 +67,11 @@ function parseLocaleIntoCodes (locale) { if (!match) { return null; } - + var localeParts = []; if (match[1]) { match[1] = match[1].toLowerCase(); - localeParts.push(match[1]); + localeParts.push(handleDeprecatedLocales(match[1])); } if (match[2]) { match[2] = match[2][0].toUpperCase() + match[2].substring(1).toLowerCase(); @@ -88,6 +90,14 @@ function parseLocaleIntoCodes (locale) { }; } +function handleDeprecatedLocales(language) { + var filtered = deprecatedLocales.filter(function(locale) { + return locale.previously === language; + }); + if (filtered.length === 0) return language; + return filtered[0].current; +} + function includes(inArray, toFind) { return inArray.indexOf(toFind) > -1; } diff --git a/test.js b/test.js index 35216cd..cb5c958 100644 --- a/test.js +++ b/test.js @@ -8,7 +8,10 @@ var availableLocales = [ 'es-ES', 'pt-PT', 'tr', - 'zh-Hans' + 'zh-Hans', + 'iw', + 'ji', + 'in' ]; tape('test bestMatchingLocale', function(t) { @@ -22,6 +25,11 @@ tape('test bestMatchingLocale', function(t) { t.equal(locale.bestMatchingLocale('es-mx', availableLocales), 'es'); t.equal(locale.bestMatchingLocale('zh-Hans-region', availableLocales), 'zh-Hans'); t.equal(locale.bestMatchingLocale('pt-BR', availableLocales), 'pt-PT'); + + t.equal(locale.bestMatchingLocale('iw', availableLocales), 'he', 'Test deprecated locale'); + t.equal(locale.bestMatchingLocale('ji', availableLocales), 'yi', 'Test deprecated locale'); + t.equal(locale.bestMatchingLocale('in', availableLocales), 'id', 'Test deprecated locale'); + t.throws(function () { locale.bestMatchingLocale('en', ['foobar']); }, /foobar/); @@ -56,8 +64,8 @@ tape('test parseLocaleIntoCodes', function(t) { script: 'Hans', region: 'HK' }); - + t.equal(locale.parseLocaleIntoCodes('foobar'), null); - + t.end(); }); From d57f6afd840d09b951f22dc01476f86790057aac Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Mon, 30 Apr 2018 17:14:00 -0700 Subject: [PATCH 2/4] add --- lib/deprecated-locales.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/deprecated-locales.js diff --git a/lib/deprecated-locales.js b/lib/deprecated-locales.js new file mode 100644 index 0000000..487a6a0 --- /dev/null +++ b/lib/deprecated-locales.js @@ -0,0 +1,10 @@ +module.exports = [{ + previously: 'iw', + current: 'he' +}, { + previously: 'ji', + current: 'yi' +}, { + previously: 'in', + current: 'id' +}]; From 3eeac166e0ef9eeaec42a8fbc73f2dd050e2ac14 Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Mon, 30 Apr 2018 17:44:33 -0700 Subject: [PATCH 3/4] use dict, add new test --- index.js | 20 ++++++++++---------- lib/deprecated-locales.js | 10 ---------- lib/deprecated-locales.json | 5 +++++ test.js | 1 + 4 files changed, 16 insertions(+), 20 deletions(-) delete mode 100644 lib/deprecated-locales.js create mode 100644 lib/deprecated-locales.json diff --git a/index.js b/index.js index 1efa3d6..e321a17 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var deprecatedLocales = require('./lib/deprecated-locales'); +var deprecatedLocales = require('./lib/deprecated-locales.json'); function bestMatchingLocale (inputLocale, availableLocales) { @@ -8,7 +8,7 @@ function bestMatchingLocale (inputLocale, availableLocales) { .toLowerCase(); availableLocales = availableLocales.map(function(l) { - return l.toLowerCase(); + return getStandardLanguage(l.toLowerCase()); }); var localeCodes = parseLocaleIntoCodes(inputLocale); @@ -70,8 +70,8 @@ function parseLocaleIntoCodes (locale) { var localeParts = []; if (match[1]) { - match[1] = match[1].toLowerCase(); - localeParts.push(handleDeprecatedLocales(match[1])); + match[1] = getStandardLanguage(match[1].toLowerCase()); + localeParts.push(match[1]); } if (match[2]) { match[2] = match[2][0].toUpperCase() + match[2].substring(1).toLowerCase(); @@ -90,12 +90,12 @@ function parseLocaleIntoCodes (locale) { }; } -function handleDeprecatedLocales(language) { - var filtered = deprecatedLocales.filter(function(locale) { - return locale.previously === language; - }); - if (filtered.length === 0) return language; - return filtered[0].current; +function getStandardLanguage(language) { + if (deprecatedLocales[language]) { + return deprecatedLocales[language] + } else { + return language; + } } function includes(inArray, toFind) { diff --git a/lib/deprecated-locales.js b/lib/deprecated-locales.js deleted file mode 100644 index 487a6a0..0000000 --- a/lib/deprecated-locales.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = [{ - previously: 'iw', - current: 'he' -}, { - previously: 'ji', - current: 'yi' -}, { - previously: 'in', - current: 'id' -}]; diff --git a/lib/deprecated-locales.json b/lib/deprecated-locales.json new file mode 100644 index 0000000..204213d --- /dev/null +++ b/lib/deprecated-locales.json @@ -0,0 +1,5 @@ +{ + "iw": "he", + "ji": "yi", + "in": "id" +} diff --git a/test.js b/test.js index cb5c958..2d15829 100644 --- a/test.js +++ b/test.js @@ -27,6 +27,7 @@ tape('test bestMatchingLocale', function(t) { t.equal(locale.bestMatchingLocale('pt-BR', availableLocales), 'pt-PT'); t.equal(locale.bestMatchingLocale('iw', availableLocales), 'he', 'Test deprecated locale'); + t.equal(locale.bestMatchingLocale('iw-IL', availableLocales), 'he', 'Test deprecated locale'); t.equal(locale.bestMatchingLocale('ji', availableLocales), 'yi', 'Test deprecated locale'); t.equal(locale.bestMatchingLocale('in', availableLocales), 'id', 'Test deprecated locale'); From a3329e0e47ed0fbcfee20feccafca1f1f39ecb15 Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Tue, 1 May 2018 08:25:15 -0700 Subject: [PATCH 4/4] add one more test --- test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test.js b/test.js index 2d15829..9f8aa77 100644 --- a/test.js +++ b/test.js @@ -28,6 +28,7 @@ tape('test bestMatchingLocale', function(t) { t.equal(locale.bestMatchingLocale('iw', availableLocales), 'he', 'Test deprecated locale'); t.equal(locale.bestMatchingLocale('iw-IL', availableLocales), 'he', 'Test deprecated locale'); + t.equal(locale.bestMatchingLocale('iw', ['iw-IL']), 'he-IL', 'Test deprecated locale'); t.equal(locale.bestMatchingLocale('ji', availableLocales), 'yi', 'Test deprecated locale'); t.equal(locale.bestMatchingLocale('in', availableLocales), 'id', 'Test deprecated locale');