Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

adding normalizeLanguage api #75

Merged
merged 1 commit into from May 13, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/i18n.js
Expand Up @@ -366,6 +366,15 @@ exports.languageFrom = languageFrom = function(locale) {
}
};

/**
* Given a language code, return a normalized language code.
* @param {String} language A language code. For example, 'en-us'.
* @return {String} A normalized language code, such as 'en-US'.
*/
exports.normalizeLanguage = normalizeLanguage = function (language) {
return languageFrom(localeFrom(language));
};

/**
* Given a locale code, return a normalized locale code.
* @param {String} locale A locale code. For example, 'en_us'.
Expand Down
59 changes: 59 additions & 0 deletions tests/i18n-tests.js
Expand Up @@ -43,6 +43,65 @@ suite.addBatch({
}
});

suite.addBatch({
"a lowercase language code": {
topic: function () {
return i18n.normalizeLanguage("en");
},
"is normalized to itself": function (err, str) {
assert.equal(str, "en");
}
},
"an uppercase language code": {
topic: function () {
return i18n.normalizeLanguage("EN");
},
"is normalized to lowercase": function (err, str) {
assert.equal(str, "en");
}
},
"a two part langauge code": {
topic: function () {
return i18n.normalizeLanguage("en-us");
},
"is normalized to 'xx-XX' format": function (err, str) {
assert.equal(str, "en-US");
}
},
"a three char language code": {
topic: function () {
return i18n.normalizeLanguage("HaW");
},
"returns the same three char code in lowercase": function (err, str) {
assert.equal(str, "haw");
}
},
"a three char language code with two parts": {
topic: function () {
return i18n.normalizeLanguage("SoN-Ml");
},
"is normalized to 'xxx-XX' format": function (err, str) {
assert.equal(str, "son-ML");
}
},
"A language with three parts": {
topic: function () {
return i18n.normalizeLanguage("ja-JP-mac");
},
"ignores the second part and returns a locale in 'xx-Xxx' format": function (err, str) {
assert.equal(str, "ja-Mac");
}
},
"A language with an underscore": {
topic: function () {
return i18n.normalizeLanguage("sR_LatN");
},
"is normalized to a hyphen in correct 'xx-XXXX' format": function (err, str) {
assert.equal(str, "sr-Latn");
}
}
});

suite.addBatch({
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice symmetry!

"a lowercase locale code": {
topic: function () {
Expand Down