astro-react-i18next 0.2.0 → 0.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/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(mergedOptions)},
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
- let detectedLocale = window.location.pathname.split("/")[1];
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
- const { defaultLocale, locales, prefixDefaultLocale, domains } = i18n.options.astroReactI18next;
8
- return {
9
- defaultLocale,
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
- const { defaultLocale, locales, prefixDefaultLocale, domains } = i18n.options.astroReactI18next;
8
- return {
9
- defaultLocale,
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
- export declare function getLocaleConfig(): {
2
- defaultLocale: string;
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
- const { defaultLocale, locales, prefixDefaultLocale, domains } = i18n.options.astroReactI18next;
5
- return {
6
- defaultLocale,
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,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-react-i18next",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
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",