intor 2.2.14 → 2.3.0
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/proxy/index.d.ts +2 -0
- package/dist/exports/next/proxy/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/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/proxy/handle-prefix/handle-prefix-all.d.ts +12 -0
- package/dist/src/adapters/next/proxy/handle-prefix/handle-prefix-all.js +60 -0
- package/dist/src/adapters/next/proxy/handle-prefix/handle-prefix-except-default.d.ts +16 -0
- package/dist/src/adapters/next/proxy/handle-prefix/handle-prefix-except-default.js +81 -0
- package/dist/src/adapters/next/proxy/handle-prefix/handle-prefix-none.d.ts +10 -0
- package/dist/src/adapters/next/proxy/handle-prefix/handle-prefix-none.js +22 -0
- package/dist/src/adapters/next/proxy/index.d.ts +2 -0
- package/dist/src/adapters/next/proxy/intor-proxy.d.ts +6 -0
- package/dist/src/adapters/next/proxy/intor-proxy.js +22 -0
- package/dist/src/adapters/next/proxy/utils/create-response.d.ts +17 -0
- package/dist/src/adapters/next/proxy/utils/create-response.js +42 -0
- package/dist/src/adapters/next/proxy/utils/determine-initial-locale.d.ts +8 -0
- package/dist/src/adapters/next/proxy/utils/determine-initial-locale.js +28 -0
- package/dist/src/adapters/next/proxy/utils/set-locale-cookie-edge.d.ts +15 -0
- package/dist/src/adapters/next/proxy/utils/set-locale-cookie-edge.js +24 -0
- package/dist/src/adapters/next/proxy/utils/set-pathname-header.d.ts +12 -0
- package/dist/src/adapters/next/proxy/utils/set-pathname-header.js +13 -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 +32 -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,22 @@
|
|
|
1
|
+
import { handlePrefixAll } from './handle-prefix/handle-prefix-all.js';
|
|
2
|
+
import { handlePrefixExceptDefault } from './handle-prefix/handle-prefix-except-default.js';
|
|
3
|
+
import { handlePrefixNone } from './handle-prefix/handle-prefix-none.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handle locale routing based on prefix config
|
|
7
|
+
*/
|
|
8
|
+
async function intorProxy(config, request) {
|
|
9
|
+
const { prefix } = config.routing;
|
|
10
|
+
// ===== Prefix: none =====
|
|
11
|
+
if (prefix === "none") {
|
|
12
|
+
return handlePrefixNone(config, request);
|
|
13
|
+
}
|
|
14
|
+
// ===== Prefix: except-default =====
|
|
15
|
+
if (prefix === "except-default") {
|
|
16
|
+
return await handlePrefixExceptDefault(config, request);
|
|
17
|
+
}
|
|
18
|
+
// ===== Prefix: all =====
|
|
19
|
+
return await handlePrefixAll(config, request);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { intorProxy };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
import type { NextRequest } from "next/server";
|
|
3
|
+
import { NextResponse } from "next/server";
|
|
4
|
+
interface CreateResponseOptions<Req extends NextRequest = NextRequest> {
|
|
5
|
+
request: Req;
|
|
6
|
+
config: IntorResolvedConfig;
|
|
7
|
+
locale?: string;
|
|
8
|
+
responseType?: "next" | "redirect";
|
|
9
|
+
setCookieOptions?: {
|
|
10
|
+
override?: boolean;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a Next.js response with locale handling.
|
|
15
|
+
*/
|
|
16
|
+
export declare const createResponse: <Req extends NextRequest = NextRequest, Res extends NextResponse = NextResponse>({ request, config, locale, responseType, setCookieOptions, }: CreateResponseOptions<Req>) => Response;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { setLocaleCookieEdge } from './set-locale-cookie-edge.js';
|
|
3
|
+
import { setPathnameHeader } from './set-pathname-header.js';
|
|
4
|
+
import { localizePathname } from '../../shared/utils/localize-pathname.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a Next.js response with locale handling.
|
|
8
|
+
*/
|
|
9
|
+
const createResponse = ({ request, config, locale, responseType = "next", setCookieOptions = { override: false }, }) => {
|
|
10
|
+
const { cookie } = config;
|
|
11
|
+
const { override } = setCookieOptions;
|
|
12
|
+
const url = request.nextUrl.clone(); // Clone URL to avoid mutating original
|
|
13
|
+
// Generate locale-prefixed pathname
|
|
14
|
+
const { localePrefixedPathname } = localizePathname({
|
|
15
|
+
config,
|
|
16
|
+
pathname: url.pathname,
|
|
17
|
+
locale,
|
|
18
|
+
});
|
|
19
|
+
url.pathname = localePrefixedPathname;
|
|
20
|
+
// Create response based on the responseType
|
|
21
|
+
const response = responseType === "redirect"
|
|
22
|
+
? NextResponse.redirect(url)
|
|
23
|
+
: NextResponse.next();
|
|
24
|
+
// Set locale cookie if locale is provided
|
|
25
|
+
if (locale) {
|
|
26
|
+
setLocaleCookieEdge({
|
|
27
|
+
request,
|
|
28
|
+
response,
|
|
29
|
+
locale,
|
|
30
|
+
cookie,
|
|
31
|
+
override,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
// Set pathname header
|
|
35
|
+
const finalResponse = setPathnameHeader({
|
|
36
|
+
request,
|
|
37
|
+
response: response,
|
|
38
|
+
});
|
|
39
|
+
return finalResponse;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { createResponse };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IntorResolvedConfig } from "../../../../config/types/intor-config.types";
|
|
2
|
+
/**
|
|
3
|
+
* Determine the initial locale for the user.
|
|
4
|
+
*
|
|
5
|
+
* @param config - The resolved configuration object.
|
|
6
|
+
* @returns The initial locale string.
|
|
7
|
+
*/
|
|
8
|
+
export declare const determineInitialLocale: (config: IntorResolvedConfig) => Promise<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { headers } from 'next/headers';
|
|
2
|
+
import 'lodash.merge';
|
|
3
|
+
import { normalizeLocale } from '../../../../shared/utils/locale/normalize-locale.js';
|
|
4
|
+
import { resolvePreferredLocale } from '../../../../shared/utils/locale/resolve-preferred-locale.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Determine the initial locale for the user.
|
|
8
|
+
*
|
|
9
|
+
* @param config - The resolved configuration object.
|
|
10
|
+
* @returns The initial locale string.
|
|
11
|
+
*/
|
|
12
|
+
const determineInitialLocale = async (config) => {
|
|
13
|
+
const { defaultLocale, supportedLocales, routing } = config;
|
|
14
|
+
let initialLocale = defaultLocale;
|
|
15
|
+
// If locale should be determined from browser on first visit
|
|
16
|
+
if (routing.firstVisit.localeSource === "browser") {
|
|
17
|
+
const headersStore = await headers();
|
|
18
|
+
const acceptLanguageHeader = headersStore.get("accept-language") || undefined;
|
|
19
|
+
// Find the best matching locale from the supported locales
|
|
20
|
+
const preferredLocale = resolvePreferredLocale(acceptLanguageHeader, supportedLocales);
|
|
21
|
+
// Normalize the preferred locale or fallback to default
|
|
22
|
+
initialLocale =
|
|
23
|
+
normalizeLocale(preferredLocale, supportedLocales) || defaultLocale;
|
|
24
|
+
}
|
|
25
|
+
return initialLocale;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { determineInitialLocale };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CookieResolvedOptions } from "../../../../config/types/cookie.types";
|
|
2
|
+
import type { NextRequest, NextResponse } from "next/server";
|
|
3
|
+
interface SetLocaleCookieParams {
|
|
4
|
+
request: NextRequest;
|
|
5
|
+
response: NextResponse;
|
|
6
|
+
cookie: CookieResolvedOptions;
|
|
7
|
+
locale: string;
|
|
8
|
+
override?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Set a locale cookie on the response.
|
|
12
|
+
* - For Next.js proxy.
|
|
13
|
+
*/
|
|
14
|
+
export declare function setLocaleCookieEdge({ request, response, cookie, locale, override, }: SetLocaleCookieParams): void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set a locale cookie on the response.
|
|
3
|
+
* - For Next.js proxy.
|
|
4
|
+
*/
|
|
5
|
+
function setLocaleCookieEdge({ request, response, cookie, locale, override = false, // Default to not override existed cookie
|
|
6
|
+
}) {
|
|
7
|
+
if (!cookie.enabled || !cookie.autoSetCookie)
|
|
8
|
+
return;
|
|
9
|
+
const isCookieExists = request.cookies.has(cookie.name);
|
|
10
|
+
// Cookie already exists and cannot override
|
|
11
|
+
if (isCookieExists && !override)
|
|
12
|
+
return;
|
|
13
|
+
// Set cookie to response
|
|
14
|
+
response.cookies.set(cookie.name, locale, {
|
|
15
|
+
maxAge: cookie.maxAge,
|
|
16
|
+
path: cookie.path,
|
|
17
|
+
...(cookie.domain ? { domain: cookie.domain } : {}),
|
|
18
|
+
secure: cookie.secure,
|
|
19
|
+
httpOnly: cookie.httpOnly,
|
|
20
|
+
sameSite: cookie.sameSite,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { setLocaleCookieEdge };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
interface SetPathnameHeaderOptions<Req extends NextRequest = NextRequest, Res extends NextResponse = NextResponse> {
|
|
3
|
+
request: Req;
|
|
4
|
+
response: Res;
|
|
5
|
+
key?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Set the pathname in the response header.
|
|
9
|
+
* - For Next.js proxy.
|
|
10
|
+
*/
|
|
11
|
+
export declare const setPathnameHeader: <Req extends NextRequest = NextRequest, Res extends NextResponse = NextResponse>({ request, response, key, }: SetPathnameHeaderOptions<Req, Res>) => Response;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PATHNAME_HEADER_NAME } from '../../shared/constants/pathname-header-name.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Set the pathname in the response header.
|
|
5
|
+
* - For Next.js proxy.
|
|
6
|
+
*/
|
|
7
|
+
const setPathnameHeader = ({ request, response, key = PATHNAME_HEADER_NAME, }) => {
|
|
8
|
+
const pathname = request.nextUrl.pathname;
|
|
9
|
+
response.headers.set(key, pathname);
|
|
10
|
+
return response;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { setPathnameHeader };
|
|
@@ -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 };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Locale } from "intor-translator";
|
|
2
|
+
import type * as React from "react";
|
|
3
|
+
export type LocaleContextValue = {
|
|
4
|
+
locale: Locale;
|
|
5
|
+
setLocale: (locale: Locale) => void;
|
|
6
|
+
};
|
|
7
|
+
export type LocaleProviderProps = {
|
|
8
|
+
value: {
|
|
9
|
+
initialLocale: string;
|
|
10
|
+
onLocaleChange?: (newLocale: string) => Promise<void> | void;
|
|
11
|
+
};
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
};
|