@modern-js/main-doc 3.1.5 → 3.2.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.
Files changed (46) hide show
  1. package/docs/en/components/international/init-options-desc.mdx +1 -1
  2. package/docs/en/components/international/install-command.mdx +4 -17
  3. package/docs/en/components/international/instance-code.mdx +4 -14
  4. package/docs/en/components/international/introduce.mdx +4 -1
  5. package/docs/en/configure/app/source/enable-async-pre-entry.mdx +30 -0
  6. package/docs/en/configure/app/tools/dev-server.mdx +0 -4
  7. package/docs/en/guides/advanced-features/international/_meta.json +0 -1
  8. package/docs/en/guides/advanced-features/international/advanced.mdx +48 -109
  9. package/docs/en/guides/advanced-features/international/api.mdx +125 -290
  10. package/docs/en/guides/advanced-features/international/best-practices.mdx +203 -48
  11. package/docs/en/guides/advanced-features/international/configuration.mdx +108 -315
  12. package/docs/en/guides/advanced-features/international/locale-detection.mdx +62 -208
  13. package/docs/en/guides/advanced-features/international/quick-start.mdx +41 -55
  14. package/docs/en/guides/advanced-features/international/resource-loading.mdx +63 -322
  15. package/docs/en/guides/advanced-features/international/routing.mdx +60 -138
  16. package/docs/en/guides/advanced-features/international.mdx +19 -27
  17. package/docs/en/guides/basic-features/alias.mdx +1 -1
  18. package/docs/en/guides/basic-features/html.mdx +2 -2
  19. package/docs/en/guides/basic-features/static-assets.mdx +1 -2
  20. package/docs/en/guides/concept/entries.mdx +2 -2
  21. package/docs/zh/components/international/init-options-desc.mdx +1 -1
  22. package/docs/zh/components/international/install-command.mdx +4 -16
  23. package/docs/zh/components/international/instance-code.mdx +4 -14
  24. package/docs/zh/components/international/introduce.mdx +5 -2
  25. package/docs/zh/configure/app/source/enable-async-pre-entry.mdx +77 -0
  26. package/docs/zh/configure/app/tools/dev-server.mdx +0 -4
  27. package/docs/zh/guides/advanced-features/bff/function.mdx +2 -2
  28. package/docs/zh/guides/advanced-features/international/_meta.json +0 -1
  29. package/docs/zh/guides/advanced-features/international/advanced.mdx +48 -109
  30. package/docs/zh/guides/advanced-features/international/api.mdx +126 -292
  31. package/docs/zh/guides/advanced-features/international/best-practices.mdx +204 -49
  32. package/docs/zh/guides/advanced-features/international/configuration.mdx +105 -318
  33. package/docs/zh/guides/advanced-features/international/locale-detection.mdx +62 -236
  34. package/docs/zh/guides/advanced-features/international/quick-start.mdx +40 -54
  35. package/docs/zh/guides/advanced-features/international/resource-loading.mdx +62 -324
  36. package/docs/zh/guides/advanced-features/international/routing.mdx +58 -136
  37. package/docs/zh/guides/advanced-features/international.mdx +19 -26
  38. package/docs/zh/guides/basic-features/alias.mdx +1 -1
  39. package/docs/zh/guides/basic-features/html.mdx +2 -2
  40. package/docs/zh/guides/basic-features/static-assets.mdx +1 -2
  41. package/docs/zh/guides/concept/entries.mdx +2 -2
  42. package/package.json +4 -4
  43. package/docs/en/components/rspackPrecautions.mdx +0 -6
  44. package/docs/en/guides/advanced-features/international/basic.mdx +0 -417
  45. package/docs/zh/components/rspackPrecautions.mdx +0 -6
  46. package/docs/zh/guides/advanced-features/international/basic.mdx +0 -416
@@ -1 +1 @@
1
- `initOptions` will be passed to i18next's `init` method and supports all i18next configuration options:
1
+ All [i18next initialization options](https://www.i18next.com/overview/configuration-options) are supported. Common options are described below:
@@ -1,23 +1,10 @@
1
- ```bash
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
- Check the version of `@modern-js/app-tools` first, then install the same version of `@modern-js/plugin-i18n`:
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 the `src/modern.runtime.ts` file and configure i18n runtime options:
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
- // It's recommended to create a new i18next instance to avoid using the global default instance
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: 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 Modern.js's internationalization plugin, built on top of [i18next](https://www.i18next.com/) and [react-i18next](https://react.i18next.com/), providing a complete internationalization solution for Modern.js applications.
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,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
@@ -1,5 +1,4 @@
1
1
  [
2
- "basic",
3
2
  "quick-start",
4
3
  "configuration",
5
4
  "locale-detection",
@@ -4,103 +4,81 @@ title: Advanced Usage
4
4
 
5
5
  # Advanced Usage
6
6
 
7
- ## SSR Configuration
7
+ ## SSR Scenarios
8
8
 
9
- The plugin fully supports server-side rendering (SSR) scenarios.
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
- ### Server-side Language Detection
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
- enabled: true,
34
+ loadPath: '/locales/{{lng}}/{{ns}}.json',
50
35
  },
51
36
  }),
52
37
  ],
53
38
  });
54
39
  ```
55
40
 
56
- Resource file structure:
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
- Project Root/
60
- └── locales/
61
- ├── en/
62
- └── translation.json
63
- └── zh/
64
- └── translation.json
46
+ routes/
47
+ └── [lang]/
48
+ ├── layout.tsx
49
+ └── page.tsx
65
50
  ```
66
51
 
67
- ## Multi-Entry Configuration
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
- If the project has multiple entries, you can configure language detection and backend options separately for each entry.
54
+ ## Multiple Entries
70
55
 
71
- ### Configure Language Detection by Entry
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 configuration
62
+ // Global defaults
77
63
  localePathRedirect: true,
78
64
  languages: ['zh', 'en'],
79
65
  fallbackLanguage: 'en',
80
66
 
81
- // Override configuration by entry
67
+ // Per-entry overrides
82
68
  localeDetectionByEntry: {
83
69
  admin: {
84
- localePathRedirect: false, // admin entry does not use path redirection
85
- languages: ['en'], // admin entry only supports English
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 entry supports more languages
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
- enabled: true,
101
- loadPath: '/locales/{{lng}}/{{ns}}.json', // Default path
79
+ loadPath: '/locales/{{lng}}/{{ns}}.json', // Global default path
102
80
 
103
- // Override configuration by entry
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 to use a custom i18next instance, you can provide it in runtime configuration.
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
- // Custom configuration
128
- fallbackLng: 'en',
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
- // Other configuration options
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
- If `localePathRedirect` is not enabled, language switching will only update the i18next instance and cache, without changing the URL.
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.