Skip to content

Commit

Permalink
Merge pull request #245 from HitkoDev/master
Browse files Browse the repository at this point in the history
Fix plurals bug when locale contains region code
  • Loading branch information
mashpie authored Jun 21, 2016
2 parents d4cdac8 + 3960dad commit fcc11aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ module.exports = (function() {
if (PluralsForLocale[targetLocale]) {
p = PluralsForLocale[targetLocale];
} else {
p = new MakePlural(targetLocale);
// split locales with a region code
var lc = targetLocale.toLowerCase().split(/[_-\s]+/)
.filter(function(el){ return true && el; });
// take the first part of locale, fallback to full locale
p = new MakePlural(lc[0] || targetLocale);
PluralsForLocale[targetLocale] = p;
}

Expand Down
34 changes: 33 additions & 1 deletion test/i18n.makePlurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,32 @@ function putJson(l, d) {
describe('i18n supports MakePlural', function() {

var TestScope = {};
var locales = ['en', 'de', 'fr', 'ru', 'ar'];
var locales = ['en', 'de', 'fr', 'ru', 'ar', 'de-DE', 'de-AT', 'de-CH'];
var fixture = {
de: {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-DE': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-AT': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-CH': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
en: {
"%s cat": {
one: '%d cat',
Expand Down Expand Up @@ -138,4 +156,18 @@ describe('i18n supports MakePlural', function() {
done();
});

it('__n() should return correctly in german for all regions', function(done) {
var regions = ['de-DE', 'de-AT', 'de-CH'];
for(var i = 0; i < regions.length; i++) {
TestScope.setLocale(regions[i]);
should.deepEqual(TestScope.__n('%s cat', 0), '0 Katzen');
should.deepEqual(TestScope.__n('%s cat', 1), '1 Katze');
should.deepEqual(TestScope.__n('%s cat', 2), '2 Katzen');
should.deepEqual(TestScope.__n('%s cat', 5), '5 Katzen');
should.deepEqual(TestScope.__n('%s cat', 6), '6 Katzen');
should.deepEqual(TestScope.__n('%s cat', 21), '21 Katzen');
}
done();
});

});

0 comments on commit fcc11aa

Please sign in to comment.