Skip to content

Commit

Permalink
Merge commit 'e707deee461bfad86e94d0014dcf600896689318' into preactX
Browse files Browse the repository at this point in the history
  • Loading branch information
billneff79 committed May 10, 2021
2 parents f225f69 + e707dee commit 900abad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The available forms for specifying pluralization values are as follows:

- `"key": { "singular":"apple", "plural":"apples" }`
- `"key": { "none":"no apples", "one":"apple", "many":"apples" }`
- `"key": { "zero":"no apples", "one":"apple", "other":"apples" }`
- `"key": ["apples", "apple"]`

Taking `<Text id="news.totalStories" ..>` from our example:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function translate(id, scope, dictionary, fields, plural, fallbac
if (value.splice) {
value = value[plural] || value[0];
}
else if (plural===0 && defined(value.none)) {
else if (plural===0 && defined(value.none || value.zero)) {
value = value.none || value.zero;
}
else if (plural===1 && defined(value.one || value.singular)) {
Expand Down
23 changes: 23 additions & 0 deletions test/lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ describe('translate', () => {
it('should translate <Text /> components that exist in field values when working on templated strings', () => {
expect(translate('foo.bar', undefined, { foo: { bar: 'hello{{c.d}}' }, e: 'World' }, { c: { d: <Text id="e" /> } })).to.equal('helloWorld');
});

it('should pluralise for none/one/many pluralisation keys', () => {
expect(translate('foo.bar', undefined, { foo: { bar: { none: 'none', one: 'one', many: 'many' } } }, undefined, 0)).to.equal('none');
expect(translate('foo.bar', undefined, { foo: { bar: { none: 'none', one: 'one', many: 'many' } } }, undefined, 1)).to.equal('one');
expect(translate('foo.bar', undefined, { foo: { bar: { none: 'none', one: 'one', many: 'many' } } }, undefined, 2)).to.equal('many');
expect(translate('foo.bar', undefined, { foo: { bar: { none: 'none', one: 'one', many: 'many' } } }, undefined, 100)).to.equal('many');
});

it('should pluralise for zero/one/other pluralisation keys', () => {
expect(translate('foo.bar', undefined, { foo: { bar: { zero: 'zero', one: 'one', other: 'other' } } }, undefined, 0)).to.equal('zero');
expect(translate('foo.bar', undefined, { foo: { bar: { zero: 'zero', one: 'one', other: 'other' } } }, undefined, 1)).to.equal('one');
expect(translate('foo.bar', undefined, { foo: { bar: { zero: 'zero', one: 'one', other: 'other' } } }, undefined, 2)).to.equal('other');
expect(translate('foo.bar', undefined, { foo: { bar: { zero: 'zero', one: 'one', other: 'other' } } }, undefined, 100)).to.equal('other');
});

it('should pluralise for singular/plural pluralisation keys', () => {
// assume 0 is a plural form if using the singular/plural pluralisation convention
expect(translate('foo.bar', undefined, { foo: { bar: { singular: 'singular', plural: 'plural' } } }, undefined, 0)).to.equal('plural');

expect(translate('foo.bar', undefined, { foo: { bar: { singular: 'singular', plural: 'plural' } } }, undefined, 1)).to.equal('singular');
expect(translate('foo.bar', undefined, { foo: { bar: { singular: 'singular', plural: 'plural' } } }, undefined, 2)).to.equal('plural');
expect(translate('foo.bar', undefined, { foo: { bar: { singular: 'singular', plural: 'plural' } } }, undefined, 100)).to.equal('plural');
});


});

0 comments on commit 900abad

Please sign in to comment.