Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next-international): plurals with count 0 works with #zero #217

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/next-international/__tests__/use-i18n.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,69 @@ describe('useI18n', () => {

expect(Array.isArray(result.current)).toBe(true);
});

const pluralTestCases = [
{
count: 0,
expected: 'No cows (#zero)',
},
{
count: 1,
expected: 'One cow (#one)',
},
{
count: 2,
expected: '2 cows (#other)',
},
...[...new Array(30).fill(0)].map((_, i) => ({ count: i + 3, expected: `${i + 3} cows (#other)` })),
];

it.each(pluralTestCases)('should return correct plural: $count ($expected)', async ({ count, expected }) => {
const { useI18n, I18nProvider } = createI18n({
en: () => import('./utils/en'),
fr: () => import('./utils/fr'),
});

const App = ({ children }: { children: React.ReactNode }) => {
return <I18nProvider locale={en}>{children}</I18nProvider>;
};

const { result } = renderHook(
() => {
const t = useI18n();
return t('cow', {
count,
});
},
{
wrapper: App,
},
);

expect(result.current).toBe(expected);
});
it('should fallback on #other if #zero is not defined', async () => {
const { useI18n, I18nProvider } = createI18n({
en: () => import('./utils/en'),
fr: () => import('./utils/fr'),
});

const App = ({ children }: { children: React.ReactNode }) => {
return <I18nProvider locale={en}>{children}</I18nProvider>;
};

const { result } = renderHook(
() => {
const t = useI18n();
return t('horse', {
count: 0,
});
},
{
wrapper: App,
},
);

expect(result.current).toBe('0 horses (#other)');
});
});
5 changes: 5 additions & 0 deletions packages/next-international/__tests__/utils/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export default {
'namespace.subnamespace.user.description': '{name} is {years} years old',
'only.exists.in.en': 'EN locale',
'double.param': 'This {param} is used twice ({param})',
'cow#zero': 'No cows (#zero)',
'cow#one': 'One cow (#one)',
'cow#other': '{count} cows (#other)',
'horse#one': 'One horse (#one)',
'horse#other': '{count} horses (#other)',
} as const;
8 changes: 7 additions & 1 deletion packages/next-international/src/common/create-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ export function createT<Locale extends BaseLocale, Scope extends Scopes<Locale>
.filter(key => key.includes('#'))
.map(key => key.split('#')[0]),
);

const pluralRules = new Intl.PluralRules(context.locale);

function getPluralKey(count: number) {
if (count === 0) return 'zero';
return pluralRules.select(count);
}

function t<Key extends LocaleKeys<Locale, Scope>, Value extends LocaleValue = ScopedValue<Locale, Scope, Key>>(
key: Key,
...params: CreateParams<ParamsObject<Value>, Locale, Scope, Key, Value>
Expand All @@ -48,7 +54,7 @@ export function createT<Locale extends BaseLocale, Scope extends Scopes<Locale>
const isPluralKey = scope ? pluralKeys.has(`${scope}.${key}`) : pluralKeys.has(key);

if (isPluralKey) {
key = `${key}#${pluralRules.select(paramObject.count)}` as Key;
key = `${key}#${getPluralKey(paramObject.count)}` as Key;
isPlural = true;
}
}
Expand Down