Skip to content

Commit

Permalink
Add retryInDefaultLocale option
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Feb 12, 2016
1 parent 0cd5d7f commit ee0da09
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
27 changes: 22 additions & 5 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ i18n.options = {
autoReload: false,
objectNotation: false,
fallbacks: {},
retryInDefaultLocale: false,
logDebugFn: debug,
logWarnFn: warn,
logErrorFn: error
Expand Down Expand Up @@ -85,6 +86,9 @@ i18n.configure = function i18nConfigure(opt) {
// setting defaultLocale
i18n.options.defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';

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

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

Expand Down Expand Up @@ -581,6 +585,7 @@ function translate(locale, singular, plural) {
read(locale);
}

// This allow pass default value as 'greeting.formal:Hello'
var defaultSingular = singular;
var defaultPlural = plural;
if (i18n.options.objectNotation) {
Expand All @@ -604,16 +609,28 @@ function translate(locale, singular, plural) {

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

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

Expand Down
1 change: 0 additions & 1 deletion test/i18n.objectnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ describe('Object Notation', function () {
});
});


});
73 changes: 73 additions & 0 deletions test/i18n.retryInDefaultLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */

// now with coverage suport
var i18n = require('../i18n'),
should = require("should"),
path = require("path");

describe('retryInDefaultLocale', function () {
beforeEach(function () {
i18n.removeLocale('nl');
i18n.configure({
locales: ['en', 'nl'],
directory: './locales',
register: global,
updateFiles: false,
objectNotation: true,
retryInDefaultLocale: true
});
});

describe('singular', function () {
var phrase = {phrase: 'greeting.formal', locale: 'nl'};

it('should return for return english translation', function () {
i18n.options.retryInDefaultLocale = false;
should.equal(i18n.__(phrase), 'greeting.formal');

// reload cache, becasue previous command already add new property
i18n.removeLocale('nl');

i18n.options.retryInDefaultLocale = true;
should.equal(i18n.__(phrase), 'Hello');
});

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

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

describe('plural', function () {
var phrase = {singular: "cat", plural: "cat", locale: "nl"};

it('should return for plural', function () {
i18n.options.retryInDefaultLocale = false;
should.equal(i18n.__n(phrase, 3), 'cat');

// reload cache, becasue previous command already add new property
i18n.removeLocale('nl');

i18n.options.retryInDefaultLocale = true;
should.equal(i18n.__n(phrase, 3), '3 cats');
});

it('should work multple times (not set wrong cache)', function () {
for (var i = 0; i <= 5; i += 1) {
should.equal(i18n.__n(phrase, 3), '3 cats', "Fail on " + i + " interation");
}
});

it('should set cache to work fast', function () {
should.equal(i18n.__n(phrase, 3), '3 cats');
should.deepEqual(i18n.getCatalog('nl').cat, {one: "%s cat", other: "%s cats"});
});
});

});

0 comments on commit ee0da09

Please sign in to comment.