Skip to content

Commit

Permalink
added retryInDefaultLocale as proposed by PR #206
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Aug 19, 2020
1 parent cd18b23 commit 818d0d8
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -4,7 +4,11 @@ test.examples:
mocha --exit examples/node-http/test.js
mocha --exit examples/node-http-autoreload/test.js

test:
clean:
rm -rf ./localestowrite
rm -rf ./localesmakeplural

test: clean
npm run test

cover:
Expand Down
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -225,7 +225,10 @@ i18n.configure({
fallbacks:{'nl': 'de', 'de-*': 'de'},

// you may alter a site wide default locale
defaultLocale: 'de',
defaultLocale: 'en',

// will return translation from defaultLocale in case current locale doesn't provide it
retryInDefaultLocale: false,

// sets a custom cookie name to parse locale settings from - defaults to NULL
cookie: 'yourcookiename',
Expand Down
44 changes: 38 additions & 6 deletions i18n.js
Expand Up @@ -53,6 +53,7 @@ const i18n = function I18n(_OPTS = false) {
autoReload,
cookiename,
defaultLocale,
retryInDefaultLocale,
directory,
directoryPermissions,
extension,
Expand Down Expand Up @@ -139,6 +140,9 @@ const i18n = function I18n(_OPTS = false) {
// setting defaultLocale
defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';

// allow to retry in default locale, useful for production
retryInDefaultLocale = (typeof opt.retryInDefaultLocale === 'boolean') ? opt.retryInDefaultLocale : false;

// auto reload locale files when changed
autoReload = (typeof opt.autoReload === 'boolean') ? opt.autoReload : false;

Expand Down Expand Up @@ -901,6 +905,7 @@ const i18n = function I18n(_OPTS = false) {
locale = defaultLocale;
}

// try to get a fallback
if (!locales[locale]) {
locale = getFallback(locale, fallbacks) || locale;
}
Expand Down Expand Up @@ -944,18 +949,45 @@ const i18n = function I18n(_OPTS = false) {
var accessor = localeAccessor(locale, singular);
var mutator = localeMutator(locale, singular);

// if (plural) {
// if (accessor() == null) {
// mutator({
// 'one': defaultSingular || singular,
// 'other': defaultPlural || plural
// });
// write(locale);
// }
// }

// if (accessor() == null) {
// mutator(defaultSingular || singular);
// write(locale);
// }

if (plural) {
if (accessor() == null) {
mutator({
'one': defaultSingular || singular,
'other': defaultPlural || plural
});
// when retryInDefaultLocale is true - try to set default value from defaultLocale
if (retryInDefaultLocale && locale !== defaultLocale) {
logDebug('Missing ' + singular + ' in ' + locale + ' retrying in ' + defaultLocale);
mutator(translate(defaultLocale, singular, plural));
} else {
mutator({
'one': defaultSingular || singular,
'other': defaultPlural || plural
});
}
write(locale);
}
}

if (accessor() == null) {
mutator(defaultSingular || singular);
// when retryInDefaultLocale is true - try to set default value from defaultLocale
if (retryInDefaultLocale && locale !== defaultLocale) {
logDebug('Missing ' + singular + ' in ' + locale + ' retrying in ' + defaultLocale);
mutator(translate(defaultLocale, singular, plural));
} else {
mutator(defaultSingular || singular);
}
write(locale);
}

Expand Down
158 changes: 158 additions & 0 deletions test/i18n.retryInDefaultLocale.js
@@ -0,0 +1,158 @@
/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */

// now with coverage suport
const { I18n } = require('..');
var should = require("should"),
path = require("path");

describe('retryInDefaultLocale', function () {

const i18nWithDefault = new I18n({
locales: ['en', 'nl'],
defaultLocale: 'en',
directory: './locales',
updateFiles: false,
objectNotation: true,
retryInDefaultLocale: true
})

const i18nNoDefault = new I18n({
locales: ['en', 'nl'],
defaultLocale: 'en',
directory: './locales',
updateFiles: false,
objectNotation: true,
retryInDefaultLocale: false
})

describe('singular with retryInDefaultLocale enabled', function () {
it('should use translations from defaultLocale for missing key', function () {
i18nWithDefault.setLocale('nl')
should.equal(i18nWithDefault.getLocale(), 'nl');
should.equal(i18nWithDefault.__('greeting.formal'), 'Hello');

i18nWithDefault.setLocale('en')
should.equal(i18nWithDefault.getLocale(), 'en');
should.equal(i18nWithDefault.__('greeting.formal'), 'Hello');
});

it('should default "en" when locale is set to unconfigured value', function () {
i18nWithDefault.setLocale('sv')
should.equal(i18nWithDefault.getLocale(), 'en');
should.equal(i18nWithDefault.__('greeting.formal'), 'Hello');
});

it('should work multple times (not set wrong cache)', function () {
i18nWithDefault.setLocale('nl')
for (var i = 0; i <= 5; i += 1) {
should.equal(i18nWithDefault.__('greeting.formal'), 'Hello', "Fail on " + i + " interation");
}
});

it('should set cache to work fast', function () {
i18nWithDefault.setLocale('nl')
i18nWithDefault.__('greeting.formal');
should.equal(i18nWithDefault.getCatalog('nl').greeting.formal, 'Hello');
});
});

describe('singular without retryInDefaultLocale', function () {
it('should english translation for missing key', function () {
i18nNoDefault.setLocale('nl')
should.equal(i18nNoDefault.getLocale(), 'nl');
should.equal(i18nNoDefault.__('greeting.formal'), 'greeting.formal');

i18nNoDefault.setLocale('en')
should.equal(i18nNoDefault.getLocale(), 'en');
should.equal(i18nNoDefault.__('greeting.formal'), 'Hello');
});

it('should default "en" when locale is set to unconfigured value', function () {
i18nNoDefault.setLocale('sv')
should.equal(i18nNoDefault.getLocale(), 'en');
should.equal(i18nNoDefault.__('greeting.formal'), 'Hello');
});

it('should work multple times (not set wrong cache)', function () {
i18nNoDefault.setLocale('nl')
for (var i = 0; i <= 5; i += 1) {
should.equal(i18nNoDefault.__('greeting.formal'), 'greeting.formal', "Fail on " + i + " interation");
}
});

it('should set cache to work fast', function () {
i18nNoDefault.setLocale('nl')
i18nNoDefault.__('greeting.formal');
should.equal(i18nNoDefault.getCatalog('nl').greeting.formal, 'greeting.formal');
});
});

describe('plural with retryInDefaultLocale enabled', function () {
it('should use translations from defaultLocale for missing key', function () {
i18nWithDefault.setLocale('nl')
should.equal(i18nWithDefault.getLocale(), 'nl');
should.equal(i18nWithDefault.__n('%s star', 1), '1 star');
should.equal(i18nWithDefault.__n('%s star', 3), '3 stars');

i18nWithDefault.setLocale('en')
should.equal(i18nWithDefault.getLocale(), 'en');
should.equal(i18nWithDefault.__n('%s star', 1), '1 star');
should.equal(i18nWithDefault.__n('%s star', 3), '3 stars');
});

it('should default "en" when locale is set to unconfigured value', function () {
i18nWithDefault.setLocale('sv')
should.equal(i18nWithDefault.getLocale(), 'en');
should.equal(i18nWithDefault.__n('%s star', 1), '1 star');
should.equal(i18nWithDefault.__n('%s star', 3), '3 stars');
});

it('should work multple times (not set wrong cache)', function () {
i18nWithDefault.setLocale('nl')
for (var i = 0; i <= 5; i += 1) {
should.equal(i18nWithDefault.__n('%s star', 3), '3 stars', "Fail on " + i + " interation");
}
});

it('should set cache to work fast', function () {
i18nWithDefault.setLocale('nl')
should.equal(i18nWithDefault.__n('%s star', 3), '3 stars');
should.deepEqual(i18nWithDefault.getCatalog('nl')['%s star'], {one: "%s star", other: "%s stars"});
});
});

describe('plural without retryInDefaultLocale enabled', function () {
it('should use translations from defaultLocale for missing key', function () {
i18nNoDefault.setLocale('nl')
should.equal(i18nNoDefault.getLocale(), 'nl');
should.equal(i18nNoDefault.__n('%s star', 1), '1 star');
should.equal(i18nNoDefault.__n('%s star', 3), '3 star');

i18nNoDefault.setLocale('en')
should.equal(i18nNoDefault.getLocale(), 'en');
should.equal(i18nNoDefault.__n('%s star', 1), '1 star');
should.equal(i18nNoDefault.__n('%s star', 3), '3 stars');
});

it('should default "en" when locale is set to unconfigured value', function () {
i18nNoDefault.setLocale('sv')
should.equal(i18nNoDefault.getLocale(), 'en');
should.equal(i18nNoDefault.__n('%s star', 1), '1 star');
should.equal(i18nNoDefault.__n('%s star', 3), '3 stars');
});

it('should work multple times (not set wrong cache)', function () {
i18nNoDefault.setLocale('nl')
for (var i = 0; i <= 5; i += 1) {
should.equal(i18nNoDefault.__n('%s star', 3), '3 star', "Fail on " + i + " interation");
}
});

it('should set cache to work fast', function () {
i18nNoDefault.setLocale('nl')
should.equal(i18nNoDefault.__n('%s star', 3), '3 star');
should.deepEqual(i18nNoDefault.getCatalog('nl')['%s star'], {one: "%s star", other: "%s star"});
});
});

});

0 comments on commit 818d0d8

Please sign in to comment.