Skip to content

Commit

Permalink
Fix PLURAL operator when less params are specified
Browse files Browse the repository at this point in the history
The behaviour was previously incorrect for cases where the language has no plural rule defined (such as English, Chinese, etc) and the number isn't 1.

such as `new Banana('en').i18n('{{PLURAL:$1|foo}}', 3)` which was giving `undefined` instead of `foo`.

This bug isn't affecting `banana-i18n` because it uses Intl so this code path isn't there.
  • Loading branch information
siddharthvp committed May 20, 2021
1 parent 2a13edf commit 8d73d29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/languages/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class BananaLanguage {

if (!pluralRules) {
// default fallback.
return (count === 1) ? forms[ 0 ] : forms[ 1 ]
pluralRules = { one: 'i = 1 and v = 0' }
}

let pluralFormIndex = this.getPluralForm(count, pluralRules)
Expand Down
9 changes: 6 additions & 3 deletions test/banana.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,6 @@ describe('Banana', function () {
it('should parse the plural and gender', () => {
let locale = 'en'
const banana = new Banana(locale, {})
const messages = fs.readFileSync(`./test/i18n/${locale}.json`)
banana.load(JSON.parse(messages), locale)
assert.strictEqual(
banana.i18n('This message key does not exist'),
'This message key does not exist',
Expand Down Expand Up @@ -527,7 +525,12 @@ describe('Banana', function () {
assert.strictEqual(
banana.i18n(pluralAndGenderMessageWithLessParaMS, 'Meera', 1, 'female'),
'Meera has 1 kitten. She loves to play with it.',
'Plural and gender test - female, singular, but will less parameters in message'
'Plural and gender test - female, singular, but with less parameters in message'
)
assert.strictEqual(
banana.i18n(pluralAndGenderMessageWithLessParaMS, 'Meera', 2, 'female'),
'Meera has 2 kitten. She loves to play with it.',
'Plural and gender test - female, plural, with less parameters in message'
)
assert.strictEqual(
banana.i18n(pluralAndGenderMessageWithCase, 'Meera', 1, 'female'),
Expand Down

0 comments on commit 8d73d29

Please sign in to comment.