Skip to content

Commit aafa8b7

Browse files
committed
feat: Support usage of rootParams in i18n/request.ts
1 parent 7e7011d commit aafa8b7

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

packages/next-intl/src/server/react-server/getConfig.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ See also: https://next-intl.dev/docs/usage/configuration#i18n-request
3838
}
3939

4040
const params: GetRequestConfigParams = {
41+
locale: localeOverride,
42+
4143
// In case the consumer doesn't read `params.locale` and instead provides the
4244
// `locale` (either in a single-language workflow or because the locale is
4345
// read from the user settings), don't attempt to read the request locale.

packages/next-intl/src/server/react-server/getRequestConfig.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {IntlConfig} from 'use-intl/core';
1+
import type {IntlConfig, Locale} from 'use-intl/core';
22

33
export type RequestConfig = Omit<IntlConfig, 'locale'> & {
44
/**
@@ -8,6 +8,13 @@ export type RequestConfig = Omit<IntlConfig, 'locale'> & {
88
};
99

1010
export type GetRequestConfigParams = {
11+
/**
12+
* If you provide an explicit locale to an async server-side function like
13+
* `getTranslations({locale: 'en'})`, it will be passed via `locale` to
14+
* `getRequestConfig` so you can use it instead of the segment value.
15+
*/
16+
locale?: Locale;
17+
1118
/**
1219
* Typically corresponds to the `[locale]` segment that was matched by the middleware.
1320
*

packages/use-intl/src/core/hasLocale.test.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {it} from 'vitest';
1+
import {expect, it} from 'vitest';
22
import hasLocale from './hasLocale.tsx';
33

44
it('narrows down the type', () => {
@@ -24,3 +24,11 @@ it('can be called with a non-matching narrow candidate', () => {
2424
candidate satisfies never;
2525
}
2626
});
27+
28+
it('can be called with any candidate', () => {
29+
const locales = ['en-US', 'en-GB'] as const;
30+
expect(hasLocale(locales, 'unknown')).toBe(false);
31+
32+
// Relevant since `ParamValue` in Next.js includes `string[]`
33+
expect(hasLocale(locales, ['de'])).toBe(false);
34+
});

packages/use-intl/src/core/hasLocale.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {Locale} from './AppConfig.tsx';
77
*/
88
export default function hasLocale<LocaleType extends Locale>(
99
locales: ReadonlyArray<LocaleType>,
10-
candidate?: string | null
10+
candidate?: unknown
1111
): candidate is LocaleType {
1212
return locales.includes(candidate as LocaleType);
1313
}

0 commit comments

Comments
 (0)