From ee0da09cfe21c2f82f57913f94eacc2e2e4b7c15 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 12 Feb 2016 16:39:56 +0700 Subject: [PATCH] Add retryInDefaultLocale option --- i18n.js | 27 +++++++++--- test/i18n.objectnotation.js | 1 - test/i18n.retryInDefaultLocale.js | 73 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 test/i18n.retryInDefaultLocale.js diff --git a/i18n.js b/i18n.js index 02a34668..f92dffc0 100644 --- a/i18n.js +++ b/i18n.js @@ -44,6 +44,7 @@ i18n.options = { autoReload: false, objectNotation: false, fallbacks: {}, + retryInDefaultLocale: false, logDebugFn: debug, logWarnFn: warn, logErrorFn: error @@ -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; @@ -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) { @@ -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); } diff --git a/test/i18n.objectnotation.js b/test/i18n.objectnotation.js index 6ec9b12c..93f12407 100644 --- a/test/i18n.objectnotation.js +++ b/test/i18n.objectnotation.js @@ -57,5 +57,4 @@ describe('Object Notation', function () { }); }); - }); \ No newline at end of file diff --git a/test/i18n.retryInDefaultLocale.js b/test/i18n.retryInDefaultLocale.js new file mode 100644 index 00000000..b4a36018 --- /dev/null +++ b/test/i18n.retryInDefaultLocale.js @@ -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"}); + }); + }); + +});