You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/document/docs/en/guides/advanced-features/international/api.mdx
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,9 @@ interface UseModernI18nReturn {
26
26
27
27
/** Check if language is supported */
28
28
isLanguageSupported: (lang:string) =>boolean;
29
+
30
+
/** Indicates if translation resources for current language are ready to use */
31
+
isResourcesReady:boolean;
29
32
}
30
33
```
31
34
@@ -105,6 +108,37 @@ function handleLanguageChange(lang: string) {
105
108
}
106
109
```
107
110
111
+
### Resource Loading State
112
+
113
+
`isResourcesReady` indicates whether the translation resources for the current language are loaded and ready to use. This is particularly useful when using SDK backend to load resources dynamically.
For simpler cases, you can also check i18next initialization status:
76
+
52
77
```tsx
53
78
import { useTranslation } from'react-i18next';
54
79
55
80
function MyComponent() {
56
81
const { t, i18n } =useTranslation();
57
82
58
-
// Check if resources are loaded
83
+
// Check if i18n is initialized
59
84
if (!i18n.isInitialized) {
60
85
return <div>Loading...</div>;
61
86
}
@@ -64,6 +89,10 @@ function MyComponent() {
64
89
}
65
90
```
66
91
92
+
:::tip
93
+
`isResourcesReady` is more accurate for SDK backend scenarios as it checks if all required resources are actually loaded, not just if the instance is initialized.
94
+
:::
95
+
67
96
## Type Safety
68
97
69
98
Add type definitions for translation keys to improve development experience:
Copy file name to clipboardExpand all lines: packages/document/docs/en/guides/advanced-features/international/configuration.mdx
+140-2Lines changed: 140 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,23 @@ i18nPlugin({
85
85
86
86
`backend` is used to configure resource loading methods:
87
87
88
+
:::info
89
+
**Auto-detection**: The plugin automatically detects and enables backend in the following scenarios:
90
+
91
+
1.**If you configure `loadPath` or `addPath`**: The backend will be automatically enabled (`enabled: true`) without checking for locales directory, since you've already specified the resource path.
92
+
93
+
2.**If you don't configure backend**: The plugin will automatically detect if a `locales` directory exists in:
94
+
- Project root: `{projectRoot}/locales`
95
+
- Config public directory: `{projectRoot}/config/public/locales`
96
+
- Public directory configured via `server.publicDir`: `{projectRoot}/{publicDir}/locales`
97
+
98
+
If the directory exists and contains JSON files, the backend will be automatically enabled.
99
+
100
+
3.**If you explicitly set `enabled: false`**: No auto-detection will be performed, and the backend will remain disabled.
101
+
102
+
This automatic detection helps reduce unnecessary backend registration when locales directory doesn't exist, improving performance.
Or simply configure `loadPath` or `addPath`, and backend will be automatically enabled:
166
+
167
+
```ts
168
+
i18nPlugin({
169
+
backend: {
170
+
// enabled will be automatically set to true
171
+
loadPath: '/locales/{{lng}}/{{ns}}.json',
172
+
},
173
+
});
174
+
```
175
+
176
+
**Auto-detection without configuration**:
177
+
178
+
If you don't configure backend at all, the plugin will automatically detect locales directory:
179
+
180
+
```ts
181
+
i18nPlugin({
182
+
// No backend config - plugin will auto-detect locales directory
183
+
localeDetection: {
184
+
languages: ['zh', 'en'],
185
+
fallbackLanguage: 'en',
186
+
},
187
+
});
188
+
```
189
+
190
+
If `locales` directory exists with JSON files, backend will be automatically enabled with default `loadPath: '/locales/{{lng}}/{{ns}}.json'`.
191
+
192
+
**2. Chained backend (recommended)**: Use both HTTP/FS backend and SDK backend
193
+
194
+
When `backend.enabled = true` and `sdk` is configured, if `loadPath` is not explicitly configured, the default `loadPath` will be used automatically and chained backend will be enabled:
195
+
196
+
```ts
197
+
i18nPlugin({
198
+
backend: {
199
+
enabled: true,
200
+
// When loadPath is not configured, default '/locales/{{lng}}/{{ns}}.json' will be used
201
+
sdk: true, // SDK backend
202
+
// cacheHitMode: 'refreshAndUpdateStore', // Default value, can be omitted
0 commit comments