Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Handle deprecated locales #5

Merged
merged 4 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var deprecatedLocales = require('./lib/deprecated-locales.json');

function bestMatchingLocale (inputLocale, availableLocales) {

/// Normalize
Expand All @@ -6,7 +8,7 @@ function bestMatchingLocale (inputLocale, availableLocales) {
.toLowerCase();

availableLocales = availableLocales.map(function(l) {
return l.toLowerCase();
return getStandardLanguage(l.toLowerCase());
});

var localeCodes = parseLocaleIntoCodes(inputLocale);
Expand Down Expand Up @@ -65,10 +67,10 @@ function parseLocaleIntoCodes (locale) {
if (!match) {
return null;
}

var localeParts = [];
if (match[1]) {
match[1] = match[1].toLowerCase();
match[1] = getStandardLanguage(match[1].toLowerCase());
localeParts.push(match[1]);
}
if (match[2]) {
Expand All @@ -88,6 +90,14 @@ function parseLocaleIntoCodes (locale) {
};
}

function getStandardLanguage(language) {
if (deprecatedLocales[language]) {
return deprecatedLocales[language]
} else {
return language;
}
}

function includes(inArray, toFind) {
return inArray.indexOf(toFind) > -1;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/deprecated-locales.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"iw": "he",
"ji": "yi",
"in": "id"
}
16 changes: 13 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ var availableLocales = [
'es-ES',
'pt-PT',
'tr',
'zh-Hans'
'zh-Hans',
'iw',
'ji',
'in'
];

tape('test bestMatchingLocale', function(t) {
Expand All @@ -22,6 +25,13 @@ 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test case for a deprecated language plus a country code, such as iw-IL.

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');

t.throws(function () {
locale.bestMatchingLocale('en', ['foobar']);
}, /foobar/);
Expand Down Expand Up @@ -56,8 +66,8 @@ tape('test parseLocaleIntoCodes', function(t) {
script: 'Hans',
region: 'HK'
});

t.equal(locale.parseLocaleIntoCodes('foobar'), null);

t.end();
});