Skip to content

Commit

Permalink
feat(react-i18n): add dutch plural (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
flepretre committed Jun 19, 2021
1 parent 8f65044 commit 714620a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/react-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ This is the configuration of plural form for keys:

| language | zero | singular | general plural | first plural | second plural | third plural |
| -------- | ------- | -------- | -------------- | ------------ | ------------- | ------------ |
| FR | `one` | `one` | `other` | `other` | - | - |
| EN | `other` | `one` | `other` | `other` | - | - |
| HU | `one` | `one` | `other` | `one` | - | - |
| FR | `one` | `one` | `other` | `other` | - | - |
| HR | `many` | `one` | `other` | `one` | `few` | `many` |
| HU | `one` | `one` | `other` | `one` | - | - |
| NL | `other` | `one` | `other` | `other` | - | - |

The variable used in translation template string has to be `%(number)d`, and is automatically provided by the translate function.

Expand Down
11 changes: 11 additions & 0 deletions packages/react-i18n/src/utils/__tests__/i18n.utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ describe('i18n translate function', () => {
expect(t('foo.bar', undefined, 1, true)).toBe('first plural');
expect(t('foo.bar', undefined, 2, true)).toBe('general plural');
});

it('should pluralize in dutch', () => {
const lang = {
foo: { bar: { one: 'foo bar!', other: 'foos bars!!!' } },
_i18n: { lang: 'nl' },
};
const t = translate(lang);
expect(t('foo.bar', undefined, 0, true)).toBe('foos bars!!!');
expect(t('foo.bar', undefined, 1, true)).toBe('foo bar!');
expect(t('foo.bar', undefined, 2, true)).toBe('foos bars!!!');
});
});

describe('with number interpolation', () => {
Expand Down
17 changes: 9 additions & 8 deletions packages/react-i18n/src/utils/i18n.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import { sprintf } from 'sprintf-js';
import { interpolateHTMLTags } from './html.utils';

const pluralizeFunctions = {
en: number => (number === 0 || number > 1 ? 'other' : 'one'),
en: number => (number === 1 ? 'one' : 'other'),
fr: number => (number > 1 ? 'other' : 'one'),
hu: (number, general) => {
if (!general) {
return 'one';
}

return number > 1 ? 'other' : 'one';
},
hr: (number, general) => {
// General plural
if (general) {
Expand All @@ -34,6 +27,14 @@ const pluralizeFunctions = {

return '';
},
hu: (number, general) => {
if (!general) {
return 'one';
}

return number > 1 ? 'other' : 'one';
},
nl: number => (number === 1 ? 'one' : 'other'),
};

export const translate = (lang, i18nNames = {}, errorCallback = _.noop, parseHTML = false) => {
Expand Down

0 comments on commit 714620a

Please sign in to comment.