Skip to content

Commit

Permalink
Add an unload() method for clearing the dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
recurser committed Aug 11, 2017
1 parent 44005c3 commit e77d4f3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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
---------

Expand Down
13 changes: 12 additions & 1 deletion jquery.i18n.js
Expand Up @@ -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($) {
/**
Expand Down Expand Up @@ -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;
},

/**
* _()
*
Expand Down
2 changes: 1 addition & 1 deletion jquery.i18n.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 27 additions & 19 deletions 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();
});

});
11 changes: 11 additions & 0 deletions src/jquery.i18n.js
Expand Up @@ -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;
},

/**
* _()
*
Expand Down

0 comments on commit e77d4f3

Please sign in to comment.