Skip to content

Commit

Permalink
Merge 6942515 into d54e465
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantanasa committed Apr 13, 2018
2 parents d54e465 + 6942515 commit c9b4a24
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
@@ -1,10 +1,12 @@
language: node_js
node_js:
- 0.10
- 0.11
- 0.12
- 4
- 5
- 6
- 7
- 8
- iojs
branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ test:
mocha

cover:
istanbul cover ./node_modules/mocha/bin/_mocha
istanbul cover _mocha -- --recursive

hint:
jshint --verbose .
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -137,8 +137,8 @@ i18n.configure({
// setup some locales - other locales default to en silently
locales:['en', 'de'],

// fall back from Dutch to German
fallbacks:{'nl': 'de'},
// fall back from Dutch to German and from any localized German (de-at, de-li etc.) to German
fallbacks:{'nl': 'de', 'de-*': 'de'},

// you may alter a site wide default locale
defaultLocale: 'de',
Expand Down
3 changes: 3 additions & 0 deletions appveyor.yml
Expand Up @@ -4,6 +4,9 @@ environment:
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "iojs"
branches:
only:
Expand Down
38 changes: 28 additions & 10 deletions i18n.js
Expand Up @@ -402,8 +402,8 @@ module.exports = (function() {
}

// consider a fallback
if (!locales[targetLocale] && fallbacks[targetLocale]) {
targetLocale = fallbacks[targetLocale];
if (!locales[targetLocale]) {
targetLocale = getFallback(targetLocale, fallbacks) || targetLocale;
}

// now set locale on object
Expand Down Expand Up @@ -498,8 +498,8 @@ module.exports = (function() {
return locales;
}

if (!locales[targetLocale] && fallbacks[targetLocale]) {
targetLocale = fallbacks[targetLocale];
if (!locales[targetLocale]) {
targetLocale = getFallback(targetLocale, fallbacks) || targetLocale;
}

if (locales[targetLocale]) {
Expand Down Expand Up @@ -688,8 +688,9 @@ module.exports = (function() {
region = lr[1];

// Check if we have a configured fallback set for this language.
if (fallbacks && fallbacks[lang]) {
fallback = fallbacks[lang];
var fallbackLang = getFallback(lang, fallbacks);
if (fallbackLang) {
fallback = fallbackLang;
// Fallbacks for languages should be inserted
// where the original, unsupported language existed.
var acceptedLanguageIndex = acceptedLanguages.indexOf(lang);
Expand All @@ -701,8 +702,9 @@ module.exports = (function() {
}

// Check if we have a configured fallback set for the parent language of the locale.
if (fallbacks && fallbacks[parentLang]) {
fallback = fallbacks[parentLang];
var fallbackParentLang = getFallback(parentLang, fallbacks);
if (fallbackParentLang) {
fallback = fallbackParentLang;
// Fallbacks for a parent language should be inserted
// to the end of the list, so they're only picked
// if there is no better match.
Expand Down Expand Up @@ -842,8 +844,8 @@ module.exports = (function() {
locale = defaultLocale;
}

if (!locales[locale] && fallbacks[locale]) {
locale = fallbacks[locale];
if (!locales[locale]) {
locale = getFallback(locale, fallbacks) || locale;
}

// attempt to read when defined as valid locale
Expand Down Expand Up @@ -1165,6 +1167,22 @@ module.exports = (function() {
return filepath;
};

/**
* Get locales with wildcard support
*/
var getFallback = function(targetLocale, fallbacks) {
fallbacks = fallbacks || {};
if (fallbacks[targetLocale]) return fallbacks[targetLocale];
var fallBackLocale = null;
for (var key in fallbacks) {
if(targetLocale.match(new RegExp('^' + key.replace('*', '.*') + '$'))) {
fallBackLocale = fallbacks[key];
break;
}
}
return fallBackLocale;
};

/**
* Logging proxies
*/
Expand Down
1 change: 1 addition & 0 deletions locales/fr-CA.json
@@ -0,0 +1 @@
{}
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -19,9 +19,10 @@
},
"dependencies": {
"debug": "*",
"make-plural": "^3.0.3",
"js-yaml": "^3.10.0",
"make-plural": "^4",
"math-interval-parser": "^1.1.0",
"messageformat": "^0.3.1",
"messageformat": "^0",
"mustache": "*",
"sprintf-js": ">=1.0.3"
},
Expand All @@ -30,9 +31,9 @@
"cookie-parser": "^1.4.1",
"express": "^4.13.4",
"jshint": "*",
"mocha": "*",
"mocha": "^3.5.3",
"should": "*",
"sinon": "*",
"sinon": "^3.3.0",
"url": "^0.11.0",
"zombie": "*"
},
Expand Down
2 changes: 1 addition & 1 deletion test/i18n.configureLocales.js
Expand Up @@ -12,7 +12,7 @@ describe('locales configuration', function() {
directory: directory
});

var expected = ['de', 'de-AT', 'de-DE', 'en', 'en-GB', 'en-US', 'fr', 'nl', 'ru', 'tr-TR'].sort();
var expected = ['de', 'de-AT', 'de-DE', 'en', 'en-GB', 'en-US', 'fr', 'fr-CA', 'nl', 'ru', 'tr-TR'].sort();
should.deepEqual(i18n.getLocales(), expected);

done();
Expand Down
23 changes: 19 additions & 4 deletions test/i18n.fallbacks.js
Expand Up @@ -15,9 +15,9 @@ describe('Fallbacks', function() {
beforeEach(function() {

i18n.configure({
locales: ['en', 'de'],
locales: ['en', 'de', 'fr'],
defaultLocale: 'en',
fallbacks: { 'foo': 'de', 'de-AT': 'de' },
fallbacks: { 'foo': 'de', 'de-AT': 'de', 'fr-*': 'fr' },
directory: './locales',
register: req
});
Expand Down Expand Up @@ -45,11 +45,21 @@ describe('Fallbacks', function() {
i18n.init(req);
i18n.getLocale(req).should.equal('de');
});
it('should fall back to "fr" for locale "fr-CA"', function() {
req.headers['accept-language'] = 'fr-CA';
i18n.init(req);
i18n.getLocale(req).should.equal('fr');
});
it('should fall back to "de" for second-order locale "de-AT"', function() {
req.headers['accept-language'] = 'de-DK,de-AT';
i18n.init(req);
i18n.getLocale(req).should.equal('de');
});
it('should fall back to "fr" for second-order locale "fr-CA"', function() {
req.headers['accept-language'] = 'de-DK,fr-CA,de-AT';
i18n.init(req);
i18n.getLocale(req).should.equal('fr');
});
it('should use default "en" for valid locale request (ignoring fallbacks)', function() {
req.headers['accept-language'] = 'en-US,en,de-DK,de-AT';
i18n.init(req);
Expand Down Expand Up @@ -100,9 +110,9 @@ describe('Fallbacks', function() {
i18n = require(i18nFilename);

i18n.configure({
locales: ['en-US', 'de-DE'],
locales: ['en-US', 'de-DE', 'fr-CA'],
defaultLocale: 'en-US',
fallbacks: { 'de': 'de-DE' },
fallbacks: { 'de': 'de-DE', 'fr*': 'fr-CA' },
directory: './locales',
register: req
});
Expand All @@ -124,6 +134,11 @@ describe('Fallbacks', function() {
i18n.init(req);
i18n.getLocale(req).should.equal('de-DE');
});
it('should fall back to "fr-CA" for language "fr"', function() {
req.headers['accept-language'] = 'fr';
i18n.init(req);
i18n.getLocale(req).should.equal('fr-CA');
});
});

describe('Keep valid locale', function() {
Expand Down

0 comments on commit c9b4a24

Please sign in to comment.