@better-i18n/use-intl 0.1.7 → 0.1.8
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/package.json +2 -2
- package/src/middleware/index.ts +43 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-i18n/use-intl",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Better i18n integration for use-intl (React, TanStack Start)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"clean": "rm -rf dist"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@better-i18n/core": "0.1.
|
|
47
|
+
"@better-i18n/core": "0.1.8"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"react": ">=18.0.0",
|
package/src/middleware/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { detectLocale, getLocales } from "@better-i18n/core";
|
|
|
5
5
|
import type { I18nMiddlewareConfig } from "@better-i18n/core";
|
|
6
6
|
|
|
7
7
|
export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
8
|
-
const { project, defaultLocale, detection = {} } = config;
|
|
8
|
+
const { project, defaultLocale, localePrefix = "as-needed", detection = {} } = config;
|
|
9
9
|
|
|
10
10
|
const {
|
|
11
11
|
cookie = true,
|
|
@@ -27,7 +27,9 @@ export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
|
27
27
|
|
|
28
28
|
// 2. Extract locale indicators
|
|
29
29
|
const url = new URL(request.url);
|
|
30
|
-
const
|
|
30
|
+
const pathSegment = url.pathname.split("/")[1];
|
|
31
|
+
const hasLocaleInPath =
|
|
32
|
+
!!pathSegment && availableLocales.includes(pathSegment);
|
|
31
33
|
|
|
32
34
|
// Dynamic imports for TanStack Start server functions to avoid bundling them in client
|
|
33
35
|
const {
|
|
@@ -54,13 +56,49 @@ export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
|
54
56
|
const result = detectLocale({
|
|
55
57
|
project,
|
|
56
58
|
defaultLocale,
|
|
57
|
-
pathLocale,
|
|
59
|
+
pathLocale: pathSegment,
|
|
58
60
|
cookieLocale,
|
|
59
61
|
headerLocale,
|
|
60
62
|
availableLocales,
|
|
61
63
|
});
|
|
62
64
|
|
|
63
|
-
// 4.
|
|
65
|
+
// 4. Redirect if locale prefix is needed but missing
|
|
66
|
+
// Skip API routes and paths that already have a locale prefix
|
|
67
|
+
const isApiRoute = url.pathname.startsWith("/api/");
|
|
68
|
+
if (
|
|
69
|
+
localePrefix !== "never" &&
|
|
70
|
+
!hasLocaleInPath &&
|
|
71
|
+
!isApiRoute &&
|
|
72
|
+
result.detectedFrom !== "path"
|
|
73
|
+
) {
|
|
74
|
+
const shouldRedirect =
|
|
75
|
+
localePrefix === "always" ||
|
|
76
|
+
(localePrefix === "as-needed" && result.locale !== defaultLocale);
|
|
77
|
+
|
|
78
|
+
if (shouldRedirect) {
|
|
79
|
+
const redirectUrl = new URL(
|
|
80
|
+
`/${result.locale}${url.pathname}`,
|
|
81
|
+
url.origin,
|
|
82
|
+
);
|
|
83
|
+
redirectUrl.search = url.search;
|
|
84
|
+
|
|
85
|
+
// Build redirect response with locale cookie
|
|
86
|
+
const headers = new Headers({
|
|
87
|
+
Location: redirectUrl.toString(),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (cookie && result.shouldSetCookie) {
|
|
91
|
+
headers.set(
|
|
92
|
+
"Set-Cookie",
|
|
93
|
+
`${cookieName}=${result.locale}; Path=/; Max-Age=${cookieMaxAge}; SameSite=Lax`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return new Response(null, { status: 302, headers });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 5. Set cookie if needed (non-redirect path)
|
|
64
102
|
if (cookie && result.shouldSetCookie) {
|
|
65
103
|
setCookie(cookieName, result.locale, {
|
|
66
104
|
path: "/",
|
|
@@ -69,7 +107,7 @@ export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
|
69
107
|
});
|
|
70
108
|
}
|
|
71
109
|
|
|
72
|
-
//
|
|
110
|
+
// 6. Pass locale to route context
|
|
73
111
|
return next({ context: { locale: result.locale } });
|
|
74
112
|
},
|
|
75
113
|
);
|