Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/adriantanasa/i18n-node in…
Browse files Browse the repository at this point in the history
…to adriantanasa-master

# Conflicts:
#	.travis.yml
#	Makefile
#	appveyor.yml
#	package.json
  • Loading branch information
mashpie committed Aug 3, 2020
2 parents e10d212 + 6942515 commit 838f6cf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -138,8 +138,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
38 changes: 28 additions & 10 deletions i18n.js
Expand Up @@ -417,8 +417,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 @@ -513,8 +513,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 @@ -705,8 +705,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 @@ -718,8 +719,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 @@ -864,8 +866,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 @@ -1194,6 +1196,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 @@
{}
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 838f6cf

Please sign in to comment.