astro-react-i18next 0.1.0 → 0.1.2

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 CHANGED
@@ -1 +1,199 @@
1
1
  # astro-react-i18next
2
+
3
+ Integrates i18next and react-i18next seamlessly into your Astro website to provide robust i18n support for React components.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/astro-react-i18next?style=flat-square)](https://www.npmjs.com/package/astro-react-i18next)
6
+ ![GitHub License](https://img.shields.io/github/license/jeremyxgo/astro-react-i18next?style=flat-square)
7
+
8
+ ## Examples
9
+
10
+ | Example | |
11
+ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
12
+ | [SSG](https://github.com/jeremyxgo/astro-react-i18next/tree/main/examples/ssg) | [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz_small.svg)](https://stackblitz.com/github/jeremyxgo/astro-react-i18next/tree/main/examples/ssg?startScript=dev) |
13
+ | [SSR](https://github.com/jeremyxgo/astro-react-i18next/tree/main/examples/ssr) | [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz_small.svg)](https://stackblitz.com/github/jeremyxgo/astro-react-i18next/tree/main/examples/ssr?startScript=dev) |
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npx astro add astro-react-i18next
19
+ ```
20
+
21
+ Follow the prompts to install the necessary dependencies and add the required configuration to your Astro project.
22
+
23
+ You will see the following changes in your `astro.config.mjs` file:
24
+
25
+ ```diff
26
+ import { defineConfig } from "astro/config";
27
+ import reactI18next from "astro-react-i18next";
28
+
29
+ export default defineConfig({
30
+ // ...
31
+ integrations: [
32
+ // ...
33
+ + reactI18next(),
34
+ ],
35
+ });
36
+ ```
37
+
38
+ ## Configuration
39
+
40
+ The initialization function accepts an optional configuration object with the following options:
41
+
42
+ | Option | Type | Description | Default |
43
+ | --------------------- | ---------- | ---------------------------------------------------------------------------------- | ------------ |
44
+ | `defaultLocale` | `string` | The default locale to use when no locale is specified. | `"en-US"` |
45
+ | `locales` | `string[]` | An array of locales to support. | `["en-US"]` |
46
+ | `defaultNamespace` | `string` | The default namespace to use when no namespace is specified. | `"common"` |
47
+ | `namespaces` | `string[]` | An array of namespaces to support. | `["common"]` |
48
+ | `prefixDefaultLocale` | `boolean` | Whether to prefix the default locale with the locale code. | `false` |
49
+ | `localesDir` | `string` | The directory where the locale files are stored, relative to the public directory. | `"locales"` |
50
+
51
+ Here is an example of how to configure the integration:
52
+
53
+ ```diff
54
+ import { defineConfig } from "astro/config";
55
+ import reactI18next from "astro-react-i18next";
56
+
57
+ export default defineConfig({
58
+ // ...
59
+ integrations: [
60
+ // ...
61
+ - reactI18next(),
62
+ + reactI18next({
63
+ + defaultLocale: "en-US",
64
+ + locales: ["en-US", "fr-FR", "zh-TW"],
65
+ + defaultNamespace: "app",
66
+ + namespaces: ["app"],
67
+ + }),
68
+ ],
69
+ });
70
+ ```
71
+
72
+ It also supports `Server (SSR) Mode`, such as using the `@astrojs/node` adapter:
73
+
74
+ ```diff
75
+ import { defineConfig } from "astro/config";
76
+ +import node from '@astrojs/node';
77
+ import reactI18next from "astro-react-i18next";
78
+
79
+ export default defineConfig({
80
+ // ...
81
+ integrations: [
82
+ // ...
83
+ reactI18next({
84
+ defaultLocale: "en-US",
85
+ locales: ["en-US", "fr-FR", "zh-TW"],
86
+ defaultNamespace: "app",
87
+ namespaces: ["app"],
88
+ }),
89
+ ],
90
+ + output: "server",
91
+ + adapter: node({
92
+ + mode: "standalone",
93
+ + }),
94
+ });
95
+ ```
96
+
97
+ ## Locale Resources
98
+
99
+ Create locale files for each locale and namespace in the `localesDir` directory.
100
+
101
+ For example, create the following files:
102
+
103
+ ```text
104
+ /
105
+ ├── public/
106
+ │ └── locales/
107
+ │ ├── en-US/
108
+ │ │ └── common.json
109
+ │ ├── fr-FR/
110
+ │ │ └── common.json
111
+ │ └── zh-TW/
112
+ │ └── common.json
113
+ ├── src/
114
+ └── package.json
115
+ ```
116
+
117
+ The content of the `locales/en-US/common.json` file should look like this:
118
+
119
+ ```json
120
+ {
121
+ "hello_world": "Hello, World!"
122
+ }
123
+ ```
124
+
125
+ ## Dynamic Routes for Locales
126
+
127
+ To manage dynamic routes for each locale, create a root route named `[...locale]` in the `src` directory.
128
+
129
+ ```text
130
+ /
131
+ ├── public/
132
+ ├── src/
133
+ │ └── [...locale]/
134
+ │ ├── ...
135
+ │ └── index.jsx
136
+ └── package.json
137
+ ```
138
+
139
+ ## Static Paths for Locales
140
+
141
+ If you're using `Static (SSG) Mode`, static paths are required. You can easily generate them by using the `buildStaticPaths` utility function provided by this integration.
142
+
143
+ ```js
144
+ ---
145
+ import { buildStaticPaths } from "astro-react-i18next/utils";
146
+
147
+ export function getStaticPaths() {
148
+ return buildStaticPaths();
149
+ }
150
+ ---
151
+
152
+ <html>
153
+ ...
154
+ </html>
155
+ ```
156
+
157
+ ## Translating Content in Astro Components
158
+
159
+ Use the `i18next` instance to translate content in your Astro components.
160
+
161
+ ```js
162
+ ---
163
+ import i18n from "i18next";
164
+ ---
165
+
166
+ <html lang={i18n.language}>
167
+ <p>{i18n.t("hello_world")}</p>
168
+ </html>
169
+ ```
170
+
171
+ ## Translating Content in React Components
172
+
173
+ Use the `useTranslation` hook to translate content in your React components.
174
+
175
+ ```js
176
+ import { useTranslation } from "react-i18next";
177
+
178
+ export function MyComponent() {
179
+ const { t } = useTranslation();
180
+ return <p>{t("hello_world")}</p>;
181
+ }
182
+ ```
183
+
184
+ ## Utilities
185
+
186
+ The integration provides utility functions to help manage locales and translations.
187
+
188
+ All utility functions are available in the `astro-react-i18next/utils` module.
189
+
190
+ | Function | Description | Returns |
191
+ | -------------------------------------------------- | -------------------------------------------------------- | ---------------------------------------------------------------------------- |
192
+ | `getLocaleConfig()` | Returns the locale configuration object. | `{ defaultLocale: string, locales: string[], prefixDefaultLocale: boolean }` |
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. | |
196
+
197
+ ## License
198
+
199
+ Licensed under the [MIT License](https://github.com/jeremyxgo/astro-react-i18next/blob/main/LICENSE).
package/dist/index.d.ts CHANGED
@@ -15,5 +15,5 @@ declare module "i18next" {
15
15
  astroReactI18next: MergedAstroReactI18nextOptions;
16
16
  }
17
17
  }
18
- export default function AstroReactI18nextIntegration(options: AstroReactI18nextOptions): AstroIntegration;
18
+ export default function AstroReactI18nextIntegration(options?: AstroReactI18nextOptions): AstroIntegration;
19
19
  export {};
@@ -34,10 +34,10 @@ async function onRequest(context, next) {
34
34
  ].find((locale) => locale && locales.includes(locale));
35
35
  await i18n2.changeLanguage(nextLocale);
36
36
  context.cookies.set("i18next", nextLocale || "", { path: "/" });
37
- const { hash, origin, pathname, search } = context.url;
37
+ const { hash, pathname, search } = context.url;
38
38
  const nextPathname = getLocalizedPathname(pathname, nextLocale);
39
39
  if (nextPathname !== pathname) {
40
- const nextUrl = origin + nextPathname + search + hash;
40
+ const nextUrl = nextPathname + search + hash;
41
41
  return context.redirect(nextUrl);
42
42
  }
43
43
  return next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-react-i18next",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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",