intor 2.2.14 → 2.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exports/config/index.d.ts +2 -0
- package/dist/exports/config/index.js +4 -0
- package/dist/exports/index.d.ts +5 -0
- package/dist/exports/index.js +11 -0
- package/dist/exports/next/index.d.ts +3 -0
- package/dist/exports/next/index.js +9 -0
- package/dist/exports/next/middleware/index.d.ts +2 -0
- package/dist/exports/next/middleware/index.js +2 -0
- package/dist/exports/next/server/index.d.ts +2 -0
- package/dist/exports/next/server/index.js +2 -0
- package/dist/exports/react/index.d.ts +2 -0
- package/dist/exports/react/index.js +6 -0
- package/dist/exports/server/index.d.ts +2 -0
- package/dist/exports/server/index.js +7 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-all.d.ts +12 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-all.js +60 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-except-default.d.ts +16 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-except-default.js +81 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-none.d.ts +10 -0
- package/dist/src/adapters/next/middleware/handle-prefix/handle-prefix-none.js +22 -0
- package/dist/src/adapters/next/middleware/index.d.ts +2 -0
- package/dist/src/adapters/next/middleware/intor-middleware.d.ts +6 -0
- package/dist/src/adapters/next/middleware/intor-middleware.js +22 -0
- package/dist/src/adapters/next/middleware/utils/create-response.d.ts +17 -0
- package/dist/src/adapters/next/middleware/utils/create-response.js +42 -0
- package/dist/src/adapters/next/middleware/utils/determine-initial-locale.d.ts +8 -0
- package/dist/src/adapters/next/middleware/utils/determine-initial-locale.js +28 -0
- package/dist/src/adapters/next/middleware/utils/set-locale-cookie-edge.d.ts +15 -0
- package/dist/src/adapters/next/middleware/utils/set-locale-cookie-edge.js +24 -0
- package/dist/src/adapters/next/middleware/utils/set-pathname-header.d.ts +12 -0
- package/dist/src/adapters/next/middleware/utils/set-pathname-header.js +13 -0
- package/dist/src/adapters/next/navigation/index.d.ts +4 -0
- package/dist/src/adapters/next/navigation/link.d.ts +16 -0
- package/dist/src/adapters/next/navigation/link.js +25 -0
- package/dist/src/adapters/next/navigation/redirect.d.ts +16 -0
- package/dist/src/adapters/next/navigation/redirect.js +36 -0
- package/dist/src/adapters/next/navigation/use-pathname.d.ts +17 -0
- package/dist/src/adapters/next/navigation/use-pathname.js +49 -0
- package/dist/src/adapters/next/navigation/use-router.d.ts +20 -0
- package/dist/src/adapters/next/navigation/use-router.js +31 -0
- package/dist/src/adapters/next/navigation/utils/should-full-reload.d.ts +7 -0
- package/dist/src/adapters/next/navigation/utils/should-full-reload.js +19 -0
- package/dist/src/adapters/next/navigation/utils/use-locale-switch.d.ts +16 -0
- package/dist/src/adapters/next/navigation/utils/use-locale-switch.js +58 -0
- package/dist/src/adapters/next/server/get-i18n-context.d.ts +9 -0
- package/dist/src/adapters/next/server/get-i18n-context.js +47 -0
- package/dist/src/adapters/next/server/get-translator.d.ts +30 -0
- package/dist/src/adapters/next/server/get-translator.js +19 -0
- package/dist/src/adapters/next/server/index.d.ts +2 -0
- package/dist/src/adapters/next/shared/constants/pathname-header-name.d.ts +1 -0
- package/dist/src/adapters/next/shared/constants/pathname-header-name.js +4 -0
- package/dist/src/adapters/next/shared/utils/locale-prefix-pathname.d.ts +16 -0
- package/dist/src/adapters/next/shared/utils/locale-prefix-pathname.js +33 -0
- package/dist/src/adapters/next/shared/utils/localize-pathname.d.ts +18 -0
- package/dist/src/adapters/next/shared/utils/localize-pathname.js +36 -0
- package/dist/src/client/react/contexts/config/context.d.ts +3 -0
- package/dist/src/client/react/contexts/config/context.js +6 -0
- package/dist/src/client/react/contexts/config/hook.d.ts +2 -0
- package/dist/src/client/react/contexts/config/hook.js +12 -0
- package/dist/src/client/react/contexts/config/index.d.ts +2 -0
- package/dist/src/client/react/contexts/config/provider.d.ts +2 -0
- package/dist/src/client/react/contexts/config/provider.js +11 -0
- package/dist/src/client/react/contexts/config/types.d.ts +10 -0
- package/dist/src/client/react/contexts/intor-provider/index.d.ts +2 -0
- package/dist/src/client/react/contexts/intor-provider/intor-provider.d.ts +2 -0
- package/dist/src/client/react/contexts/intor-provider/intor-provider.js +17 -0
- package/dist/src/client/react/contexts/intor-provider/types.d.ts +14 -0
- package/dist/src/client/react/contexts/locale/context.d.ts +3 -0
- package/dist/src/client/react/contexts/locale/context.js +6 -0
- package/dist/src/client/react/contexts/locale/hook.d.ts +1 -0
- package/dist/src/client/react/contexts/locale/hook.js +12 -0
- package/dist/src/client/react/contexts/locale/index.d.ts +2 -0
- package/dist/src/client/react/contexts/locale/provider.d.ts +3 -0
- package/dist/src/client/react/contexts/locale/provider.js +48 -0
- package/dist/src/client/react/contexts/locale/types.d.ts +13 -0
- package/dist/src/client/react/contexts/locale/utils/change-locale.d.ts +22 -0
- package/dist/src/client/react/contexts/locale/utils/change-locale.js +35 -0
- package/dist/src/client/react/contexts/locale/utils/use-init-locale-cookie.d.ts +8 -0
- package/dist/src/client/react/contexts/locale/utils/use-init-locale-cookie.js +28 -0
- package/dist/src/client/react/contexts/messages/context.d.ts +3 -0
- package/dist/src/client/react/contexts/messages/context.js +6 -0
- package/dist/src/client/react/contexts/messages/hook.d.ts +2 -0
- package/dist/src/client/react/contexts/messages/hook.js +12 -0
- package/dist/src/client/react/contexts/messages/index.d.ts +2 -0
- package/dist/src/client/react/contexts/messages/provider.d.ts +3 -0
- package/dist/src/client/react/contexts/messages/provider.js +32 -0
- package/dist/src/client/react/contexts/messages/types.d.ts +15 -0
- package/dist/src/client/react/contexts/messages/utils/use-refetch-messages.d.ts +16 -0
- package/dist/src/client/react/contexts/messages/utils/use-refetch-messages.js +57 -0
- package/dist/src/client/react/contexts/translate-handlers/context.d.ts +2 -0
- package/dist/src/client/react/contexts/translate-handlers/context.js +6 -0
- package/dist/src/client/react/contexts/translate-handlers/hook.d.ts +2 -0
- package/dist/src/client/react/contexts/translate-handlers/hook.js +10 -0
- package/dist/src/client/react/contexts/translate-handlers/index.d.ts +3 -0
- package/dist/src/client/react/contexts/translate-handlers/provider.d.ts +2 -0
- package/dist/src/client/react/contexts/translate-handlers/provider.js +11 -0
- package/dist/src/client/react/contexts/translate-handlers/types.d.ts +7 -0
- package/dist/src/client/react/contexts/translator/context.d.ts +3 -0
- package/dist/src/client/react/contexts/translator/context.js +6 -0
- package/dist/src/client/react/contexts/translator/hook.d.ts +2 -0
- package/dist/src/client/react/contexts/translator/hook.js +12 -0
- package/dist/src/client/react/contexts/translator/index.d.ts +2 -0
- package/dist/src/client/react/contexts/translator/provider.d.ts +2 -0
- package/dist/src/client/react/contexts/translator/provider.js +53 -0
- package/dist/src/client/react/contexts/translator/types.d.ts +11 -0
- package/dist/src/client/react/hooks/use-translator.d.ts +12 -0
- package/dist/src/client/react/hooks/use-translator.js +38 -0
- package/dist/src/client/react/index.d.ts +4 -0
- package/dist/src/client/shared/utils/get-initial-locale.d.ts +11 -0
- package/dist/src/client/shared/utils/get-initial-locale.js +21 -0
- package/dist/src/config/constants/cache.constants.d.ts +2 -0
- package/dist/src/config/constants/cache.constants.js +7 -0
- package/dist/src/config/constants/cookie.constants.d.ts +2 -0
- package/dist/src/config/constants/cookie.constants.js +14 -0
- package/dist/src/config/constants/routing.constants.d.ts +2 -0
- package/dist/src/config/constants/routing.constants.js +11 -0
- package/dist/src/config/define-intor-config.d.ts +2 -0
- package/dist/src/config/define-intor-config.js +34 -0
- package/dist/src/config/index.d.ts +5 -0
- package/dist/src/config/resolvers/resolve-cache-options.d.ts +2 -0
- package/dist/src/config/resolvers/resolve-cache-options.js +10 -0
- package/dist/src/config/resolvers/resolve-cookie-options.d.ts +11 -0
- package/dist/src/config/resolvers/resolve-cookie-options.js +19 -0
- package/dist/src/config/resolvers/resolve-fallback-locales.d.ts +3 -0
- package/dist/src/config/resolvers/resolve-fallback-locales.js +33 -0
- package/dist/src/config/resolvers/resolve-routing-options.d.ts +13 -0
- package/dist/src/config/resolvers/resolve-routing-options.js +28 -0
- package/dist/src/config/types/cache.types.d.ts +7 -0
- package/dist/src/config/types/cookie.types.d.ts +23 -0
- package/dist/src/config/types/intor-config.types.d.ts +41 -0
- package/dist/src/config/types/loader.types.d.ts +38 -0
- package/dist/src/config/types/logger.types.d.ts +7 -0
- package/dist/src/config/types/routing.types.d.ts +15 -0
- package/dist/src/config/types/translator.types.d.ts +4 -0
- package/dist/src/config/validators/validate-default-locale.d.ts +3 -0
- package/dist/src/config/validators/validate-default-locale.js +24 -0
- package/dist/src/config/validators/validate-supported-locales.d.ts +7 -0
- package/dist/src/config/validators/validate-supported-locales.js +22 -0
- package/dist/src/server/index.d.ts +4 -0
- package/dist/src/server/intor/index.d.ts +2 -0
- package/dist/src/server/intor/intor.d.ts +14 -0
- package/dist/src/server/intor/intor.js +67 -0
- package/dist/src/server/intor/types.d.ts +14 -0
- package/dist/src/server/messages/index.d.ts +7 -0
- package/dist/src/server/messages/load-local-messages/index.d.ts +2 -0
- package/dist/src/server/messages/load-local-messages/load-local-messages.d.ts +11 -0
- package/dist/src/server/messages/load-local-messages/load-local-messages.js +85 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/collect-file-entries/collect-file-entries.d.ts +29 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/collect-file-entries/collect-file-entries.js +95 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/collect-file-entries/index.d.ts +1 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/collect-file-entries/types.d.ts +15 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/index.d.ts +2 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/index.d.ts +1 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/parse-file-entries.d.ts +34 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/parse-file-entries.js +85 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/types.d.ts +18 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/utils/json-reader.d.ts +6 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/utils/json-reader.js +12 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/utils/nest-object-from-path.d.ts +14 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/parse-file-entries/utils/nest-object-from-path.js +21 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/read-locale-messages.d.ts +10 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/read-locale-messages.js +31 -0
- package/dist/src/server/messages/load-local-messages/read-locale-messages/types.d.ts +23 -0
- package/dist/src/server/messages/load-local-messages/types.d.ts +21 -0
- package/dist/src/server/messages/load-messages.d.ts +11 -0
- package/dist/src/server/messages/load-messages.js +77 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.d.ts +9 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/fetch-locale-messages.js +50 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/index.d.ts +1 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/types.d.ts +13 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.d.ts +5 -0
- package/dist/src/server/messages/load-remote-messages/fetch-locale-messages/utils/build-search-params.js +25 -0
- package/dist/src/server/messages/load-remote-messages/index.d.ts +2 -0
- package/dist/src/server/messages/load-remote-messages/load-remote-messages.d.ts +9 -0
- package/dist/src/server/messages/load-remote-messages/load-remote-messages.js +79 -0
- package/dist/src/server/messages/load-remote-messages/types.d.ts +20 -0
- package/dist/src/server/messages/shared/global-messages-pool.d.ts +15 -0
- package/dist/src/server/messages/shared/global-messages-pool.js +10 -0
- package/dist/src/server/messages/shared/types.d.ts +39 -0
- package/dist/src/server/messages/shared/utils/is-valid-messages.d.ts +15 -0
- package/dist/src/server/messages/shared/utils/is-valid-messages.js +36 -0
- package/dist/src/server/messages/types.d.ts +15 -0
- package/dist/src/server/shared/logger/get-logger.d.ts +11 -0
- package/dist/src/server/shared/logger/get-logger.js +34 -0
- package/dist/src/server/shared/logger/global-logger-pool.d.ts +15 -0
- package/dist/src/server/shared/logger/global-logger-pool.js +8 -0
- package/dist/src/server/translator/get-translator.d.ts +33 -0
- package/dist/src/server/translator/get-translator.js +39 -0
- package/dist/src/server/translator/index.d.ts +1 -0
- package/dist/src/shared/constants/prefix-placeholder.d.ts +1 -0
- package/dist/src/shared/constants/prefix-placeholder.js +4 -0
- package/dist/src/shared/error/index.d.ts +1 -0
- package/dist/src/shared/error/intor-error.d.ts +16 -0
- package/dist/src/shared/error/intor-error.js +21 -0
- package/dist/src/shared/types/generated.types.d.ts +30 -0
- package/dist/src/shared/types/translator-instance.types.d.ts +33 -0
- package/dist/src/shared/utils/client/build-cookie-string.d.ts +9 -0
- package/dist/src/shared/utils/client/build-cookie-string.js +33 -0
- package/dist/src/shared/utils/client/set-locale-cookie-browser.d.ts +13 -0
- package/dist/src/shared/utils/client/set-locale-cookie-browser.js +21 -0
- package/dist/src/shared/utils/index.d.ts +8 -0
- package/dist/src/shared/utils/locale/normalize-locale.d.ts +4 -0
- package/dist/src/shared/utils/locale/normalize-locale.js +42 -0
- package/dist/src/shared/utils/locale/resolve-preferred-locale.d.ts +5 -0
- package/dist/src/shared/utils/locale/resolve-preferred-locale.js +26 -0
- package/dist/src/shared/utils/merge-messages.d.ts +6 -0
- package/dist/src/shared/utils/merge-messages.js +13 -0
- package/dist/src/shared/utils/normalize-cache-key.d.ts +3 -0
- package/dist/src/shared/utils/normalize-cache-key.js +29 -0
- package/dist/src/shared/utils/pathname/extract-pathname.d.ts +28 -0
- package/dist/src/shared/utils/pathname/extract-pathname.js +58 -0
- package/dist/src/shared/utils/pathname/normalize-pathname.d.ts +12 -0
- package/dist/src/shared/utils/pathname/normalize-pathname.js +43 -0
- package/dist/src/shared/utils/pathname/standardize-pathname.d.ts +18 -0
- package/dist/src/shared/utils/pathname/standardize-pathname.js +30 -0
- package/dist/src/shared/utils/resolve-namespaces.d.ts +10 -0
- package/dist/src/shared/utils/resolve-namespaces.js +33 -0
- package/package.json +28 -30
- package/dist/config/index.cjs +0 -198
- package/dist/config/index.d.cts +0 -145
- package/dist/config/index.d.ts +0 -145
- package/dist/config/index.js +0 -193
- package/dist/index.cjs +0 -234
- package/dist/index.d.cts +0 -224
- package/dist/index.d.ts +0 -224
- package/dist/index.js +0 -214
- package/dist/next/index.cjs +0 -940
- package/dist/next/index.d.cts +0 -282
- package/dist/next/index.d.ts +0 -282
- package/dist/next/index.js +0 -907
- package/dist/next/middleware/index.cjs +0 -387
- package/dist/next/middleware/index.d.cts +0 -128
- package/dist/next/middleware/index.d.ts +0 -128
- package/dist/next/middleware/index.js +0 -384
- package/dist/next/server/index.cjs +0 -734
- package/dist/next/server/index.d.cts +0 -277
- package/dist/next/server/index.d.ts +0 -277
- package/dist/next/server/index.js +0 -723
- package/dist/react/index.cjs +0 -649
- package/dist/react/index.d.cts +0 -224
- package/dist/react/index.d.ts +0 -224
- package/dist/react/index.js +0 -620
- package/dist/server/index.cjs +0 -696
- package/dist/server/index.d.cts +0 -377
- package/dist/server/index.d.ts +0 -377
- package/dist/server/index.js +0 -680
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { usePathname as usePathname$1 } from 'next/navigation';
|
|
2
|
+
import { localizePathname } from '../shared/utils/localize-pathname.js';
|
|
3
|
+
import 'react/jsx-runtime';
|
|
4
|
+
import 'react';
|
|
5
|
+
import '../../../client/react/contexts/config/context.js';
|
|
6
|
+
import { useConfig } from '../../../client/react/contexts/config/hook.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import '../../../config/constants/cache.constants.js';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
import '../../../client/react/contexts/messages/context.js';
|
|
16
|
+
import '../../../client/react/contexts/locale/context.js';
|
|
17
|
+
import { useLocale } from '../../../client/react/contexts/locale/hook.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom hook to get the current pathname in different forms based on the active locale.
|
|
21
|
+
*
|
|
22
|
+
* This hook wraps Next.js `usePathname` and processes the pathname according to the app's
|
|
23
|
+
* locale configuration.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const { localizedPathname, standardizedPathname, unprefixedPathname } = usePathname();
|
|
27
|
+
* console.log(localizedPathname); // e.g. "/en/about"
|
|
28
|
+
* console.log(standardizedPathname); // e.g. "/{locale}/about"
|
|
29
|
+
* console.log(unprefixedPathname); // e.g. "/about"
|
|
30
|
+
*/
|
|
31
|
+
const usePathname = () => {
|
|
32
|
+
const { config } = useConfig();
|
|
33
|
+
const { locale } = useLocale();
|
|
34
|
+
// Get the raw pathname from the Next.js usePathname hook
|
|
35
|
+
const rawPathname = usePathname$1();
|
|
36
|
+
// Generate the locale-prefixed pathname
|
|
37
|
+
const { localePrefixedPathname, standardizedPathname, unprefixedPathname } = localizePathname({
|
|
38
|
+
config,
|
|
39
|
+
pathname: rawPathname,
|
|
40
|
+
locale,
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
localizedPathname: localePrefixedPathname,
|
|
44
|
+
standardizedPathname,
|
|
45
|
+
unprefixedPathname,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { usePathname };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GenLocale } from "../../../shared/types/generated.types";
|
|
2
|
+
import type { Locale } from "intor-translator";
|
|
3
|
+
import type { NavigateOptions } from "next/dist/shared/lib/app-router-context.shared-runtime";
|
|
4
|
+
/**
|
|
5
|
+
* useRouter hook.
|
|
6
|
+
*
|
|
7
|
+
* Wraps Next.js useRouter and provides push/replace methods that automatically switch locale.
|
|
8
|
+
*/
|
|
9
|
+
export declare const useRouter: () => {
|
|
10
|
+
back(): void;
|
|
11
|
+
forward(): void;
|
|
12
|
+
refresh(): void;
|
|
13
|
+
prefetch(href: string, options?: import("next/dist/shared/lib/app-router-context.shared-runtime").PrefetchOptions): void;
|
|
14
|
+
push: (href: string, options?: NavigateOptions & {
|
|
15
|
+
locale?: GenLocale;
|
|
16
|
+
}) => void;
|
|
17
|
+
replace: (href: string, options?: NavigateOptions & {
|
|
18
|
+
locale?: Locale;
|
|
19
|
+
}) => void;
|
|
20
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useRouter as useRouter$1 } from 'next/navigation';
|
|
2
|
+
import { useLocaleSwitch } from './utils/use-locale-switch.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* useRouter hook.
|
|
6
|
+
*
|
|
7
|
+
* Wraps Next.js useRouter and provides push/replace methods that automatically switch locale.
|
|
8
|
+
*/
|
|
9
|
+
const useRouter = () => {
|
|
10
|
+
const { push, replace, ...rest } = useRouter$1();
|
|
11
|
+
const { switchLocale } = useLocaleSwitch();
|
|
12
|
+
// Push with locale
|
|
13
|
+
const pushWithLocale = (href, options) => {
|
|
14
|
+
const resolvedHref = switchLocale({ href, locale: options?.locale });
|
|
15
|
+
if (resolvedHref)
|
|
16
|
+
push(resolvedHref, options);
|
|
17
|
+
};
|
|
18
|
+
// Replace with locale
|
|
19
|
+
const replaceWithLocale = (href, options) => {
|
|
20
|
+
const resolvedHref = switchLocale({ href, locale: options?.locale });
|
|
21
|
+
if (resolvedHref)
|
|
22
|
+
replace(resolvedHref, options);
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
push: pushWithLocale,
|
|
26
|
+
replace: replaceWithLocale,
|
|
27
|
+
...rest,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { useRouter };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config";
|
|
2
|
+
export declare const shouldFullReload: ({ config, targetPathname, locale, currentLocale, }: {
|
|
3
|
+
config: IntorResolvedConfig;
|
|
4
|
+
targetPathname: string;
|
|
5
|
+
locale?: string;
|
|
6
|
+
currentLocale: string;
|
|
7
|
+
}) => boolean;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
import { extractPathname } from '../../../../shared/utils/pathname/extract-pathname.js';
|
|
3
|
+
|
|
4
|
+
const shouldFullReload = ({ config, targetPathname, locale, currentLocale, }) => {
|
|
5
|
+
const loader = config.loader;
|
|
6
|
+
if (!loader || !loader.type)
|
|
7
|
+
return false;
|
|
8
|
+
if (loader.type === "remote" && !loader.fullReload)
|
|
9
|
+
return false;
|
|
10
|
+
const { maybeLocale, isLocalePrefixed } = extractPathname({
|
|
11
|
+
config,
|
|
12
|
+
pathname: targetPathname,
|
|
13
|
+
});
|
|
14
|
+
const isDifferentLocale = (locale && locale !== currentLocale) ||
|
|
15
|
+
(isLocalePrefixed && maybeLocale !== currentLocale);
|
|
16
|
+
return isDifferentLocale ? true : false;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { shouldFullReload };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Locale } from "intor-translator";
|
|
2
|
+
export declare const useLocaleSwitch: () => {
|
|
3
|
+
resolveHref: ({ href, locale, }: {
|
|
4
|
+
href?: string;
|
|
5
|
+
locale?: Locale;
|
|
6
|
+
}) => {
|
|
7
|
+
resolvedHref: string;
|
|
8
|
+
isExternal: boolean;
|
|
9
|
+
targetLocale: string;
|
|
10
|
+
targetPathname: string;
|
|
11
|
+
};
|
|
12
|
+
switchLocale: ({ href, locale, }: {
|
|
13
|
+
href?: string;
|
|
14
|
+
locale?: Locale;
|
|
15
|
+
}) => string | undefined;
|
|
16
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { usePathname } from '../use-pathname.js';
|
|
2
|
+
import { shouldFullReload } from './should-full-reload.js';
|
|
3
|
+
import { localizePathname } from '../../shared/utils/localize-pathname.js';
|
|
4
|
+
import 'react/jsx-runtime';
|
|
5
|
+
import 'react';
|
|
6
|
+
import '../../../../client/react/contexts/config/context.js';
|
|
7
|
+
import { useConfig } from '../../../../client/react/contexts/config/hook.js';
|
|
8
|
+
import { setLocaleCookieBrowser } from '../../../../shared/utils/client/set-locale-cookie-browser.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import '../../../../config/constants/cache.constants.js';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
import '../../../../client/react/contexts/messages/context.js';
|
|
18
|
+
import '../../../../client/react/contexts/locale/context.js';
|
|
19
|
+
import { useLocale } from '../../../../client/react/contexts/locale/hook.js';
|
|
20
|
+
|
|
21
|
+
const useLocaleSwitch = () => {
|
|
22
|
+
const { config } = useConfig();
|
|
23
|
+
const { locale: currentLocale, setLocale } = useLocale();
|
|
24
|
+
const { localizedPathname } = usePathname();
|
|
25
|
+
// Resolve href
|
|
26
|
+
const resolveHref = ({ href, locale, }) => {
|
|
27
|
+
const isLocaleValid = locale && config.supportedLocales?.includes(locale);
|
|
28
|
+
const targetLocale = isLocaleValid ? locale : currentLocale;
|
|
29
|
+
const targetPathname = href ?? localizedPathname;
|
|
30
|
+
const isExternal = targetPathname.startsWith("http");
|
|
31
|
+
const resolvedHref = !isExternal
|
|
32
|
+
? localizePathname({
|
|
33
|
+
config,
|
|
34
|
+
pathname: targetPathname,
|
|
35
|
+
locale: targetLocale,
|
|
36
|
+
}).localePrefixedPathname
|
|
37
|
+
: targetPathname;
|
|
38
|
+
return { resolvedHref, isExternal, targetLocale, targetPathname };
|
|
39
|
+
};
|
|
40
|
+
// Switch locale
|
|
41
|
+
const switchLocale = ({ href, locale, }) => {
|
|
42
|
+
const { resolvedHref, isExternal, targetLocale, targetPathname } = resolveHref({ href, locale });
|
|
43
|
+
if (isExternal)
|
|
44
|
+
return;
|
|
45
|
+
if (shouldFullReload({ config, targetPathname, locale, currentLocale })) {
|
|
46
|
+
setLocaleCookieBrowser({ cookie: config.cookie, locale: targetLocale });
|
|
47
|
+
globalThis.location.href = resolvedHref; // Full reload navigation
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
setLocale(targetLocale);
|
|
52
|
+
}
|
|
53
|
+
return resolvedHref;
|
|
54
|
+
};
|
|
55
|
+
return { resolveHref, switchLocale };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { useLocaleSwitch };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../config/types/intor-config.types";
|
|
2
|
+
import type { I18nContext } from "../../../server/intor/types";
|
|
3
|
+
import type { GenConfigKeys } from "../../../shared/types/generated.types";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves the i18n context for the current request.
|
|
6
|
+
*
|
|
7
|
+
* Next.js adapter implementation: uses `next/headers` and `next/cookies`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const getI18nContext: <CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig) => Promise<I18nContext>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { cookies, headers } from 'next/headers';
|
|
2
|
+
import { PATHNAME_HEADER_NAME } from '../shared/constants/pathname-header-name.js';
|
|
3
|
+
import { getLogger } from '../../../server/shared/logger/get-logger.js';
|
|
4
|
+
import 'lodash.merge';
|
|
5
|
+
import { normalizeLocale } from '../../../shared/utils/locale/normalize-locale.js';
|
|
6
|
+
import { resolvePreferredLocale } from '../../../shared/utils/locale/resolve-preferred-locale.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the i18n context for the current request.
|
|
10
|
+
*
|
|
11
|
+
* Next.js adapter implementation: uses `next/headers` and `next/cookies`.
|
|
12
|
+
*/
|
|
13
|
+
const getI18nContext = async (config) => {
|
|
14
|
+
const baseLogger = getLogger({ id: config.id, ...config.logger });
|
|
15
|
+
const logger = baseLogger.child({ scope: "next-adapter" });
|
|
16
|
+
const cookiesStore = await cookies();
|
|
17
|
+
const headersStore = await headers();
|
|
18
|
+
const { defaultLocale, supportedLocales = [], cookie, routing } = config;
|
|
19
|
+
let locale;
|
|
20
|
+
// Locale from cookie (if cookie is enabled)
|
|
21
|
+
if (cookie.enabled) {
|
|
22
|
+
const localeFromCookie = cookiesStore.get(cookie.name)?.value;
|
|
23
|
+
locale = normalizeLocale(localeFromCookie, supportedLocales);
|
|
24
|
+
if (locale) {
|
|
25
|
+
logger.trace("Locale retrieved from cookie.", { locale });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//====== ▼ Locale from cookie not found ======
|
|
29
|
+
// Locale source set to "browser", retrieve from Accept-Language header
|
|
30
|
+
if (!locale && routing.firstVisit.localeSource === "browser") {
|
|
31
|
+
const aLHeader = headersStore.get("accept-language") || undefined;
|
|
32
|
+
const preferredLocale = resolvePreferredLocale(aLHeader, supportedLocales);
|
|
33
|
+
locale = normalizeLocale(preferredLocale, supportedLocales);
|
|
34
|
+
logger.trace("Locale retrieved from header.", { locale });
|
|
35
|
+
}
|
|
36
|
+
// Retrieve pathname from headers (next/headers)
|
|
37
|
+
const pathname = headersStore.get(PATHNAME_HEADER_NAME);
|
|
38
|
+
if (pathname) {
|
|
39
|
+
logger.trace("Pathname retrieved from header.", { pathname });
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
locale: (locale || defaultLocale),
|
|
43
|
+
pathname: pathname || "",
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { getI18nContext };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../config/types/intor-config.types";
|
|
2
|
+
import type { MessagesReader } from "../../../server";
|
|
3
|
+
import type { GenConfigKeys, GenMessages, IfGen } from "../../../shared/types/generated.types";
|
|
4
|
+
import type { TranslatorInstance } from "../../../shared/types/translator-instance.types";
|
|
5
|
+
import type { LocalizedNodeKeys, TranslateHandlers } from "intor-translator";
|
|
6
|
+
/**
|
|
7
|
+
* Create a translator instance ready for the current Next.js SSR environment.
|
|
8
|
+
*
|
|
9
|
+
* - **Automatically resolves the current locale and pathname using the Next.js adapter.**
|
|
10
|
+
* - Loads messages using the provided config, locale, and pathname.
|
|
11
|
+
* - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
|
|
12
|
+
* - Supports optional `preKey` to create a scoped translator for nested keys.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getTranslator<CK extends GenConfigKeys = "__default__">(options: {
|
|
15
|
+
config: IntorResolvedConfig;
|
|
16
|
+
handlers?: TranslateHandlers;
|
|
17
|
+
extraOptions?: {
|
|
18
|
+
exts?: string[];
|
|
19
|
+
messagesReader?: MessagesReader;
|
|
20
|
+
};
|
|
21
|
+
}): Promise<TranslatorInstance<GenMessages<CK>>>;
|
|
22
|
+
export declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(options: {
|
|
23
|
+
config: IntorResolvedConfig;
|
|
24
|
+
handlers?: TranslateHandlers;
|
|
25
|
+
extraOptions?: {
|
|
26
|
+
exts?: string[];
|
|
27
|
+
messagesReader?: MessagesReader;
|
|
28
|
+
};
|
|
29
|
+
preKey: IfGen<PK, string>;
|
|
30
|
+
}): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getI18nContext } from './get-i18n-context.js';
|
|
2
|
+
import { getTranslator as getTranslator$1 } from '../../../server/translator/get-translator.js';
|
|
3
|
+
|
|
4
|
+
// Implementation
|
|
5
|
+
async function getTranslator(options) {
|
|
6
|
+
const { config, preKey, handlers, extraOptions } = options;
|
|
7
|
+
const { locale, pathname } = await getI18nContext(config);
|
|
8
|
+
const translatorInstance = getTranslator$1({
|
|
9
|
+
config,
|
|
10
|
+
locale,
|
|
11
|
+
pathname,
|
|
12
|
+
handlers,
|
|
13
|
+
extraOptions,
|
|
14
|
+
preKey,
|
|
15
|
+
});
|
|
16
|
+
return translatorInstance;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { getTranslator };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PATHNAME_HEADER_NAME = "x-intor-pathname";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
interface LocalePrefixPathnameOptions {
|
|
3
|
+
pathname: string;
|
|
4
|
+
config: IntorResolvedConfig;
|
|
5
|
+
locale?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Converts a standardized pathname by applying the locale prefix according to the configured strategy.
|
|
9
|
+
*
|
|
10
|
+
* Based on the `routing.prefix` setting in the config:
|
|
11
|
+
* - "all": Always add the locale prefix.
|
|
12
|
+
* - "except-default": Add the locale prefix unless the locale is the default.
|
|
13
|
+
* - "none": Remove the locale prefix entirely.
|
|
14
|
+
*/
|
|
15
|
+
export declare const localePrefixPathname: ({ config, pathname: standardizedPathname, locale, }: LocalePrefixPathnameOptions) => string;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PREFIX_PLACEHOLDER } from '../../../../shared/constants/prefix-placeholder.js';
|
|
2
|
+
import 'lodash.merge';
|
|
3
|
+
import { normalizePathname } from '../../../../shared/utils/pathname/normalize-pathname.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Converts a standardized pathname by applying the locale prefix according to the configured strategy.
|
|
7
|
+
*
|
|
8
|
+
* Based on the `routing.prefix` setting in the config:
|
|
9
|
+
* - "all": Always add the locale prefix.
|
|
10
|
+
* - "except-default": Add the locale prefix unless the locale is the default.
|
|
11
|
+
* - "none": Remove the locale prefix entirely.
|
|
12
|
+
*/
|
|
13
|
+
const localePrefixPathname = ({ config, pathname: standardizedPathname, locale, }) => {
|
|
14
|
+
const { routing } = config;
|
|
15
|
+
const { prefix } = routing;
|
|
16
|
+
if (prefix !== "none" && !locale) {
|
|
17
|
+
throw new Error('No locale when using prefix "all", "except-default"');
|
|
18
|
+
}
|
|
19
|
+
// context: "all"
|
|
20
|
+
if (prefix === "all") {
|
|
21
|
+
return normalizePathname(standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale));
|
|
22
|
+
}
|
|
23
|
+
// context: "except-default"
|
|
24
|
+
if (prefix === "except-default") {
|
|
25
|
+
return locale === config.defaultLocale
|
|
26
|
+
? normalizePathname(standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, ""))
|
|
27
|
+
: normalizePathname(standardizedPathname.replaceAll(PREFIX_PLACEHOLDER, locale));
|
|
28
|
+
}
|
|
29
|
+
// context: "none"
|
|
30
|
+
return normalizePathname(standardizedPathname.replaceAll(`/${PREFIX_PLACEHOLDER}`, ""));
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { localePrefixPathname };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
interface LocalizePathnameOptions {
|
|
3
|
+
config: IntorResolvedConfig;
|
|
4
|
+
pathname: string;
|
|
5
|
+
locale?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Localizes a given pathname by:
|
|
9
|
+
* 1. Removing the basePath and locale prefix (if necessary) using `extractPathname`.
|
|
10
|
+
* 2. Standardizing the pathname by appending basePath and the prefixPlaceholder using `standardizedPathname`.
|
|
11
|
+
* 3. Adding the correct locale prefix back to the standardized pathname using `localePrefixPathname`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const localizePathname: ({ config, pathname: rawPathname, locale, }: LocalizePathnameOptions) => {
|
|
14
|
+
unprefixedPathname: string;
|
|
15
|
+
standardizedPathname: string;
|
|
16
|
+
localePrefixedPathname: string;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { localePrefixPathname } from './locale-prefix-pathname.js';
|
|
2
|
+
import 'lodash.merge';
|
|
3
|
+
import { standardizePathname } from '../../../../shared/utils/pathname/standardize-pathname.js';
|
|
4
|
+
import { extractPathname } from '../../../../shared/utils/pathname/extract-pathname.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Localizes a given pathname by:
|
|
8
|
+
* 1. Removing the basePath and locale prefix (if necessary) using `extractPathname`.
|
|
9
|
+
* 2. Standardizing the pathname by appending basePath and the prefixPlaceholder using `standardizedPathname`.
|
|
10
|
+
* 3. Adding the correct locale prefix back to the standardized pathname using `localePrefixPathname`.
|
|
11
|
+
*/
|
|
12
|
+
const localizePathname = ({ config, pathname: rawPathname, locale, }) => {
|
|
13
|
+
// Remove the locale prefix if necessary
|
|
14
|
+
const { unprefixedPathname } = extractPathname({
|
|
15
|
+
config,
|
|
16
|
+
pathname: rawPathname,
|
|
17
|
+
});
|
|
18
|
+
// Standardize pathname
|
|
19
|
+
const standardizedPathname = standardizePathname({
|
|
20
|
+
config,
|
|
21
|
+
pathname: unprefixedPathname,
|
|
22
|
+
});
|
|
23
|
+
// Add locale prefix to pathname
|
|
24
|
+
const localePrefixedPathname = localePrefixPathname({
|
|
25
|
+
config,
|
|
26
|
+
pathname: standardizedPathname,
|
|
27
|
+
locale,
|
|
28
|
+
});
|
|
29
|
+
return {
|
|
30
|
+
unprefixedPathname,
|
|
31
|
+
standardizedPathname,
|
|
32
|
+
localePrefixedPathname,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { localizePathname };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ConfigContext } from './context.js';
|
|
3
|
+
|
|
4
|
+
// hook
|
|
5
|
+
function useConfig() {
|
|
6
|
+
const context = React.useContext(ConfigContext);
|
|
7
|
+
if (!context)
|
|
8
|
+
throw new Error("useConfig must be used within ConfigProvider");
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { useConfig };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { ConfigContext } from './context.js';
|
|
5
|
+
|
|
6
|
+
function ConfigProvider({ value: { config, pathname }, children, }) {
|
|
7
|
+
const value = React.useMemo(() => ({ config, pathname }), [config, pathname]);
|
|
8
|
+
return (jsx(ConfigContext.Provider, { value: value, children: children }));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { ConfigProvider };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
import type * as React from "react";
|
|
3
|
+
export type ConfigContextValue = {
|
|
4
|
+
config: IntorResolvedConfig;
|
|
5
|
+
pathname: string;
|
|
6
|
+
};
|
|
7
|
+
export type ConfigProviderProps = {
|
|
8
|
+
value: ConfigContextValue;
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import 'react';
|
|
4
|
+
import { ConfigProvider } from '../config/provider.js';
|
|
5
|
+
import '../config/context.js';
|
|
6
|
+
import { LocaleProvider } from '../locale/provider.js';
|
|
7
|
+
import '../locale/context.js';
|
|
8
|
+
import { MessagesProvider } from '../messages/provider.js';
|
|
9
|
+
import '../messages/context.js';
|
|
10
|
+
import { TranslatorProvider } from '../translator/provider.js';
|
|
11
|
+
import '../translator/context.js';
|
|
12
|
+
|
|
13
|
+
const IntorProvider = ({ value: { config, pathname = "", initialLocale, messages = config.messages, onLocaleChange, isLoading, }, children, }) => {
|
|
14
|
+
return (jsx(ConfigProvider, { value: { config, pathname }, children: jsx(MessagesProvider, { value: { messages }, children: jsx(LocaleProvider, { value: { initialLocale, onLocaleChange }, children: jsx(TranslatorProvider, { value: { isLoading }, children: children }) }) }) }));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { IntorProvider };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
import type * as React from "react";
|
|
3
|
+
import { type Locale, type LocaleMessages } from "intor-translator";
|
|
4
|
+
export interface IntorProviderProps {
|
|
5
|
+
value: {
|
|
6
|
+
config: IntorResolvedConfig;
|
|
7
|
+
initialLocale: Locale;
|
|
8
|
+
pathname?: string;
|
|
9
|
+
messages?: Readonly<LocaleMessages>;
|
|
10
|
+
onLocaleChange?: (newLocale: string) => Promise<void> | void;
|
|
11
|
+
isLoading?: boolean;
|
|
12
|
+
};
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLocale(): import("./types").LocaleContextValue;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { LocaleContext } from './context.js';
|
|
3
|
+
|
|
4
|
+
// hook
|
|
5
|
+
function useLocale() {
|
|
6
|
+
const context = React.useContext(LocaleContext);
|
|
7
|
+
if (!context)
|
|
8
|
+
throw new Error("useLocale must be used within a LocaleProvider");
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { useLocale };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import '../config/context.js';
|
|
5
|
+
import { useConfig } from '../config/hook.js';
|
|
6
|
+
import { useInitLocaleCookie } from './utils/use-init-locale-cookie.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import '../../../../config/constants/cache.constants.js';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
import '../messages/context.js';
|
|
16
|
+
import { useMessages } from '../messages/hook.js';
|
|
17
|
+
import { LocaleContext } from './context.js';
|
|
18
|
+
import { changeLocale } from './utils/change-locale.js';
|
|
19
|
+
|
|
20
|
+
// provider
|
|
21
|
+
function LocaleProvider({ value: { initialLocale, onLocaleChange }, children, }) {
|
|
22
|
+
const { config } = useConfig();
|
|
23
|
+
const { refetchMessages } = useMessages();
|
|
24
|
+
const { loader, cookie } = config;
|
|
25
|
+
// Current locale
|
|
26
|
+
const [currentLocale, setCurrentLocale] = React.useState(initialLocale);
|
|
27
|
+
useInitLocaleCookie({ config, locale: initialLocale }); // Hook: Initialize cookie (If cookie not exist yet)
|
|
28
|
+
// Change locale and set cookie (If using dynamic api: refetch messages)
|
|
29
|
+
const setLocale = React.useCallback(async (newLocale) => {
|
|
30
|
+
changeLocale({
|
|
31
|
+
currentLocale,
|
|
32
|
+
newLocale,
|
|
33
|
+
loader,
|
|
34
|
+
cookie,
|
|
35
|
+
setLocale: setCurrentLocale,
|
|
36
|
+
refetchMessages,
|
|
37
|
+
});
|
|
38
|
+
onLocaleChange?.(newLocale);
|
|
39
|
+
}, [currentLocale, loader, cookie, refetchMessages, onLocaleChange]);
|
|
40
|
+
// context value
|
|
41
|
+
const value = React.useMemo(() => ({
|
|
42
|
+
locale: currentLocale,
|
|
43
|
+
setLocale,
|
|
44
|
+
}), [currentLocale, setLocale]);
|
|
45
|
+
return (jsx(LocaleContext.Provider, { value: value, children: children }));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { LocaleProvider };
|