astro-react-i18next 0.1.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +138 -0
- package/dist/middleware-server.d.ts +2 -0
- package/dist/middleware-server.js +47 -0
- package/dist/middleware-static.d.ts +2 -0
- package/dist/middleware-static.js +23 -0
- package/dist/utils.d.ts +12 -0
- package/dist/utils.js +49 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 jeremyxgo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# astro-react-i18next
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro";
|
|
2
|
+
interface AstroReactI18nextOptions {
|
|
3
|
+
defaultLocale?: string;
|
|
4
|
+
locales?: string[];
|
|
5
|
+
defaultNamespace?: string;
|
|
6
|
+
namespaces?: string[];
|
|
7
|
+
prefixDefaultLocale?: boolean;
|
|
8
|
+
localesDir?: string;
|
|
9
|
+
}
|
|
10
|
+
type MergedAstroReactI18nextOptions = {
|
|
11
|
+
[key in keyof AstroReactI18nextOptions]-?: AstroReactI18nextOptions[key];
|
|
12
|
+
};
|
|
13
|
+
declare module "i18next" {
|
|
14
|
+
interface InitOptions {
|
|
15
|
+
astroReactI18next: MergedAstroReactI18nextOptions;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export default function AstroReactI18nextIntegration(options: AstroReactI18nextOptions): AstroIntegration;
|
|
19
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
var INTEGRATION_NAME = "astro-react-i18next";
|
|
4
|
+
var DEFAULT_OPTIONS = {
|
|
5
|
+
defaultLocale: "en-US",
|
|
6
|
+
locales: ["en-US"],
|
|
7
|
+
defaultNamespace: "common",
|
|
8
|
+
namespaces: ["common"],
|
|
9
|
+
prefixDefaultLocale: false,
|
|
10
|
+
localesDir: "locales"
|
|
11
|
+
};
|
|
12
|
+
function buildI18nextInitScript({
|
|
13
|
+
backendType,
|
|
14
|
+
basePath,
|
|
15
|
+
options
|
|
16
|
+
}) {
|
|
17
|
+
const mergedOptions = {
|
|
18
|
+
...DEFAULT_OPTIONS,
|
|
19
|
+
...options
|
|
20
|
+
};
|
|
21
|
+
let imports = "";
|
|
22
|
+
let i18nextPlugins = "";
|
|
23
|
+
let i18nextOptions = "";
|
|
24
|
+
if (backendType === "fs") {
|
|
25
|
+
imports = `
|
|
26
|
+
import Backend from "i18next-fs-backend";
|
|
27
|
+
`;
|
|
28
|
+
i18nextOptions = `
|
|
29
|
+
initImmediate: false,
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
if (backendType === "http") {
|
|
33
|
+
imports = `
|
|
34
|
+
import Backend from "i18next-http-backend";
|
|
35
|
+
import LanguageDetector from "i18next-browser-languagedetector";
|
|
36
|
+
`;
|
|
37
|
+
i18nextPlugins = `
|
|
38
|
+
.use(LanguageDetector)
|
|
39
|
+
`;
|
|
40
|
+
i18nextOptions = `
|
|
41
|
+
detection: {
|
|
42
|
+
caches: ["cookie", "localStorage"],
|
|
43
|
+
order: ["cookie", "localStorage"],
|
|
44
|
+
lookupCookie: 'i18next',
|
|
45
|
+
lookupLocalStorage: 'i18next',
|
|
46
|
+
},
|
|
47
|
+
`;
|
|
48
|
+
}
|
|
49
|
+
return `
|
|
50
|
+
import i18n from "i18next";
|
|
51
|
+
import { initReactI18next } from "react-i18next";
|
|
52
|
+
${imports}
|
|
53
|
+
|
|
54
|
+
i18n
|
|
55
|
+
.use(Backend)
|
|
56
|
+
.use(initReactI18next)
|
|
57
|
+
${i18nextPlugins}
|
|
58
|
+
.init({
|
|
59
|
+
fallbackLng: "${mergedOptions.defaultLocale}",
|
|
60
|
+
supportedLngs: ${JSON.stringify(mergedOptions.locales)},
|
|
61
|
+
defaultNS: "${mergedOptions.defaultNamespace}",
|
|
62
|
+
ns: ${JSON.stringify(mergedOptions.namespaces)},
|
|
63
|
+
interpolation: {
|
|
64
|
+
escapeValue: false,
|
|
65
|
+
},
|
|
66
|
+
backend: {
|
|
67
|
+
loadPath: "${path.join(basePath, mergedOptions.localesDir)}/{{lng}}/{{ns}}.json",
|
|
68
|
+
},
|
|
69
|
+
astroReactI18next: ${JSON.stringify(mergedOptions)},
|
|
70
|
+
${i18nextOptions}
|
|
71
|
+
});
|
|
72
|
+
`;
|
|
73
|
+
}
|
|
74
|
+
function AstroReactI18nextIntegration(options) {
|
|
75
|
+
const mergedOptions = {
|
|
76
|
+
...DEFAULT_OPTIONS,
|
|
77
|
+
...options
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
name: INTEGRATION_NAME,
|
|
81
|
+
hooks: {
|
|
82
|
+
"astro:config:setup": ({
|
|
83
|
+
config,
|
|
84
|
+
addMiddleware,
|
|
85
|
+
injectScript,
|
|
86
|
+
updateConfig
|
|
87
|
+
}) => {
|
|
88
|
+
const clientLocaleRestorationScript = `
|
|
89
|
+
window.addEventListener("DOMContentLoaded", () => {
|
|
90
|
+
const defaultLocale = "${mergedOptions.defaultLocale}";
|
|
91
|
+
const locales = ${JSON.stringify(mergedOptions.locales)};
|
|
92
|
+
let detectedLocale = window.location.pathname.split("/")[1];
|
|
93
|
+
|
|
94
|
+
if (!locales.includes(detectedLocale)) {
|
|
95
|
+
detectedLocale = defaultLocale;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
i18n.changeLanguage(detectedLocale);
|
|
99
|
+
});
|
|
100
|
+
`;
|
|
101
|
+
const clientI18nextInitScript = buildI18nextInitScript({
|
|
102
|
+
backendType: "http",
|
|
103
|
+
basePath: "/",
|
|
104
|
+
options
|
|
105
|
+
});
|
|
106
|
+
const serverI18nextInitScript = buildI18nextInitScript({
|
|
107
|
+
backendType: "fs",
|
|
108
|
+
basePath: config.publicDir.pathname,
|
|
109
|
+
options
|
|
110
|
+
});
|
|
111
|
+
const middlewareEntrypoint = config.output === "server" ? `${INTEGRATION_NAME}/middleware/server` : `${INTEGRATION_NAME}/middleware/static`;
|
|
112
|
+
addMiddleware({
|
|
113
|
+
entrypoint: middlewareEntrypoint,
|
|
114
|
+
order: "post"
|
|
115
|
+
});
|
|
116
|
+
injectScript("page-ssr", serverI18nextInitScript);
|
|
117
|
+
injectScript("before-hydration", clientI18nextInitScript);
|
|
118
|
+
injectScript("before-hydration", clientLocaleRestorationScript);
|
|
119
|
+
injectScript("page", clientI18nextInitScript);
|
|
120
|
+
injectScript("page", clientLocaleRestorationScript);
|
|
121
|
+
updateConfig({
|
|
122
|
+
i18n: {
|
|
123
|
+
locales: mergedOptions.locales,
|
|
124
|
+
defaultLocale: mergedOptions.defaultLocale,
|
|
125
|
+
routing: {
|
|
126
|
+
prefixDefaultLocale: false,
|
|
127
|
+
redirectToDefaultLocale: false,
|
|
128
|
+
fallbackType: "redirect"
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
AstroReactI18nextIntegration as default
|
|
138
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/middleware-server.ts
|
|
2
|
+
import i18n2 from "i18next";
|
|
3
|
+
|
|
4
|
+
// src/utils.ts
|
|
5
|
+
import i18n from "i18next";
|
|
6
|
+
function getLocaleConfig() {
|
|
7
|
+
const { defaultLocale, locales, prefixDefaultLocale } = i18n.options.astroReactI18next;
|
|
8
|
+
return { defaultLocale, locales, prefixDefaultLocale };
|
|
9
|
+
}
|
|
10
|
+
function getLocalizedPathname(pathname = "", locale = "") {
|
|
11
|
+
const { defaultLocale, locales, prefixDefaultLocale } = getLocaleConfig();
|
|
12
|
+
const localeFromPathname = pathname.split("/")[1];
|
|
13
|
+
let localizedPathname = pathname;
|
|
14
|
+
if (locales.includes(localeFromPathname)) {
|
|
15
|
+
localizedPathname = localizedPathname.replace("/" + localeFromPathname, "") || "/";
|
|
16
|
+
}
|
|
17
|
+
if (locales.includes(locale) && (locale !== defaultLocale || prefixDefaultLocale)) {
|
|
18
|
+
localizedPathname = "/" + locale + localizedPathname.replace(/^\/$/, "");
|
|
19
|
+
}
|
|
20
|
+
return localizedPathname;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/middleware-server.ts
|
|
24
|
+
async function onRequest(context, next) {
|
|
25
|
+
const { defaultLocale, locales } = getLocaleConfig();
|
|
26
|
+
const localeFromPathname = context.url.pathname.split("/")[1];
|
|
27
|
+
const localeFromCookie = context.cookies.get("i18next")?.value;
|
|
28
|
+
const localeFromHeader = context.preferredLocale;
|
|
29
|
+
const nextLocale = [
|
|
30
|
+
localeFromPathname,
|
|
31
|
+
localeFromCookie,
|
|
32
|
+
localeFromHeader,
|
|
33
|
+
defaultLocale
|
|
34
|
+
].find((locale) => locale && locales.includes(locale));
|
|
35
|
+
await i18n2.changeLanguage(nextLocale);
|
|
36
|
+
context.cookies.set("i18next", nextLocale || "", { path: "/" });
|
|
37
|
+
const { hash, origin, pathname, search } = context.url;
|
|
38
|
+
const nextPathname = getLocalizedPathname(pathname, nextLocale);
|
|
39
|
+
if (nextPathname !== pathname) {
|
|
40
|
+
const nextUrl = origin + nextPathname + search + hash;
|
|
41
|
+
return context.redirect(nextUrl);
|
|
42
|
+
}
|
|
43
|
+
return next();
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
onRequest
|
|
47
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/middleware-static.ts
|
|
2
|
+
import i18n2 from "i18next";
|
|
3
|
+
|
|
4
|
+
// src/utils.ts
|
|
5
|
+
import i18n from "i18next";
|
|
6
|
+
function getLocaleConfig() {
|
|
7
|
+
const { defaultLocale, locales, prefixDefaultLocale } = i18n.options.astroReactI18next;
|
|
8
|
+
return { defaultLocale, locales, prefixDefaultLocale };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/middleware-static.ts
|
|
12
|
+
async function onRequest(context, next) {
|
|
13
|
+
const { defaultLocale, locales } = getLocaleConfig();
|
|
14
|
+
const localeFromPathname = context.url.pathname.split("/")[1];
|
|
15
|
+
const nextLocale = [localeFromPathname, defaultLocale].find(
|
|
16
|
+
(locale) => locale && locales.includes(locale)
|
|
17
|
+
);
|
|
18
|
+
await i18n2.changeLanguage(nextLocale);
|
|
19
|
+
return next();
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
onRequest
|
|
23
|
+
};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function getLocaleConfig(): {
|
|
2
|
+
defaultLocale: string;
|
|
3
|
+
locales: string[];
|
|
4
|
+
prefixDefaultLocale: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare function getLocalizedPathname(pathname?: string, locale?: string): string;
|
|
7
|
+
export declare function buildStaticPaths(): {
|
|
8
|
+
params: {
|
|
9
|
+
locale: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
}[];
|
|
12
|
+
export declare function changeLocale(nextLocale?: string, shallow?: boolean): void;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import i18n from "i18next";
|
|
3
|
+
function getLocaleConfig() {
|
|
4
|
+
const { defaultLocale, locales, prefixDefaultLocale } = i18n.options.astroReactI18next;
|
|
5
|
+
return { defaultLocale, locales, prefixDefaultLocale };
|
|
6
|
+
}
|
|
7
|
+
function getLocalizedPathname(pathname = "", locale = "") {
|
|
8
|
+
const { defaultLocale, locales, prefixDefaultLocale } = getLocaleConfig();
|
|
9
|
+
const localeFromPathname = pathname.split("/")[1];
|
|
10
|
+
let localizedPathname = pathname;
|
|
11
|
+
if (locales.includes(localeFromPathname)) {
|
|
12
|
+
localizedPathname = localizedPathname.replace("/" + localeFromPathname, "") || "/";
|
|
13
|
+
}
|
|
14
|
+
if (locales.includes(locale) && (locale !== defaultLocale || prefixDefaultLocale)) {
|
|
15
|
+
localizedPathname = "/" + locale + localizedPathname.replace(/^\/$/, "");
|
|
16
|
+
}
|
|
17
|
+
return localizedPathname;
|
|
18
|
+
}
|
|
19
|
+
function buildStaticPaths() {
|
|
20
|
+
const { defaultLocale, locales, prefixDefaultLocale } = getLocaleConfig();
|
|
21
|
+
return locales.map((locale) => ({
|
|
22
|
+
params: {
|
|
23
|
+
locale: locale !== defaultLocale || prefixDefaultLocale ? locale : void 0
|
|
24
|
+
}
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
function changeLocale(nextLocale = "", shallow = true) {
|
|
28
|
+
const { locales } = getLocaleConfig();
|
|
29
|
+
if (!locales.includes(nextLocale)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
i18n.changeLanguage(nextLocale);
|
|
33
|
+
const { hash, pathname, search } = window.location;
|
|
34
|
+
const nextPathname = getLocalizedPathname(pathname, nextLocale);
|
|
35
|
+
if (nextPathname !== pathname) {
|
|
36
|
+
const nextUrl = nextPathname + search + hash;
|
|
37
|
+
if (shallow) {
|
|
38
|
+
window.history.replaceState(null, "", nextUrl);
|
|
39
|
+
} else {
|
|
40
|
+
window.location.replace(nextUrl);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
buildStaticPaths,
|
|
46
|
+
changeLocale,
|
|
47
|
+
getLocaleConfig,
|
|
48
|
+
getLocalizedPathname
|
|
49
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astro-react-i18next",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Integrates i18next and react-i18next seamlessly into your Astro website to provide robust i18n support for React components.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"astro-integration",
|
|
7
|
+
"withastro"
|
|
8
|
+
],
|
|
9
|
+
"author": "jeremyxgo",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/jeremyxgo/astro-react-i18next.git"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/index.js",
|
|
21
|
+
"./middleware/server": "./dist/middleware-server.js",
|
|
22
|
+
"./middleware/static": "./dist/middleware-static.js",
|
|
23
|
+
"./utils": "./dist/utils.js"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,mjs,astro,json,md}\"",
|
|
27
|
+
"lint": "eslint --fix --max-warnings 0 \"**/*.{js,jsx,ts,tsx,mjs,astro}\"",
|
|
28
|
+
"build": "node ./build.js && tsc",
|
|
29
|
+
"prepare": "husky"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"i18next": "^23.15.1",
|
|
33
|
+
"i18next-browser-languagedetector": "^8.0.0",
|
|
34
|
+
"i18next-fs-backend": "^2.3.2",
|
|
35
|
+
"i18next-http-backend": "^2.6.1",
|
|
36
|
+
"react-i18next": "^15.0.2"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^22.7.4",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^8.7.0",
|
|
41
|
+
"@typescript-eslint/parser": "^8.7.0",
|
|
42
|
+
"astro": "^4.15.9",
|
|
43
|
+
"astro-eslint-parser": "^1.0.3",
|
|
44
|
+
"esbuild": "^0.24.0",
|
|
45
|
+
"eslint": "^8.57.1",
|
|
46
|
+
"eslint-config-prettier": "^9.1.0",
|
|
47
|
+
"eslint-plugin-astro": "^1.2.4",
|
|
48
|
+
"eslint-plugin-import": "^2.30.0",
|
|
49
|
+
"eslint-plugin-react": "^7.37.0",
|
|
50
|
+
"husky": "^9.1.6",
|
|
51
|
+
"lint-staged": "^15.2.10",
|
|
52
|
+
"prettier": "^3.3.3",
|
|
53
|
+
"prettier-plugin-astro": "^0.14.1",
|
|
54
|
+
"typescript": "^5.5.4"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": "^18.17.1 || ^20.3.0 || >=21.0.0"
|
|
58
|
+
}
|
|
59
|
+
}
|