From e77d4f35783c9cdc87849cc985da7efaa2d7a852 Mon Sep 17 00:00:00 2001 From: Dave Perrett Date: Fri, 11 Aug 2017 12:26:15 +0900 Subject: [PATCH] Add an unload() method for clearing the dictionary. --- README.md | 8 +++++++ jquery.i18n.js | 13 ++++++++++- jquery.i18n.min.js | 2 +- specs/jquery.i18n.spec.js | 46 +++++++++++++++++++++++---------------- src/jquery.i18n.js | 11 ++++++++++ 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9ac4977..d0834b3 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ or using $('selector')._t() function $('div#example')._t('some text'); ``` +If you'd like to switch languages, you can unload the current dictionary and load a new one: + +```javascript +$.i18n.load('en'); +$.i18n.unload(); +$.i18n.load('ja'); +``` + Wildcards --------- diff --git a/jquery.i18n.js b/jquery.i18n.js index 312eae0..c3b2848 100644 --- a/jquery.i18n.js +++ b/jquery.i18n.js @@ -6,7 +6,7 @@ * * Licensed under the MIT license. * - * Version: 1.1.1 (Wed, 28 Jan 2015 15:16:19 GMT) + * Version: 1.1.1 (Fri, 11 Aug 2017 03:37:25 GMT) */ (function($) { /** @@ -37,11 +37,22 @@ } else { this.dict = i18n_dict; } + if (missingPattern) { this.missingPattern = missingPattern; } }, + /** + * unload() + * + * Unloads translations and clears the dictionary. + */ + unload: function() { + this.dict = null; + this.missingPattern = null; + }, + /** * _() * diff --git a/jquery.i18n.min.js b/jquery.i18n.min.js index 8c8000a..27be0b9 100644 --- a/jquery.i18n.min.js +++ b/jquery.i18n.min.js @@ -1 +1 @@ -!function(a){var b=Array.prototype.slice,c={dict:null,missingPattern:null,load:function(b,c){null!==this.dict?a.extend(this.dict,b):this.dict=b,c&&(this.missingPattern=c)},_:function(a){if(dict=this.dict,dict&&dict.hasOwnProperty(a))a=dict[a];else if(null!==this.missingPattern)return this.printf(this.missingPattern,a);return args=b.call(arguments),args[0]=a,this.printf.apply(this,args)},printf:function(c,d){return arguments.length<2?c:(d=a.isArray(d)?d:b.call(arguments,1),c.replace(/([^%]|^)%(?:(\d+)\$)?s/g,function(a,b,c){return c?b+d[parseInt(c)-1]:b+d.shift()}).replace(/%%s/g,"%s"))}};a.fn._t=function(){return a(this).html(c._.apply(c,arguments))},a.i18n=c}(jQuery); \ No newline at end of file +!function(a){var b=Array.prototype.slice,c={dict:null,missingPattern:null,load:function(b,c){null!==this.dict?a.extend(this.dict,b):this.dict=b,c&&(this.missingPattern=c)},unload:function(){this.dict=null,this.missingPattern=null},_:function(a){if(dict=this.dict,dict&&dict.hasOwnProperty(a))a=dict[a];else if(null!==this.missingPattern)return this.printf(this.missingPattern,a);return args=b.call(arguments),args[0]=a,this.printf.apply(this,args)},printf:function(c,d){return arguments.length<2?c:(d=a.isArray(d)?d:b.call(arguments,1),c.replace(/([^%]|^)%(?:(\d+)\$)?s/g,function(a,b,c){return c?b+d[parseInt(c)-1]:b+d.shift()}).replace(/%%s/g,"%s"))}};a.fn._t=function(){return a(this).html(c._.apply(c,arguments))},a.i18n=c}(jQuery); \ No newline at end of file diff --git a/specs/jquery.i18n.spec.js b/specs/jquery.i18n.spec.js index de2f382..698120b 100644 --- a/specs/jquery.i18n.spec.js +++ b/specs/jquery.i18n.spec.js @@ -1,75 +1,83 @@ describe ('jquery.i18n plugin', function() { - + it ('translates a key into the string', function() { $.i18n.load({ a_key: 'translated string' }); - + expect($.i18n._('a_key')).toEqual('translated string'); }); - + it ('returns the key when there is no translation', function() { $.i18n.load({ a_key: 'translated string' }); - + expect($.i18n._('another_key')).toEqual('another_key'); }); it ('returns the missing pattern when there is no translation, and a missing pattern is provided', function() { $.i18n.load({ a_key: 'translated string' }, "{{ %s }}"); - + expect($.i18n._('another_key')).toEqual('{{ another_key }}'); }); - + describe ('variable substitution', function() { - + describe ('variable lists', function() { - + it ('allows a string variable to be substituted into a translation', function() { $.i18n.load({ a_key: 'translated string %s' }); expect($.i18n._('a_key', ['variable'])).toEqual('translated string variable'); }); - + it ('allows many string variable to be substituted into a translation', function() { $.i18n.load({ a_key: 'translated string %s - %s - %s' }); expect($.i18n._('a_key', ['variables', 'in', 'list'])).toEqual('translated string variables - in - list'); }); - + it ('handles variables at the start of a translation', function() { $.i18n.load({ a_key: '%s and %s' }); expect($.i18n._('a_key', ['string 1', 'string 2'])).toEqual('string 1 and string 2'); }); - + it ('treats %%s as a literal %s', function() { $.i18n.load({ a_key: '%s and a literal %%s and %s' }); expect($.i18n._('a_key', ['string 1', 'string 2'])).toEqual('string 1 and a literal %s and string 2'); }); - + }); - + describe ('numbered variables', function() { - + it ('put 2 numbered variables out of order', function() { $.i18n.load({ a_key: 'translated string %2$s - %1$s' }); expect($.i18n._('a_key', ['order', 'in'])).toEqual('translated string in - order'); }); - + it ('put 2 numbered variables in order', function() { $.i18n.load({ a_key: 'translated string %1$s - %2$s' }); expect($.i18n._('a_key', ['order', 'in'])).toEqual('translated string order - in'); }); - + it ('put many numbered variables in random order', function() { $.i18n.load({ a_key: 'translated string %3$s %1$s - %2$s' }); expect($.i18n._('a_key', ['in', 'order', 'many' ])).toEqual('translated string many in - order'); }); - + }); - + }); - + + it ('allows the dictionary to be cleared', function() { + $.i18n.load({ a_key: 'translated string' }); + expect($.i18n._('a_key')).toEqual('translated string'); + $.i18n.unload(); + expect($.i18n._('a_key')).toEqual('a_key'); + expect($.i18n.dict).toBeNull(); + }); + }); diff --git a/src/jquery.i18n.js b/src/jquery.i18n.js index 142f1ae..ab16cb7 100644 --- a/src/jquery.i18n.js +++ b/src/jquery.i18n.js @@ -37,11 +37,22 @@ } else { this.dict = i18n_dict; } + if (missingPattern) { this.missingPattern = missingPattern; } }, + /** + * unload() + * + * Unloads translations and clears the dictionary. + */ + unload: function() { + this.dict = null; + this.missingPattern = null; + }, + /** * _() *