-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Infer default
locale
for NextIntlClientProvider
from `useP…
…arams` when rendered on the client side (#1483) Bumps the Next.js peer dependency to 13.3.
- Loading branch information
Showing
6 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 25 additions & 21 deletions
46
packages/next-intl/src/shared/NextIntlClientProvider.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
import {render, screen} from '@testing-library/react'; | ||
import {it} from 'vitest'; | ||
import {it, vi} from 'vitest'; | ||
import { | ||
NextIntlClientProvider, | ||
useTranslations | ||
} from '../index.react-client.tsx'; | ||
|
||
it('can use messages from the provider', () => { | ||
function Component() { | ||
const t = useTranslations(); | ||
return <>{t('message')}</>; | ||
vi.mock('next/navigation.js', () => ({ | ||
useParams() { | ||
return {locale: 'en'}; | ||
} | ||
})); | ||
|
||
render( | ||
<NextIntlClientProvider locale="en" messages={{message: 'Hello'}}> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
|
||
screen.getByText('Hello'); | ||
}); | ||
function Component() { | ||
const t = useTranslations(); | ||
return <>{t('message', {price: 29000.5})}</>; | ||
} | ||
|
||
it('can override the locale from Next.js', () => { | ||
function Component() { | ||
const t = useTranslations(); | ||
return <>{t('message', {price: 29000.5})}</>; | ||
} | ||
|
||
render( | ||
function TestProvider({locale}: {locale?: string}) { | ||
return ( | ||
<NextIntlClientProvider | ||
locale="de" | ||
locale={locale} | ||
messages={{message: '{price, number, ::currency/EUR}'}} | ||
> | ||
<Component /> | ||
</NextIntlClientProvider> | ||
); | ||
} | ||
|
||
it('can use messages from the provider', () => { | ||
render(<TestProvider locale="en" />); | ||
screen.getByText('€29,000.50'); | ||
}); | ||
|
||
it('reads a default locale from params', () => { | ||
render(<TestProvider />); | ||
screen.getByText('€29,000.50'); | ||
}); | ||
|
||
it('can override the locale from Next.js', () => { | ||
render(<TestProvider locale="de" />); | ||
screen.getByText('29.000,50 €'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {useParams as useNextParams} from 'next/navigation.js'; | ||
|
||
export default function useParams() { | ||
// The types aren't entirely correct here. Outside of Next.js | ||
// `useParams` can be called, but the return type is `null`. | ||
return useNextParams() as ReturnType<typeof useNextParams> | null; | ||
} |