Skip to content

Commit f5c287f

Browse files
committed
Allow define the plural function directly, to prevent CSP errors #4
1 parent 78c7bd7 commit f5c287f

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"sprintf-js": "^1.0.3"
2121
},
2222
"devDependencies": {
23-
"mocha": "^3.4.2"
23+
"mocha": "^5.0.0"
2424
}
2525
}

src/translator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
return this;
3939
}
4040

41-
if (translations['plural-forms']) {
41+
if (translations.fn) {
42+
this.plurals[domain] = { fn: translations.fn };
43+
} else if (translations['plural-forms']) {
4244
var plural = translations['plural-forms'].split(';', 2);
4345

4446
this.plurals[domain] = {

tests/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ describe('vsprintf functions', function() {
2727
assert.equal('2 arquivos', i18n.n__('one file', '%s files', 2, 2));
2828
});
2929
});
30+
31+
describe('custom plural functions', function() {
32+
var translations = require(__dirname + '/translations3.json');
33+
var called = false;
34+
translations.fn = function (n) {
35+
called = true;
36+
return 2;
37+
};
38+
39+
i18n.loadTranslations(translations);
40+
41+
it('should return always the second plural', function () {
42+
assert.equal('foo 3', i18n.dngettext('foo', 'foo x', 'foo y', 0));
43+
assert.equal('foo 3', i18n.dngettext('foo', 'foo x', 'foo y', 1));
44+
assert.equal('foo 3', i18n.dngettext('foo', 'foo x', 'foo y', 2));
45+
assert.equal('foo 3', i18n.dngettext('foo', 'foo x', 'foo y', 3));
46+
});
47+
48+
it('the custom function was called', function () {
49+
assert(called, 'The custom function was not called')
50+
});
51+
});

0 commit comments

Comments
 (0)