-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(i18n): adopt next/root-params
#9105
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import PlatformAnalytics from '#platform/analytics'; | |
| import { availableLocales, defaultLocale } from '@node-core/website-i18n'; | ||
| import classNames from 'classnames'; | ||
| import { NextIntlClientProvider } from 'next-intl'; | ||
| import { getLocale } from 'next-intl/server'; | ||
|
|
||
| import BaseLayout from '#site/layouts/Base'; | ||
| import { IBM_PLEX_MONO, OPEN_SANS } from '#site/next.fonts'; | ||
|
|
@@ -13,12 +14,8 @@ import '#site/styles/index.css'; | |
|
|
||
| const fontClasses = classNames(IBM_PLEX_MONO.variable, OPEN_SANS.variable); | ||
|
|
||
| type RootLayoutProps = PropsWithChildren<{ | ||
| params: Promise<{ locale: string }>; | ||
| }>; | ||
|
|
||
| const RootLayout: FC<RootLayoutProps> = async ({ children, params }) => { | ||
| const { locale } = await params; | ||
| const RootLayout: FC<PropsWithChildren> = async ({ children }) => { | ||
| const locale = await getLocale(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
|
|
||
| const { langDir, hrefLang } = | ||
| availableLocales.find(l => l.code === locale) || defaultLocale; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { availableLocaleCodes, defaultLocale } from '@node-core/website-i18n'; | ||
| import defaultMessages from '@node-core/website-i18n/locales/en.json'; | ||
| import { locale as getRootLocale } from 'next/root-params'; | ||
| import { getRequestConfig } from 'next-intl/server'; | ||
|
|
||
| import { deepMerge } from './util/objects'; | ||
|
|
@@ -25,9 +26,10 @@ const loadLocaleDictionary = async (locale: string) => { | |
| }; | ||
|
|
||
| // Provides `next-intl` configuration for RSC/SSR | ||
| export default getRequestConfig(async ({ requestLocale }) => { | ||
| // This typically corresponds to the `[locale]` segment | ||
| let locale = await requestLocale; | ||
| export default getRequestConfig(async params => { | ||
| // An explicit locale passed to an awaitable API like `getTranslations({ locale })` | ||
| // wins, otherwise we read the `[locale]` segment of the root layout | ||
| let locale = params.locale ?? (await getRootLocale()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key: I've kept the behavior to read an override from a call site though, as this can be handy for Server Actions and Route Handlers (where
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noice ! |
||
|
|
||
| // Ensure that the incoming locale is valid | ||
| if (!locale || !availableLocaleCodes.includes(locale)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import { | |
| availableLocaleCodes, | ||
| } from '@node-core/website-i18n'; | ||
| import { notFound, redirect } from 'next/navigation'; | ||
| import { setRequestLocale } from 'next-intl/server'; | ||
| import { locale as getRootLocale } from 'next/root-params'; | ||
|
|
||
| import { setClientContext } from '#site/client-context'; | ||
| import WithLayout from '#site/components/withLayout'; | ||
|
|
@@ -28,11 +28,13 @@ export const generateViewport = () => ({ ...PAGE_VIEWPORT }); | |
| * | ||
| * @see https://nextjs.org/docs/app/api-reference/functions/generate-metadata | ||
| * | ||
| * @param {{ params: Promise<{ path: Array<string>; locale: string }>, prefix?: string }} props | ||
| * @param {{ params: Promise<{ path: Array<string> }>, prefix?: string }} props | ||
| * @returns {Promise<import('next').Metadata>} the metadata for the page | ||
| */ | ||
| export const generateMetadata = async ({ params, prefix }) => { | ||
| const { path = [], locale = defaultLocale.code } = await params; | ||
| const { path = [] } = await params; | ||
|
|
||
| const locale = (await getRootLocale()) ?? defaultLocale.code; | ||
|
|
||
| const pathname = dynamicRouter.getPathname(path); | ||
|
|
||
|
|
@@ -46,15 +48,16 @@ export const generateMetadata = async ({ params, prefix }) => { | |
| /** | ||
| * This method is used for retrieving the current locale and pathname from the request | ||
| * | ||
| * The locale comes from the `[locale]` root param, so pages don't have to read it | ||
| * from their own `params` and hand it over. | ||
| * | ||
| * @param {string|Array<string>} path | ||
| * @param {string} locale | ||
| * @returns {[string, string]} the locale and pathname for the request | ||
| * @returns {Promise<[string, string]>} the locale and pathname for the request | ||
| */ | ||
| export const getLocaleAndPath = (path = [], locale = defaultLocale.code) => { | ||
| if (!availableLocaleCodes.includes(locale)) { | ||
| // Forces the current locale to be the Default Locale | ||
| setRequestLocale(defaultLocale.code); | ||
| export const getLocaleAndPath = async (path = []) => { | ||
| const locale = (await getRootLocale()) ?? defaultLocale.code; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've considered reading from |
||
|
|
||
| if (!availableLocaleCodes.includes(locale)) { | ||
| if (!allLocaleCodes.includes(locale)) { | ||
| // when the locale is not listed in the locales, return NotFound | ||
| return notFound(); | ||
|
|
@@ -66,9 +69,6 @@ export const getLocaleAndPath = (path = [], locale = defaultLocale.code) => { | |
| return redirect(`/${defaultLocale.code}/${pathname}`); | ||
| } | ||
|
|
||
| // Configures the current Locale to be the given Locale of the Request | ||
| setRequestLocale(locale); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer needed. |
||
|
|
||
| // Gets the current full pathname for a given path | ||
| return [locale, dynamicRouter.getPathname(path)]; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass the
routeLocaleanymore since we can read fromnext/root-paramswithingetLocaleAndPath.