Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ Gettext.prototype.dnpgettext = function(domain, msgctxt, msgid, msgidPlural, cou

if (translation) {
if (typeof count === 'number') {
var pluralsFunc = plurals[Gettext.getLanguageCode(this.locale)].pluralsFunc;
// Prefer a locale-specific plural form (e.g. pt_BR) before the language (pt)
var localeKey = this.locale.toLowerCase().replace(/-/g, '_');
var pluralForms = plurals[localeKey] || plurals[Gettext.getLanguageCode(this.locale)];
var pluralsFunc = pluralForms.pluralsFunc;
index = pluralsFunc(count);
if (typeof index === 'boolean') {
index = index ? 1 : 0;
Expand Down
15 changes: 15 additions & 0 deletions lib/plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,21 @@ module.exports = {
return (n !== 1);
}
},
pt_br: {
name: 'Brazilian Portuguese',
examples: [{
plural: 0,
sample: 1
}, {
plural: 1,
sample: 2
}],
nplurals: 2,
pluralsText: 'nplurals = 2; plural = (n > 1)',
pluralsFunc: function(n) {
return (n > 1);
}
},
rm: {
name: 'Romansh',
examples: [{
Expand Down
53 changes: 53 additions & 0 deletions test/gettext-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,59 @@ describe('Gettext', function() {
});
});

describe('Locale-specific plural forms', function() {
var catalog;

beforeEach(function() {
catalog = {
charset: 'utf-8',
headers: {},
translations: {
'': {
apple: {
msgid: 'apple',
msgid_plural: 'apples',
msgstr: ['singular', 'plural']
}
}
}
};
});

it('should use n > 1 for Brazilian Portuguese (pt_BR)', function() {
gt.addTranslations('pt_BR', 'messages', catalog);
gt.setLocale('pt_BR');

expect(gt.ngettext('apple', 'apples', 0)).to.equal('singular');
expect(gt.ngettext('apple', 'apples', 1)).to.equal('singular');
expect(gt.ngettext('apple', 'apples', 2)).to.equal('plural');
});

it('should treat hyphenated pt-BR the same as pt_BR', function() {
gt.addTranslations('pt-BR', 'messages', catalog);
gt.setLocale('pt-BR');

expect(gt.ngettext('apple', 'apples', 0)).to.equal('singular');
});

it('should keep Portuguese (pt) as n !== 1', function() {
gt.addTranslations('pt', 'messages', catalog);
gt.setLocale('pt');

expect(gt.ngettext('apple', 'apples', 0)).to.equal('plural');
expect(gt.ngettext('apple', 'apples', 1)).to.equal('singular');
expect(gt.ngettext('apple', 'apples', 2)).to.equal('plural');
});

it('should fall back to the language-level plural form when the locale is not listed', function() {
gt.addTranslations('et-EE', 'messages', catalog);
gt.setLocale('et-EE');

expect(gt.ngettext('apple', 'apples', 0)).to.equal('plural');
expect(gt.ngettext('apple', 'apples', 1)).to.equal('singular');
});
});

describe('Unresolvable transaltions', function() {
beforeEach(function() {
gt.addTranslations('et-EE', 'messages', jsonFile);
Expand Down