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): use a regex to allow multiple interpolations #30

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions packages/next-international/__tests__/use-i18n.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ describe('useI18n', () => {
expect(screen.getByText("Today's weather is sunny")).toBeInTheDocument();
});

it('should translate with the same param used twice', async () => {
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({
en: () => import('./utils/en'),
fr: () => import('./utils/fr'),
});

function App() {
const { t } = useI18n();

return (
<p>
{t('double.param', {
param: '<PARAMETER>',
})}
</p>
);
}

render(
<I18nProvider locale={en}>
<App />
</I18nProvider>,
);

expect(screen.getByText('This <PARAMETER> is used twice (<PARAMETER>)')).toBeInTheDocument();
});

it('should translate with multiple params', async () => {
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({
en: () => import('./utils/en'),
Expand Down
1 change: 1 addition & 0 deletions packages/next-international/__tests__/utils/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default {
'namespace.subnamespace.weather': "Today's weather is {weather}",
'namespace.subnamespace.user.description': '{name} is {years} years old',
'only.exists.in.en': 'EN locale',
'double.param': 'This {param} is used twice ({param})',
} as const;
2 changes: 1 addition & 1 deletion packages/next-international/src/i18n/create-use-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function createUsei18n<Locale extends BaseLocale>(I18nContext: Context<Lo
}

for (const [param, paramValue] of Object.entries(paramObject as Locale)) {
value = value.toString().replace(`{${param}}`, paramValue.toString());
value = value.toString().replace(new RegExp(`{${param}}`, 'g'), paramValue.toString());
}

return value;
Expand Down