@modern-js/main-doc 3.1.5 → 3.2.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/docs/en/community/contributing-guide.mdx +1 -1
- package/docs/en/components/international/init-options-desc.mdx +1 -1
- package/docs/en/components/international/install-command.mdx +4 -17
- package/docs/en/components/international/instance-code.mdx +4 -14
- package/docs/en/components/international/introduce.mdx +4 -1
- package/docs/en/configure/app/server/tsconfig-path.mdx +63 -0
- package/docs/en/configure/app/source/enable-async-pre-entry.mdx +30 -0
- package/docs/en/configure/app/tools/dev-server.mdx +0 -4
- package/docs/en/guides/advanced-features/international/_meta.json +0 -1
- package/docs/en/guides/advanced-features/international/advanced.mdx +48 -109
- package/docs/en/guides/advanced-features/international/api.mdx +125 -290
- package/docs/en/guides/advanced-features/international/best-practices.mdx +203 -48
- package/docs/en/guides/advanced-features/international/configuration.mdx +108 -315
- package/docs/en/guides/advanced-features/international/locale-detection.mdx +62 -208
- package/docs/en/guides/advanced-features/international/quick-start.mdx +41 -55
- package/docs/en/guides/advanced-features/international/resource-loading.mdx +63 -322
- package/docs/en/guides/advanced-features/international/routing.mdx +60 -138
- package/docs/en/guides/advanced-features/international.mdx +19 -27
- package/docs/en/guides/basic-features/alias.mdx +1 -1
- package/docs/en/guides/basic-features/html.mdx +2 -2
- package/docs/en/guides/basic-features/static-assets.mdx +1 -2
- package/docs/en/guides/basic-features/testing/_meta.json +1 -1
- package/docs/en/guides/concept/entries.mdx +2 -2
- package/docs/en/guides/get-started/tech-stack.mdx +6 -0
- package/docs/zh/community/contributing-guide.mdx +1 -1
- package/docs/zh/components/international/init-options-desc.mdx +1 -1
- package/docs/zh/components/international/install-command.mdx +4 -16
- package/docs/zh/components/international/instance-code.mdx +4 -14
- package/docs/zh/components/international/introduce.mdx +5 -2
- package/docs/zh/configure/app/server/tsconfig-path.mdx +63 -0
- package/docs/zh/configure/app/source/enable-async-pre-entry.mdx +77 -0
- package/docs/zh/configure/app/tools/dev-server.mdx +0 -4
- package/docs/zh/guides/advanced-features/bff/function.mdx +2 -2
- package/docs/zh/guides/advanced-features/international/_meta.json +0 -1
- package/docs/zh/guides/advanced-features/international/advanced.mdx +48 -109
- package/docs/zh/guides/advanced-features/international/api.mdx +126 -292
- package/docs/zh/guides/advanced-features/international/best-practices.mdx +204 -49
- package/docs/zh/guides/advanced-features/international/configuration.mdx +105 -318
- package/docs/zh/guides/advanced-features/international/locale-detection.mdx +62 -236
- package/docs/zh/guides/advanced-features/international/quick-start.mdx +40 -54
- package/docs/zh/guides/advanced-features/international/resource-loading.mdx +62 -324
- package/docs/zh/guides/advanced-features/international/routing.mdx +58 -136
- package/docs/zh/guides/advanced-features/international.mdx +19 -26
- package/docs/zh/guides/basic-features/alias.mdx +1 -1
- package/docs/zh/guides/basic-features/html.mdx +2 -2
- package/docs/zh/guides/basic-features/static-assets.mdx +1 -2
- package/docs/zh/guides/basic-features/testing/_meta.json +1 -1
- package/docs/zh/guides/concept/entries.mdx +2 -2
- package/docs/zh/guides/get-started/tech-stack.mdx +6 -0
- package/package.json +4 -4
- package/docs/en/components/rspackPrecautions.mdx +0 -6
- package/docs/en/guides/advanced-features/international/basic.mdx +0 -417
- package/docs/en/guides/basic-features/testing/cypress.mdx +0 -95
- package/docs/en/guides/basic-features/testing/jest.mdx +0 -148
- package/docs/en/guides/basic-features/testing/vitest.mdx +0 -100
- package/docs/zh/components/rspackPrecautions.mdx +0 -6
- package/docs/zh/guides/advanced-features/international/basic.mdx +0 -416
- package/docs/zh/guides/basic-features/testing/cypress.mdx +0 -95
- package/docs/zh/guides/basic-features/testing/jest.mdx +0 -148
- package/docs/zh/guides/basic-features/testing/vitest.mdx +0 -100
|
@@ -125,7 +125,7 @@ pnpm run reset
|
|
|
125
125
|
|
|
126
126
|
If you've fixed a bug or added code that should be tested, then add some tests.
|
|
127
127
|
|
|
128
|
-
You can add
|
|
128
|
+
Modern.js uses [Rstest](https://rstest.rs/) for unit tests across the repository. You can add test cases in the `<PACKAGE_DIR>/tests` folder, using Rstest syntax.
|
|
129
129
|
|
|
130
130
|
### Run Unit Tests
|
|
131
131
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
All [i18next initialization options](https://www.i18next.com/overview/configuration-options) are supported. Common options are described below:
|
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
pnpm add @modern-js/plugin-i18n i18next react-i18next
|
|
3
|
-
```
|
|
4
|
-
|
|
5
|
-
:::tip Version Consistency
|
|
6
|
-
Make sure the version of `@modern-js/plugin-i18n` matches the version of `@modern-js/app-tools` in your project. All Modern.js official packages are released with a uniform version number, and version mismatches may cause compatibility issues.
|
|
1
|
+
`i18next` and `react-i18next` are **Peer Dependencies** and must be installed manually.
|
|
7
2
|
|
|
8
|
-
|
|
3
|
+
:::note Version consistency
|
|
4
|
+
`@modern-js/plugin-i18n` should use the same version as `@modern-js/app-tools` in your project.
|
|
5
|
+
:::
|
|
9
6
|
|
|
10
7
|
```bash
|
|
11
|
-
# Check the current version of @modern-js/app-tools
|
|
12
8
|
pnpm list @modern-js/app-tools
|
|
13
|
-
|
|
14
|
-
# Install the same version of @modern-js/plugin-i18n
|
|
15
9
|
pnpm add @modern-js/plugin-i18n@<version> i18next react-i18next
|
|
16
10
|
```
|
|
17
|
-
|
|
18
|
-
:::
|
|
19
|
-
|
|
20
|
-
:::info
|
|
21
|
-
`i18next` and `react-i18next` are peer dependencies and need to be installed manually.
|
|
22
|
-
|
|
23
|
-
:::
|
|
@@ -1,26 +1,16 @@
|
|
|
1
|
-
Create
|
|
1
|
+
Create `src/modern.runtime.ts`:
|
|
2
2
|
|
|
3
3
|
```ts
|
|
4
4
|
import { defineRuntimeConfig } from '@modern-js/runtime';
|
|
5
5
|
import i18next from 'i18next';
|
|
6
6
|
|
|
7
|
-
//
|
|
7
|
+
// i18next exports a global singleton by default, which can cause multiple apps to affect each other.
|
|
8
|
+
// createInstance() creates an isolated instance so each app has independent language state.
|
|
8
9
|
const i18nInstance = i18next.createInstance();
|
|
9
10
|
|
|
10
11
|
export default defineRuntimeConfig({
|
|
11
12
|
i18n: {
|
|
12
|
-
i18nInstance
|
|
13
|
-
initOptions: {
|
|
14
|
-
fallbackLng: 'en',
|
|
15
|
-
supportedLngs: ['zh', 'en'],
|
|
16
|
-
},
|
|
13
|
+
i18nInstance,
|
|
17
14
|
},
|
|
18
15
|
});
|
|
19
16
|
```
|
|
20
|
-
|
|
21
|
-
:::info
|
|
22
|
-
`modern.runtime.ts` is a runtime configuration file used to configure i18next initialization options. Even for the most basic configuration, it's recommended to create this file to ensure the plugin works correctly.
|
|
23
|
-
|
|
24
|
-
It's recommended to use `i18next.createInstance()` to create a new instance instead of directly using the default exported `i18next`. This avoids global state pollution and ensures each application has an independent i18n instance.
|
|
25
|
-
|
|
26
|
-
:::
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
`@modern-js/plugin-i18n` is
|
|
1
|
+
`@modern-js/plugin-i18n` is the internationalization plugin for Modern.js, built on [i18next](https://www.i18next.com/) and [react-i18next](https://react.i18next.com/).
|
|
2
2
|
|
|
3
|
+
- **The plugin itself**: Integrates with the Modern.js framework, including SSR language passing, route prefix handling, and more.
|
|
4
|
+
- **i18next**: Provides core translation capabilities, such as the `t()` function, interpolation, plurals, and namespaces.
|
|
5
|
+
- **react-i18next**: Provides React components and Hooks, such as `useTranslation`, to integrate translations with the React lifecycle.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: tsconfigPath
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# server.tsconfigPath
|
|
6
|
+
|
|
7
|
+
- **Type:** `string`
|
|
8
|
+
- **Default:** `<appDirectory>/tsconfig.json`
|
|
9
|
+
|
|
10
|
+
Specifies the tsconfig file used by server-side TypeScript processes.
|
|
11
|
+
|
|
12
|
+
This option affects:
|
|
13
|
+
|
|
14
|
+
- BFF / API compilation (`@modern-js/plugin-bff`)
|
|
15
|
+
- Custom server compilation (`*.ts` files under `server/`)
|
|
16
|
+
- Runtime ts-node registration, such as SSR, `modern.config.ts`, and `.ts` files under `config/`
|
|
17
|
+
|
|
18
|
+
When this option is not set, `<appDirectory>/tsconfig.json` is used by default.
|
|
19
|
+
|
|
20
|
+
## When to use
|
|
21
|
+
|
|
22
|
+
Use this option when frontend and server-side code need different TypeScript configurations.
|
|
23
|
+
|
|
24
|
+
For example, the frontend can use `moduleResolution: "Bundler"`, while the server side uses `moduleResolution: "NodeNext"` for type resolution of conditional exports or subpath exports.
|
|
25
|
+
|
|
26
|
+
## Example
|
|
27
|
+
|
|
28
|
+
The example below uses `tsconfig.server.json` as the file name. You can use another name in your project.
|
|
29
|
+
|
|
30
|
+
Add `<appDirectory>/tsconfig.server.json`:
|
|
31
|
+
|
|
32
|
+
```jsonc title="tsconfig.server.json"
|
|
33
|
+
{
|
|
34
|
+
"extends": "./tsconfig.json",
|
|
35
|
+
"compilerOptions": {
|
|
36
|
+
"module": "NodeNext",
|
|
37
|
+
"moduleResolution": "NodeNext",
|
|
38
|
+
"target": "ES2022",
|
|
39
|
+
"lib": ["ESNext"],
|
|
40
|
+
"types": ["node"],
|
|
41
|
+
"esModuleInterop": true,
|
|
42
|
+
"experimentalDecorators": true,
|
|
43
|
+
"emitDecoratorMetadata": true
|
|
44
|
+
},
|
|
45
|
+
"include": ["api", "shared", "server", "config", "modern.config.ts"]
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Specify this file in `modern.config.ts`:
|
|
50
|
+
|
|
51
|
+
```ts title="modern.config.ts"
|
|
52
|
+
export default defineConfig({
|
|
53
|
+
server: {
|
|
54
|
+
tsconfigPath: './tsconfig.server.json',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
- `tsconfigPath` is resolved relative to `appDirectory`. Absolute paths are also supported.
|
|
62
|
+
- It is recommended to inherit shared options such as `paths` from the root tsconfig through `extends`, instead of maintaining them in multiple files.
|
|
63
|
+
- If the configured file does not exist, TypeScript / ts-node will throw an error at startup.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: enableAsyncPreEntry
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# source.enableAsyncPreEntry
|
|
6
|
+
|
|
7
|
+
- **Type:** `boolean`
|
|
8
|
+
- **Default:** `false`
|
|
9
|
+
|
|
10
|
+
When enabled, Modern.js will inject the modules configured in `source.preEntry` to the top of the auto-generated entry file (`index.jsx`) in order.
|
|
11
|
+
|
|
12
|
+
This option only takes effect when `source.enableAsyncEntry` is enabled. If async entry is not enabled, this behavior will be skipped and the original builder-side `source.preEntry` injection remains unchanged.
|
|
13
|
+
|
|
14
|
+
This option is mainly designed to work with `source.enableAsyncEntry`: when async entry is enabled, the final build entry becomes `bootstrap.jsx`, and the builder-side `source.preEntry` may not be injected into the real entry code. With `source.enableAsyncPreEntry` enabled, `preEntry` will be injected into `index.jsx` (the real entry code), so it also works in async entry scenarios.
|
|
15
|
+
|
|
16
|
+
Meanwhile, when both `source.enableAsyncEntry` and `source.enableAsyncPreEntry` are enabled, Modern.js will not pass `source.preEntry` into builder config to avoid duplicate injection or injection into an unexpected entry.
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
```ts title="modern.config.ts"
|
|
21
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
22
|
+
|
|
23
|
+
export default defineConfig({
|
|
24
|
+
source: {
|
|
25
|
+
enableAsyncEntry: true,
|
|
26
|
+
enableAsyncPreEntry: true,
|
|
27
|
+
preEntry: ['./src/pre-a.ts', './src/pre-b.ts'],
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
@@ -9,10 +9,6 @@ title: devServer
|
|
|
9
9
|
|
|
10
10
|
The config of DevServer can be modified through `tools.devServer`.
|
|
11
11
|
|
|
12
|
-
:::tip
|
|
13
|
-
Modern.js does not directly use [webpack-dev-server](https://webpack.js.org/api/webpack-dev-server/) or [@rspack/dev-server](https://rspack.rs/guide/dev-server.html), but implement DevServer based on [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware).
|
|
14
|
-
:::
|
|
15
|
-
|
|
16
12
|
### Options
|
|
17
13
|
|
|
18
14
|
#### compress
|
|
@@ -4,103 +4,81 @@ title: Advanced Usage
|
|
|
4
4
|
|
|
5
5
|
# Advanced Usage
|
|
6
6
|
|
|
7
|
-
## SSR
|
|
7
|
+
## SSR Scenarios
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
In SSR scenarios, the plugin detects the language on the server, loads translation resources, and injects the result into the page. The client reuses it directly to avoid language flicker.
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
In SSR scenarios, the plugin will detect language from HTTP requests:
|
|
14
|
-
|
|
15
|
-
1. **URL Path**: Extract language prefix from path
|
|
16
|
-
2. **Cookie**: Read language settings from cookies
|
|
17
|
-
3. **Request Headers**: Read from `Accept-Language` request header
|
|
18
|
-
|
|
19
|
-
**Configuration Example**:
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
i18nPlugin({
|
|
23
|
-
localeDetection: {
|
|
24
|
-
localePathRedirect: true,
|
|
25
|
-
i18nextDetector: true,
|
|
26
|
-
languages: ['zh', 'en'],
|
|
27
|
-
fallbackLanguage: 'en',
|
|
28
|
-
detection: {
|
|
29
|
-
order: ['path', 'cookie', 'header'],
|
|
30
|
-
lookupHeader: 'accept-language',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
})
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### SSR Resource Loading
|
|
37
|
-
|
|
38
|
-
In SSR scenarios, the plugin will automatically use the file system backend to load resources:
|
|
11
|
+
**Complete configuration:**
|
|
39
12
|
|
|
40
13
|
```ts
|
|
14
|
+
// modern.config.ts
|
|
41
15
|
export default defineConfig({
|
|
42
16
|
server: {
|
|
43
17
|
ssr: true,
|
|
44
|
-
publicDir: './locales', // Resource file directory
|
|
45
18
|
},
|
|
46
19
|
plugins: [
|
|
20
|
+
appTools(),
|
|
47
21
|
i18nPlugin({
|
|
22
|
+
localeDetection: {
|
|
23
|
+
localePathRedirect: true,
|
|
24
|
+
i18nextDetector: true,
|
|
25
|
+
languages: ['zh', 'en'],
|
|
26
|
+
fallbackLanguage: 'en',
|
|
27
|
+
detection: {
|
|
28
|
+
order: ['path', 'cookie', 'header'],
|
|
29
|
+
lookupHeader: 'accept-language',
|
|
30
|
+
caches: ['cookie'],
|
|
31
|
+
},
|
|
32
|
+
},
|
|
48
33
|
backend: {
|
|
49
|
-
|
|
34
|
+
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
50
35
|
},
|
|
51
36
|
}),
|
|
52
37
|
],
|
|
53
38
|
});
|
|
54
39
|
```
|
|
55
40
|
|
|
56
|
-
|
|
41
|
+
Server-side language detection priority: URL path -> Cookie -> `Accept-Language` request header -> `fallbackLanguage`.
|
|
42
|
+
|
|
43
|
+
**Route configuration (file-system routes):**
|
|
57
44
|
|
|
58
45
|
```
|
|
59
|
-
|
|
60
|
-
└──
|
|
61
|
-
├──
|
|
62
|
-
|
|
63
|
-
└── zh/
|
|
64
|
-
└── translation.json
|
|
46
|
+
routes/
|
|
47
|
+
└── [lang]/
|
|
48
|
+
├── layout.tsx
|
|
49
|
+
└── page.tsx
|
|
65
50
|
```
|
|
66
51
|
|
|
67
|
-
|
|
52
|
+
In SSR scenarios, the language detected on the server is written to `window._SSR_DATA`. The client reads it directly and does not detect again, ensuring both sides use the same language.
|
|
68
53
|
|
|
69
|
-
|
|
54
|
+
## Multiple Entries
|
|
70
55
|
|
|
71
|
-
|
|
56
|
+
Different entries can use different language lists, detection methods, and resource paths:
|
|
72
57
|
|
|
73
58
|
```ts
|
|
59
|
+
// modern.config.ts
|
|
74
60
|
i18nPlugin({
|
|
75
61
|
localeDetection: {
|
|
76
|
-
// Global
|
|
62
|
+
// Global defaults
|
|
77
63
|
localePathRedirect: true,
|
|
78
64
|
languages: ['zh', 'en'],
|
|
79
65
|
fallbackLanguage: 'en',
|
|
80
66
|
|
|
81
|
-
//
|
|
67
|
+
// Per-entry overrides
|
|
82
68
|
localeDetectionByEntry: {
|
|
83
69
|
admin: {
|
|
84
|
-
localePathRedirect: false, // admin
|
|
85
|
-
languages: ['en'],
|
|
70
|
+
localePathRedirect: false, // admin does not use path prefixes
|
|
71
|
+
languages: ['en'], // admin only supports English
|
|
86
72
|
},
|
|
87
73
|
mobile: {
|
|
88
|
-
languages: ['zh', 'en', 'ja'], // mobile
|
|
74
|
+
languages: ['zh', 'en', 'ja'], // mobile supports more languages
|
|
89
75
|
},
|
|
90
76
|
},
|
|
91
77
|
},
|
|
92
|
-
})
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### Configure Backend by Entry
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
i18nPlugin({
|
|
99
78
|
backend: {
|
|
100
|
-
|
|
101
|
-
loadPath: '/locales/{{lng}}/{{ns}}.json', // Default path
|
|
79
|
+
loadPath: '/locales/{{lng}}/{{ns}}.json', // Global default path
|
|
102
80
|
|
|
103
|
-
//
|
|
81
|
+
// Per-entry overrides
|
|
104
82
|
backendOptionsByEntry: {
|
|
105
83
|
admin: {
|
|
106
84
|
loadPath: '/admin/locales/{{lng}}/{{ns}}.json',
|
|
@@ -110,33 +88,24 @@ i18nPlugin({
|
|
|
110
88
|
},
|
|
111
89
|
},
|
|
112
90
|
},
|
|
113
|
-
})
|
|
91
|
+
});
|
|
114
92
|
```
|
|
115
93
|
|
|
116
94
|
## Custom i18next Instance
|
|
117
95
|
|
|
118
|
-
If you need
|
|
119
|
-
|
|
120
|
-
### Create Custom Instance
|
|
96
|
+
By default, the plugin uses an internally created i18next instance. If you need deeper customization, such as custom plugins or preconfigured options, pass a custom instance through runtime configuration:
|
|
121
97
|
|
|
122
98
|
```ts
|
|
123
99
|
// src/i18n.ts
|
|
124
100
|
import i18next from 'i18next';
|
|
125
101
|
|
|
126
|
-
const customI18n = i18next.createInstance(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
supportedLngs: ['zh', 'en'],
|
|
130
|
-
interpolation: {
|
|
131
|
-
escapeValue: false,
|
|
132
|
-
},
|
|
133
|
-
});
|
|
102
|
+
const customI18n = i18next.createInstance();
|
|
103
|
+
// You can use custom plugins here.
|
|
104
|
+
// customI18n.use(MyPlugin);
|
|
134
105
|
|
|
135
106
|
export default customI18n;
|
|
136
107
|
```
|
|
137
108
|
|
|
138
|
-
### Pass Custom Instance
|
|
139
|
-
|
|
140
109
|
```ts
|
|
141
110
|
// src/modern.runtime.ts
|
|
142
111
|
import { defineRuntimeConfig } from '@modern-js/runtime';
|
|
@@ -146,48 +115,18 @@ export default defineRuntimeConfig({
|
|
|
146
115
|
i18n: {
|
|
147
116
|
i18nInstance: customI18n,
|
|
148
117
|
initOptions: {
|
|
149
|
-
|
|
118
|
+
fallbackLng: 'en',
|
|
119
|
+
supportedLngs: ['zh', 'en'],
|
|
150
120
|
},
|
|
151
121
|
},
|
|
152
122
|
});
|
|
153
123
|
```
|
|
154
124
|
|
|
155
|
-
## Language Switching
|
|
156
|
-
|
|
157
|
-
### Programmatic Switching
|
|
158
|
-
|
|
159
|
-
Use the `changeLanguage` method of the `useModernI18n` Hook:
|
|
160
|
-
|
|
161
|
-
```tsx
|
|
162
|
-
import { useModernI18n } from '@modern-js/plugin-i18n/runtime';
|
|
163
|
-
|
|
164
|
-
function LanguageSwitcher() {
|
|
165
|
-
const { language, changeLanguage, supportedLanguages } = useModernI18n();
|
|
166
|
-
|
|
167
|
-
return (
|
|
168
|
-
<select
|
|
169
|
-
value={language}
|
|
170
|
-
onChange={e => changeLanguage(e.target.value)}
|
|
171
|
-
>
|
|
172
|
-
{supportedLanguages.map(lang => (
|
|
173
|
-
<option key={lang} value={lang}>
|
|
174
|
-
{lang}
|
|
175
|
-
</option>
|
|
176
|
-
))}
|
|
177
|
-
</select>
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### URL Synchronization
|
|
183
|
-
|
|
184
|
-
When `localePathRedirect` is enabled, switching languages will automatically update the URL:
|
|
185
|
-
|
|
186
|
-
```tsx
|
|
187
|
-
// Current URL: /en/about
|
|
188
|
-
// Call changeLanguage('zh')
|
|
189
|
-
// URL automatically updates to: /zh/about
|
|
190
|
-
```
|
|
125
|
+
## Language Switching Behavior
|
|
191
126
|
|
|
192
|
-
|
|
127
|
+
When `changeLanguage` is called to switch language:
|
|
193
128
|
|
|
129
|
+
- **Translation update**: All components that use `t()` re-render automatically. No manual refresh is needed.
|
|
130
|
+
- **URL update** (when `localePathRedirect` is enabled): Updates the URL with `history.pushState`, without triggering a page refresh and without affecting browser back/forward history.
|
|
131
|
+
- **Cache update**: Writes to Cookie or LocalStorage based on the `caches` configuration, so the language choice is restored on the next visit.
|
|
132
|
+
- **When path prefixes are not enabled**: Only the i18next instance and cache are updated. The URL stays unchanged.
|