2 changes: 1 addition & 1 deletion dist/gettext.esm.min.js
6 changes: 5 additions & 1 deletion dist/gettext.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ var i18n = (function () {
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');

if (!pf_re.test(plural_form))
var match = plural_form.match(pf_re);

if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));

console.log('>>> Plural form:', plural_form);

// Careful here, this is a hidden eval() equivalent..
// Risk should be reasonable though since we test the plural_form through regex before
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
Expand Down
2 changes: 1 addition & 1 deletion dist/gettext.iife.min.js
4 changes: 3 additions & 1 deletion lib/gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ var i18n = function (options) {
// plural forms list available here http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
var pf_re = new RegExp('^\\s*nplurals\\s*=\\s*[0-9]+\\s*;\\s*plural\\s*=\\s*(?:\\s|[-\\?\\|&=!<>+*/%:;n0-9_\(\)])+');

if (!pf_re.test(plural_form))
var match = plural_form.match(pf_re);

if (!match || match[0] !== plural_form)
throw new Error(strfmt('The plural form "%1" is not valid', plural_form));

// Careful here, this is a hidden eval() equivalent..
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@
expect(e.message).to.be('The plural form "nplurals=2; plural=[not valid];" is not valid');
}
});
it('should fail another unvalid plural form', function () {
i18n = new window.i18n({ locale: 'foo' });
i18n.setMessages('messages', 'foo', {
"There is %1 apple": [
"Il y a %1 pomme",
"Il y a %1 pommes"
]
}, 'nplurals=2; plural=n>1; console.log(`PWNED!`);');

// do not throw error on default plural form if key does not have a translation
expect(i18n.ngettext('foo', 'bar', 2)).to.be('bar');

try {
i18n.ngettext('There is %1 apple', 'There are %1 apples', 42);
} catch (e) {
expect(e.message).to.be('The plural form "nplurals=2; plural=n>1; console.log(`PWNED!`);" is not valid');
}
});
it('should handle multiple locale & pluals cohabitation', function () {
i18n = new window.i18n({ locale: 'foo' });
i18n.setMessages('messages', 'foo', {
Expand Down