astro-react-i18next 0.2.0 → 0.3.1
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/README.md +7 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +23 -3
- package/dist/middleware-server.js +10 -8
- package/dist/middleware-static.js +3 -7
- package/dist/utils.d.ts +2 -9
- package/dist/utils.js +3 -7
- package/package.json +30 -18
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ The initialization function accepts an optional configuration object with the fo
|
|
|
48
48
|
| `prefixDefaultLocale` | `boolean` | Whether to prefix the default locale with the locale code. | `false` |
|
|
49
49
|
| `localesDir` | `string` | The directory where the locale files are stored, relative to the public directory. | `"locales"` |
|
|
50
50
|
| `domains` | `{ domain: string; defaultLocale: string; }[]` | An array of domains for language selection. | `[]` |
|
|
51
|
+
| `reservedRoutes` | `string[]` | An array of routes excluded from locale handling. | `["/api"]` |
|
|
51
52
|
|
|
52
53
|
Here is an example of how to configure the integration:
|
|
53
54
|
|
|
@@ -63,8 +64,6 @@ export default defineConfig({
|
|
|
63
64
|
+ reactI18next({
|
|
64
65
|
+ defaultLocale: "en-US",
|
|
65
66
|
+ locales: ["en-US", "fr-FR", "zh-TW"],
|
|
66
|
-
+ defaultNamespace: "app",
|
|
67
|
-
+ namespaces: ["app"],
|
|
68
67
|
+ }),
|
|
69
68
|
],
|
|
70
69
|
});
|
|
@@ -84,8 +83,6 @@ export default defineConfig({
|
|
|
84
83
|
reactI18next({
|
|
85
84
|
defaultLocale: "en-US",
|
|
86
85
|
locales: ["en-US", "fr-FR", "zh-TW"],
|
|
87
|
-
defaultNamespace: "app",
|
|
88
|
-
namespaces: ["app"],
|
|
89
86
|
}),
|
|
90
87
|
],
|
|
91
88
|
+ output: "server",
|
|
@@ -190,12 +187,12 @@ The integration provides utility functions to help manage locales and translatio
|
|
|
190
187
|
|
|
191
188
|
All utility functions are available in the `astro-react-i18next/utils` module.
|
|
192
189
|
|
|
193
|
-
| Function | Description | Returns
|
|
194
|
-
| -------------------------------------------------- | -------------------------------------------------------- |
|
|
195
|
-
| `getLocaleConfig()` | Returns the locale configuration object. | `{ defaultLocale: string; locales: string[]; prefixDefaultLocale: boolean; domains: { domain: string; defaultLocale: string; }[]; }` |
|
|
196
|
-
| `getLocalizedPathname(pathname = "", locale = "")` | Returns the localized pathname for the specified locale. | `string`
|
|
197
|
-
| `buildStaticPaths()` | Generates static paths for each locale. | `{ params: { locale: string \| undefined; }; }[]`
|
|
198
|
-
| `changeLocale(nextLocale = "", shallow = true)` | Changes the current locale. |
|
|
190
|
+
| Function | Description | Returns |
|
|
191
|
+
| -------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
192
|
+
| `getLocaleConfig()` | Returns the locale configuration object. | `{ defaultLocale: string; locales: string[]; prefixDefaultLocale: boolean; domains: { domain: string; defaultLocale: string; }[]; reservedRoutes: string[]; }` |
|
|
193
|
+
| `getLocalizedPathname(pathname = "", locale = "")` | Returns the localized pathname for the specified locale. | `string` |
|
|
194
|
+
| `buildStaticPaths()` | Generates static paths for each locale. | `{ params: { locale: string \| undefined; }; }[]` |
|
|
195
|
+
| `changeLocale(nextLocale = "", shallow = true)` | Changes the current locale. | |
|
|
199
196
|
|
|
200
197
|
## Developing locally
|
|
201
198
|
|
package/dist/index.d.ts
CHANGED
|
@@ -10,13 +10,14 @@ interface AstroReactI18nextOptions {
|
|
|
10
10
|
domain: string;
|
|
11
11
|
defaultLocale: string;
|
|
12
12
|
}[];
|
|
13
|
+
reservedRoutes?: string[];
|
|
13
14
|
}
|
|
14
15
|
type MergedAstroReactI18nextOptions = {
|
|
15
16
|
[key in keyof AstroReactI18nextOptions]-?: AstroReactI18nextOptions[key];
|
|
16
17
|
};
|
|
17
18
|
declare module "i18next" {
|
|
18
19
|
interface InitOptions {
|
|
19
|
-
astroReactI18next: MergedAstroReactI18nextOptions
|
|
20
|
+
astroReactI18next: Pick<MergedAstroReactI18nextOptions, "defaultLocale" | "locales" | "prefixDefaultLocale" | "domains" | "reservedRoutes">;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
export default function AstroReactI18nextIntegration(options?: AstroReactI18nextOptions): AstroIntegration;
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,8 @@ var DEFAULT_OPTIONS = {
|
|
|
8
8
|
namespaces: ["common"],
|
|
9
9
|
prefixDefaultLocale: false,
|
|
10
10
|
localesDir: "locales",
|
|
11
|
-
domains: []
|
|
11
|
+
domains: [],
|
|
12
|
+
reservedRoutes: ["/api"]
|
|
12
13
|
};
|
|
13
14
|
function buildI18nextInitScript({
|
|
14
15
|
backendType,
|
|
@@ -63,7 +64,13 @@ function buildI18nextInitScript({
|
|
|
63
64
|
backend: {
|
|
64
65
|
loadPath: "${path.join(basePath, mergedOptions.localesDir || "")}/{{lng}}/{{ns}}.json",
|
|
65
66
|
},
|
|
66
|
-
astroReactI18next: ${JSON.stringify(
|
|
67
|
+
astroReactI18next: ${JSON.stringify({
|
|
68
|
+
defaultLocale: mergedOptions.defaultLocale,
|
|
69
|
+
locales: mergedOptions.locales,
|
|
70
|
+
prefixDefaultLocale: mergedOptions.prefixDefaultLocale,
|
|
71
|
+
domains: mergedOptions.domains,
|
|
72
|
+
reservedRoutes: mergedOptions.reservedRoutes
|
|
73
|
+
})},
|
|
67
74
|
${i18nextOptions}
|
|
68
75
|
});
|
|
69
76
|
`;
|
|
@@ -73,7 +80,20 @@ function buildLocaleRestorationScript(mergedOptions) {
|
|
|
73
80
|
window.addEventListener("DOMContentLoaded", () => {
|
|
74
81
|
const defaultLocale = "${mergedOptions.defaultLocale}";
|
|
75
82
|
const locales = ${JSON.stringify(mergedOptions.locales)};
|
|
76
|
-
|
|
83
|
+
const reservedRoutes = ${JSON.stringify(mergedOptions.reservedRoutes)};
|
|
84
|
+
const pathname = window.location.pathname;
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
reservedRoutes.some(
|
|
88
|
+
(route) =>
|
|
89
|
+
pathname === route ||
|
|
90
|
+
pathname.startsWith(route + "/"),
|
|
91
|
+
)
|
|
92
|
+
) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let detectedLocale = pathname.split("/")[1];
|
|
77
97
|
|
|
78
98
|
if (!locales.includes(detectedLocale)) {
|
|
79
99
|
detectedLocale = defaultLocale;
|
|
@@ -4,13 +4,9 @@ import i18n2 from "i18next";
|
|
|
4
4
|
// src/utils.ts
|
|
5
5
|
import i18n from "i18next";
|
|
6
6
|
function getLocaleConfig() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
locales,
|
|
11
|
-
prefixDefaultLocale,
|
|
12
|
-
domains
|
|
13
|
-
};
|
|
7
|
+
return JSON.parse(
|
|
8
|
+
JSON.stringify(i18n.options.astroReactI18next)
|
|
9
|
+
);
|
|
14
10
|
}
|
|
15
11
|
function getLocalizedPathname(pathname = "", locale = "") {
|
|
16
12
|
const { defaultLocale, locales, prefixDefaultLocale } = getLocaleConfig();
|
|
@@ -26,8 +22,14 @@ function getLocalizedPathname(pathname = "", locale = "") {
|
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
// src/middleware-server.ts
|
|
25
|
+
var ASTRO_RESERVED_ROUTES = ["/_astro", "/_actions", "/_server-islands"];
|
|
29
26
|
async function onRequest(context, next) {
|
|
30
|
-
const { defaultLocale, locales, domains } = getLocaleConfig();
|
|
27
|
+
const { defaultLocale, locales, domains, reservedRoutes } = getLocaleConfig();
|
|
28
|
+
if ([...ASTRO_RESERVED_ROUTES, ...reservedRoutes].some(
|
|
29
|
+
(route) => context.url.pathname === route || context.url.pathname.startsWith(route + "/")
|
|
30
|
+
)) {
|
|
31
|
+
return next();
|
|
32
|
+
}
|
|
31
33
|
const localesByDomain = Object.fromEntries(
|
|
32
34
|
domains.map((domain) => [domain.domain, domain.defaultLocale])
|
|
33
35
|
);
|
|
@@ -4,13 +4,9 @@ import i18n2 from "i18next";
|
|
|
4
4
|
// src/utils.ts
|
|
5
5
|
import i18n from "i18next";
|
|
6
6
|
function getLocaleConfig() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
locales,
|
|
11
|
-
prefixDefaultLocale,
|
|
12
|
-
domains
|
|
13
|
-
};
|
|
7
|
+
return JSON.parse(
|
|
8
|
+
JSON.stringify(i18n.options.astroReactI18next)
|
|
9
|
+
);
|
|
14
10
|
}
|
|
15
11
|
|
|
16
12
|
// src/middleware-static.ts
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
locales: string[];
|
|
4
|
-
prefixDefaultLocale: boolean;
|
|
5
|
-
domains: {
|
|
6
|
-
domain: string;
|
|
7
|
-
defaultLocale: string;
|
|
8
|
-
}[];
|
|
9
|
-
};
|
|
1
|
+
import i18n from "i18next";
|
|
2
|
+
export declare function getLocaleConfig(): typeof i18n.options.astroReactI18next;
|
|
10
3
|
export declare function getLocalizedPathname(pathname?: string, locale?: string): string;
|
|
11
4
|
export declare function buildStaticPaths(): {
|
|
12
5
|
params: {
|
package/dist/utils.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
// src/utils.ts
|
|
2
2
|
import i18n from "i18next";
|
|
3
3
|
function getLocaleConfig() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
locales,
|
|
8
|
-
prefixDefaultLocale,
|
|
9
|
-
domains
|
|
10
|
-
};
|
|
4
|
+
return JSON.parse(
|
|
5
|
+
JSON.stringify(i18n.options.astroReactI18next)
|
|
6
|
+
);
|
|
11
7
|
}
|
|
12
8
|
function getLocalizedPathname(pathname = "", locale = "") {
|
|
13
9
|
const { defaultLocale, locales, prefixDefaultLocale } = getLocaleConfig();
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-react-i18next",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Integrates i18next and react-i18next seamlessly into your Astro website to provide robust i18n support for React components.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro-integration",
|
|
7
7
|
"withastro",
|
|
8
|
+
"seo",
|
|
9
|
+
"l10n",
|
|
8
10
|
"i18n",
|
|
9
11
|
"i18next",
|
|
10
|
-
"react"
|
|
12
|
+
"react",
|
|
13
|
+
"localization",
|
|
14
|
+
"internationalization"
|
|
11
15
|
],
|
|
12
16
|
"author": "jeremyxgo",
|
|
13
17
|
"license": "MIT",
|
|
@@ -32,30 +36,38 @@
|
|
|
32
36
|
"build": "node ./build.js && tsc",
|
|
33
37
|
"prepare": "husky"
|
|
34
38
|
},
|
|
39
|
+
"lint-staged": {
|
|
40
|
+
"*.{js,jsx,ts,tsx,mjs,astro,json,md}": "prettier --write",
|
|
41
|
+
"*.{js,jsx,ts,tsx,mjs,astro}": "eslint --fix --max-warnings 0"
|
|
42
|
+
},
|
|
35
43
|
"dependencies": {
|
|
36
|
-
"i18next": "^24.
|
|
37
|
-
"i18next-browser-languagedetector": "^8.0.
|
|
44
|
+
"i18next": "^24.2.2",
|
|
45
|
+
"i18next-browser-languagedetector": "^8.0.4",
|
|
38
46
|
"i18next-fs-backend": "^2.6.0",
|
|
39
|
-
"i18next-http-backend": "^3.0.
|
|
40
|
-
"react-i18next": "^15.1
|
|
47
|
+
"i18next-http-backend": "^3.0.2",
|
|
48
|
+
"react-i18next": "^15.4.1"
|
|
41
49
|
},
|
|
42
50
|
"devDependencies": {
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"
|
|
47
|
-
"astro
|
|
48
|
-
"esbuild": "^0.
|
|
49
|
-
"eslint": "^
|
|
50
|
-
"eslint-config-
|
|
51
|
+
"@eslint/js": "^9.22.0",
|
|
52
|
+
"@types/node": "^22.13.10",
|
|
53
|
+
"@types/react": "^19.0.10",
|
|
54
|
+
"@types/react-dom": "^19.0.4",
|
|
55
|
+
"astro": "^5.4.2",
|
|
56
|
+
"esbuild": "^0.25.0",
|
|
57
|
+
"eslint": "^9.22.0",
|
|
58
|
+
"eslint-config-flat-gitignore": "^2.1.0",
|
|
59
|
+
"eslint-config-prettier": "^10.1.1",
|
|
51
60
|
"eslint-plugin-astro": "^1.3.1",
|
|
52
61
|
"eslint-plugin-import": "^2.31.0",
|
|
53
|
-
"eslint-plugin-
|
|
62
|
+
"eslint-plugin-perfectionist": "^4.10.0",
|
|
63
|
+
"eslint-plugin-react": "^7.37.4",
|
|
64
|
+
"globals": "^16.0.0",
|
|
54
65
|
"husky": "^9.1.7",
|
|
55
|
-
"lint-staged": "^15.
|
|
56
|
-
"prettier": "^3.
|
|
66
|
+
"lint-staged": "^15.4.3",
|
|
67
|
+
"prettier": "^3.5.3",
|
|
57
68
|
"prettier-plugin-astro": "^0.14.1",
|
|
58
|
-
"typescript": "^5.
|
|
69
|
+
"typescript": "^5.8.2",
|
|
70
|
+
"typescript-eslint": "^8.26.0"
|
|
59
71
|
},
|
|
60
72
|
"engines": {
|
|
61
73
|
"node": "^18.17.1 || ^20.3.0 || >=21.0.0"
|