Skip to content

Commit

Permalink
fallback to singular if no plural is found
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Mar 10, 2015
1 parent 1520c1f commit 5ab950a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- add support on key is a number
- added getResourceBundle to API
- allow multiple post-processors
- fallback to singular if no plural is found fixes issue [issue 356](https://github.com/i18next/i18next/issues/356)

### 1.7.7
- fixes issue with stack overflow on t(lng, count)
Expand Down
34 changes: 34 additions & 0 deletions spec/translate/translate.plurals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ describe('plural usage', function() {

});

describe('fallback on count with non-plurals', function() {
var resStore = {
dev: { 'ns.2': {
pluralTestWithCount: '__count__ item from ns.2'
}},
en: { },
'en-US': {
'ns.1': {
pluralTestWithCount: '__count__ item'
}
}
};

beforeEach(function(done) {
i18n.init(i18n.functions.extend(opts, {
resStore: resStore,
ns: { namespaces: ['ns.1', 'ns.2'], defaultNs: 'ns.1'}
}),
function(t) { done(); });
});

it('it should provide correct singular form', function() {
expect(i18n.t('pluralTestWithCount', {count: 0})).to.be('0 item');
expect(i18n.t('pluralTestWithCount', {count: 1})).to.be('1 item');
expect(i18n.t('pluralTestWithCount', {count: 7})).to.be('7 item');
});

it('it should provide correct singular form for second namespace', function() {
expect(i18n.t('ns.2:pluralTestWithCount', {count: 1})).to.be('1 item from ns.2');
expect(i18n.t('ns.2:pluralTestWithCount', {count: 7})).to.be('7 item from ns.2');
});

});

describe('Plurals with passing lng to translation function', function() {

var resStore = {
Expand Down
12 changes: 11 additions & 1 deletion src/i18next.translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ function _find(key, options) {
if (needsPlural(options, lngs[0])) {
optionWithoutCount = f.extend({ lngs: [lngs[0]]}, options);
delete optionWithoutCount.count;
optionWithoutCount._origLng = optionWithoutCount._origLng || optionWithoutCount.lng || lngs[0];
delete optionWithoutCount.lng;
optionWithoutCount.defaultValue = o.pluralNotFound;

Expand Down Expand Up @@ -310,12 +311,21 @@ function _find(key, options) {
var clone = lngs.slice();
clone.shift();
options = f.extend(options, { lngs: clone });
options._origLng = optionWithoutCount._origLng;
delete options.lng;
// retry with fallbacks
translated = translate(ns + o.nsseparator + key, options);
if (translated != o.pluralNotFound) return translated;
} else {
return translated;
optionWithoutCount.lng = optionWithoutCount._origLng;
delete optionWithoutCount._origLng;
translated = translate(ns + o.nsseparator + key, optionWithoutCount);

return applyReplacement(translated, {
count: options.count,
interpolationPrefix: options.interpolationPrefix,
interpolationSuffix: options.interpolationSuffix
});
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,40 @@ describe('i18next', function() {

});

describe('fallback on count with non-plurals', function() {
var resStore = {
dev: { 'ns.2': {
pluralTestWithCount: '__count__ item from ns.2'
}},
en: { },
'en-US': {
'ns.1': {
pluralTestWithCount: '__count__ item'
}
}
};

beforeEach(function(done) {
i18n.init(i18n.functions.extend(opts, {
resStore: resStore,
ns: { namespaces: ['ns.1', 'ns.2'], defaultNs: 'ns.1'}
}),
function(t) { done(); });
});

it('it should provide correct singular form', function() {
expect(i18n.t('pluralTestWithCount', {count: 0})).to.be('0 item');
expect(i18n.t('pluralTestWithCount', {count: 1})).to.be('1 item');
expect(i18n.t('pluralTestWithCount', {count: 7})).to.be('7 item');
});

it('it should provide correct singular form for second namespace', function() {
expect(i18n.t('ns.2:pluralTestWithCount', {count: 1})).to.be('1 item from ns.2');
expect(i18n.t('ns.2:pluralTestWithCount', {count: 7})).to.be('7 item from ns.2');
});

});

describe('Plurals with passing lng to translation function', function() {

var resStore = {
Expand Down

0 comments on commit 5ab950a

Please sign in to comment.