Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring Portuguese plurals in line with i18next and CLDR #68

Merged
merged 1 commit into from
Oct 16, 2017
Merged
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
2 changes: 1 addition & 1 deletion src/lib/gettext2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function getGettextValues(values, locale, targetKey, options) {
return emptyOrObject(targetKey, values[0], options);
}

const ext = plurals.rules[locale.replace('_', '-').split('-')[0]];
const ext = plurals.getRule(locale);
const gettextValues = {};

for (let i = 0; i < values.length; i += 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/json2gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function parseGettext(domain, data, options = {}) {
translations: {},
};

const ext = plurals.rules[domain.replace('_', '-').split('-')[0]];
const ext = plurals.getRule(domain);
const trans = {};

out.headers['plural-forms'] =
Expand Down
14 changes: 9 additions & 5 deletions src/lib/plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,14 @@ module.exports = {
plurals: '(n != 1)',
},
pt: {
name: 'Portuguese',
name: 'Portuguese', // according to http://www.unicode.org/cldr/charts/26/supplemental/language_plural_rules.html#pt
nplurals: 2,
plurals: '(n != 1)',
plurals: '(n > 1)',
},
pt_br: {
name: 'Brazilian Portuguese',
pt_pt: {
name: 'European Portuguese',
nplurals: 2,
plurals: '(n > 1)',
plurals: '(n != 1)',
},
rm: {
name: 'Romansh',
Expand Down Expand Up @@ -662,4 +662,8 @@ module.exports = {
plurals: '0',
},
},
getRule(code) {
const locale = code.replace('-', '_');
return this.rules[locale.toLowerCase()] || this.rules[locale.split('_')[0]];
},
};
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ describe('i18next-gettext-converter', () => {
}),
])
));

describe('should return the correct plural forms for Portuguese', () => {
[
['pt-PT', 'plural=(n != 1)'], // pt-PT = European Portuguese = nplurals=2; plural=(n != 1);
['pt-BR', 'plural=(n > 1)'], // pt-BR = Brazillian Portuguese = nplurals=2; plural=(n > 1);
['pt-br', 'plural=(n > 1)'], // pt-BR === pt-br
['pt', 'plural=(n > 1)'], // pt = Portuguese (catch all) == pt-BR == plural=(n > 1);
].forEach(([code, plural]) => {
it(`${code} should have ${plural}`, () => (
i18nextToPo(code, '{}').then((result) => {
expect(result.toString()).to.include(`Plural-Forms: nplurals=2; ${plural}`);
})
));
});
});
});

describe('the functions', () => {
Expand Down