@intlayer/docs 5.7.4 → 5.7.6-canary.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/blog/ar/intlayer_with_i18next.md +1 -1
- package/dist/cjs/common.cjs +0 -1
- package/dist/cjs/common.cjs.map +1 -1
- package/dist/cjs/generated/docs.entry.cjs +80 -0
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/esm/common.mjs +0 -1
- package/dist/esm/common.mjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +80 -0
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/generated/docs.entry.d.ts +1 -0
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/docs/ar/CI_CD.md +0 -1
- package/docs/ar/configuration.md +42 -5
- package/docs/ar/dictionary/enumeration.md +0 -1
- package/docs/ar/packages/react-intlayer/useI18n.md +250 -0
- package/docs/de/CI_CD.md +0 -1
- package/docs/de/configuration.md +42 -5
- package/docs/de/dictionary/gender.md +3 -3
- package/docs/de/packages/react-intlayer/useI18n.md +250 -0
- package/docs/en/configuration.md +44 -7
- package/docs/en/packages/react-intlayer/useI18n.md +250 -0
- package/docs/en-GB/configuration.md +42 -5
- package/docs/en-GB/packages/react-intlayer/useI18n.md +254 -0
- package/docs/es/configuration.md +42 -5
- package/docs/es/dictionary/enumeration.md +1 -2
- package/docs/es/dictionary/gender.md +3 -3
- package/docs/es/locale_mapper.md +1 -1
- package/docs/es/packages/react-intlayer/useI18n.md +250 -0
- package/docs/fr/configuration.md +44 -7
- package/docs/fr/intlayer_with_nextjs_15.md +0 -1
- package/docs/fr/intlayer_with_vite+preact.md +1 -1
- package/docs/fr/packages/react-intlayer/useI18n.md +250 -0
- package/docs/hi/configuration.md +42 -5
- package/docs/hi/dictionary/enumeration.md +0 -2
- package/docs/hi/packages/react-intlayer/useI18n.md +259 -0
- package/docs/it/configuration.md +42 -5
- package/docs/it/dictionary/enumeration.md +0 -1
- package/docs/it/packages/react-intlayer/useI18n.md +250 -0
- package/docs/it/vs_code_extension.md +0 -1
- package/docs/ja/configuration.md +43 -28
- package/docs/ja/dictionary/enumeration.md +0 -2
- package/docs/ja/packages/react-intlayer/useI18n.md +250 -0
- package/docs/ko/configuration.md +42 -5
- package/docs/ko/dictionary/enumeration.md +0 -2
- package/docs/ko/packages/react-intlayer/useI18n.md +250 -0
- package/docs/pt/configuration.md +42 -5
- package/docs/pt/packages/react-intlayer/useI18n.md +250 -0
- package/docs/ru/configuration.md +42 -5
- package/docs/ru/dictionary/enumeration.md +0 -2
- package/docs/ru/packages/react-intlayer/useI18n.md +250 -0
- package/docs/ru/vs_code_extension.md +0 -1
- package/docs/zh/configuration.md +42 -5
- package/docs/zh/dictionary/enumeration.md +0 -1
- package/docs/zh/packages/react-intlayer/useI18n.md +250 -0
- package/frequent_questions/ar/unknown_command.md +1 -1
- package/frequent_questions/de/array_as_content_declaration.md +1 -1
- package/frequent_questions/de/build_dictionaries.md +1 -1
- package/frequent_questions/de/build_error_CI_CD.md +1 -1
- package/frequent_questions/de/customized_locale_list.md +1 -1
- package/frequent_questions/de/esbuild_error.md +1 -1
- package/frequent_questions/de/intlayer_command_undefined.md +1 -1
- package/frequent_questions/de/locale_incorect_in_url.md +1 -1
- package/frequent_questions/de/static_rendering.md +1 -1
- package/frequent_questions/de/translated_path_url.md +1 -1
- package/frequent_questions/de/unknown_command.md +1 -1
- package/frequent_questions/en-GB/customized_locale_list.md +1 -1
- package/frequent_questions/es/array_as_content_declaration.md +1 -1
- package/frequent_questions/es/build_dictionaries.md +1 -1
- package/frequent_questions/es/customized_locale_list.md +1 -1
- package/frequent_questions/es/domain_routing.md +1 -1
- package/frequent_questions/es/get_locale_cookie.md +1 -1
- package/frequent_questions/es/intlayer_command_undefined.md +1 -1
- package/frequent_questions/es/locale_incorect_in_url.md +1 -1
- package/frequent_questions/es/static_rendering.md +1 -1
- package/frequent_questions/es/translated_path_url.md +1 -1
- package/frequent_questions/es/unknown_command.md +1 -1
- package/frequent_questions/fr/array_as_content_declaration.md +1 -1
- package/frequent_questions/fr/build_dictionaries.md +1 -1
- package/frequent_questions/fr/customized_locale_list.md +1 -1
- package/frequent_questions/fr/domain_routing.md +1 -1
- package/frequent_questions/fr/esbuild_error.md +1 -1
- package/frequent_questions/fr/intlayer_command_undefined.md +1 -1
- package/frequent_questions/fr/static_rendering.md +1 -1
- package/frequent_questions/fr/translated_path_url.md +1 -1
- package/frequent_questions/fr/unknown_command.md +1 -1
- package/frequent_questions/it/array_as_content_declaration.md +1 -1
- package/frequent_questions/it/build_dictionaries.md +1 -1
- package/frequent_questions/it/customized_locale_list.md +1 -1
- package/frequent_questions/it/esbuild_error.md +1 -1
- package/frequent_questions/it/intlayer_command_undefined.md +1 -1
- package/frequent_questions/it/locale_incorect_in_url.md +1 -1
- package/frequent_questions/it/static_rendering.md +1 -1
- package/frequent_questions/it/translated_path_url.md +1 -1
- package/frequent_questions/it/unknown_command.md +1 -1
- package/frequent_questions/ko/build_dictionaries.md +2 -2
- package/frequent_questions/ko/customized_locale_list.md +3 -3
- package/frequent_questions/ko/intlayer_command_undefined.md +3 -3
- package/frequent_questions/pt/array_as_content_declaration.md +1 -1
- package/frequent_questions/pt/build_dictionaries.md +1 -1
- package/frequent_questions/pt/build_error_CI_CD.md +1 -1
- package/frequent_questions/pt/customized_locale_list.md +1 -1
- package/frequent_questions/pt/domain_routing.md +1 -1
- package/frequent_questions/pt/esbuild_error.md +1 -1
- package/frequent_questions/pt/intlayer_command_undefined.md +1 -1
- package/frequent_questions/pt/locale_incorect_in_url.md +1 -1
- package/frequent_questions/pt/static_rendering.md +1 -1
- package/frequent_questions/pt/translated_path_url.md +1 -1
- package/frequent_questions/pt/unknown_command.md +1 -1
- package/frequent_questions/zh/build_dictionaries.md +3 -3
- package/frequent_questions/zh/customized_locale_list.md +3 -3
- package/frequent_questions/zh/esbuild_error.md +3 -3
- package/frequent_questions/zh/intlayer_command_undefined.md +3 -3
- package/frequent_questions/zh/translated_path_url.md +3 -3
- package/package.json +9 -9
- package/src/common.ts +0 -1
- package/src/generated/docs.entry.ts +80 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2024-08-11
|
|
3
|
+
updatedAt: 2025-06-29
|
|
4
|
+
title: Документация по хуку useI18n | react-intlayer
|
|
5
|
+
description: Узнайте, как использовать хук useI18n в пакете react-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- useI18n
|
|
8
|
+
- i18n
|
|
9
|
+
- перевод
|
|
10
|
+
- словарь
|
|
11
|
+
- Intlayer
|
|
12
|
+
- интернационализация
|
|
13
|
+
- документация
|
|
14
|
+
- Next.js
|
|
15
|
+
- JavaScript
|
|
16
|
+
- React
|
|
17
|
+
slugs:
|
|
18
|
+
- doc
|
|
19
|
+
- packages
|
|
20
|
+
- react-intlayer
|
|
21
|
+
- useI18n
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Интеграция с React: Документация по хуку `useI18n`
|
|
25
|
+
|
|
26
|
+
В этом разделе представлено подробное руководство по использованию хука `useI18n` в приложениях React, что позволяет эффективно локализовать контент.
|
|
27
|
+
|
|
28
|
+
## Импортирование `useI18n` в React
|
|
29
|
+
|
|
30
|
+
Хук `useI18n` можно импортировать и интегрировать в приложения React в зависимости от контекста следующим образом:
|
|
31
|
+
|
|
32
|
+
- **Клиентские компоненты:**
|
|
33
|
+
|
|
34
|
+
```typescript codeFormat="typescript"
|
|
35
|
+
import { useI18n } from "react-intlayer"; // Используйте в клиентских React-компонентах
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```javascript codeFormat="esm"
|
|
39
|
+
import { useI18n } from "react-intlayer"; // Используйте в клиентских React-компонентах
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```javascript codeFormat="commonjs"
|
|
43
|
+
const { useI18n } = require("react-intlayer"); // Используйте в клиентских React-компонентах
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **Серверные компоненты:**
|
|
47
|
+
|
|
48
|
+
```typescript codeFormat="commonjs"
|
|
49
|
+
import { useI18n } from "react-intlayer/server"; // Используйте в серверных React-компонентах
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```javascript codeFormat="esm"
|
|
53
|
+
import { useI18n } from "react-intlayer/server"; // Используйте в серверных React-компонентах
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```javascript codeFormat="commonjs"
|
|
57
|
+
const { useI18n } = require("react-intlayer/server"); // Используйте в серверных React-компонентах
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Параметры
|
|
61
|
+
|
|
62
|
+
Этот хук принимает два параметра:
|
|
63
|
+
|
|
64
|
+
1. **`namespace`**: Пространство имён словаря для ограничения области ключей перевода.
|
|
65
|
+
2. **`locale`** (необязательно): Желаемая локаль. Если не указана, по умолчанию будет использоваться локаль из контекста.
|
|
66
|
+
|
|
67
|
+
## Словарь
|
|
68
|
+
|
|
69
|
+
Все ключи словаря должны быть объявлены в файлах декларации контента для повышения безопасности типов и предотвращения ошибок. [Инструкции по настройке можно найти здесь](https://github.com/aymericzip/intlayer/blob/main/docs/docs/ru/dictionary/get_started.md).
|
|
70
|
+
|
|
71
|
+
## Примеры использования в React
|
|
72
|
+
|
|
73
|
+
Примеры использования хука `useI18n` в React-компонентах:
|
|
74
|
+
|
|
75
|
+
```tsx fileName="src/App.tsx" codeFormat="typescript"
|
|
76
|
+
import type { FC } from "react";
|
|
77
|
+
import { ClientComponentExample, ServerComponentExample } from "@components";
|
|
78
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
79
|
+
import { useI18n, IntlayerServerProvider } from "react-intlayer/server";
|
|
80
|
+
import { Locales } from "intlayer";
|
|
81
|
+
|
|
82
|
+
const App: FC<{ locale: Locales }> = ({ locale }) => {
|
|
83
|
+
const t = useI18n("home-page", locale);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<>
|
|
87
|
+
<p>{t("introduction")}</p>
|
|
88
|
+
<IntlayerProvider locale={locale}>
|
|
89
|
+
<ClientComponentExample />
|
|
90
|
+
</IntlayerProvider>
|
|
91
|
+
<IntlayerServerProvider locale={locale}>
|
|
92
|
+
<ServerComponentExample />
|
|
93
|
+
</IntlayerServerProvider>
|
|
94
|
+
</>
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```jsx fileName="src/app.jsx" codeFormat="esm"
|
|
100
|
+
import { ClientComponentExample, ServerComponentExample } from "@components";
|
|
101
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
102
|
+
import { IntlayerServerProvider, useI18n } from "react-intlayer/server";
|
|
103
|
+
|
|
104
|
+
const App = ({ locale }) => {
|
|
105
|
+
const t = useI18n("home-page", locale);
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<>
|
|
109
|
+
<p>{t("introduction")}</p>
|
|
110
|
+
<IntlayerProvider locale={locale}>
|
|
111
|
+
<ClientComponentExample />
|
|
112
|
+
</IntlayerProvider>
|
|
113
|
+
<IntlayerServerProvider locale={locale}>
|
|
114
|
+
<ServerComponentExample />
|
|
115
|
+
</IntlayerServerProvider>
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```jsx fileName="src/app.cjs" codeFormat="commonjs"
|
|
122
|
+
const { IntlayerProvider } = require("react-intlayer");
|
|
123
|
+
const { IntlayerServerProvider, useI18n } = require("react-intlayer/server");
|
|
124
|
+
|
|
125
|
+
const App = ({ locale }) => {
|
|
126
|
+
const t = useI18n("home-page", locale);
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<>
|
|
130
|
+
<p>{t("introduction")}</p>
|
|
131
|
+
<IntlayerProvider locale={locale}>
|
|
132
|
+
<ClientComponentExample />
|
|
133
|
+
</IntlayerProvider>
|
|
134
|
+
<IntlayerServerProvider locale={locale}>
|
|
135
|
+
<ServerComponentExample />
|
|
136
|
+
</IntlayerServerProvider>
|
|
137
|
+
</>
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
```tsx fileName="src/components/ComponentExample.tsx" codeFormat="typescript"
|
|
143
|
+
import type { FC } from "react";
|
|
144
|
+
import { useI18n } from "react-intlayer";
|
|
145
|
+
|
|
146
|
+
const ComponentExample: FC = () => {
|
|
147
|
+
const t = useI18n("component-example");
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div>
|
|
151
|
+
<h1>{t("title")}</h1> {/* Отобразить заголовок */}
|
|
152
|
+
<p>{t("description")}</p> {/* Отобразить описание */}
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```jsx fileName="src/components/ComponentExample.jsx" codeFormat="esm"
|
|
159
|
+
import { useI18n } from "react-intlayer";
|
|
160
|
+
|
|
161
|
+
const ComponentExample = () => {
|
|
162
|
+
const t = useI18n("component-example");
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div>
|
|
166
|
+
<h1>{t("title")}</h1> {/* Отобразить заголовок */}
|
|
167
|
+
<p>{t("description")}</p> {/* Отобразить описание */}
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
```jsx fileName="src/components/ComponentExample.cjs" codeFormat="commonjs"
|
|
174
|
+
const { useI18n } = require("react-intlayer");
|
|
175
|
+
|
|
176
|
+
const ComponentExample = () => {
|
|
177
|
+
const t = useI18n("component-example");
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<div>
|
|
181
|
+
<h1>{t("title")}</h1> {/* Отобразить заголовок */}
|
|
182
|
+
<p>{t("description")}</p> {/* Отобразить описание */}
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
};
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```tsx fileName="src/components/ServerComponentExample.tsx" codeFormat="typescript"
|
|
189
|
+
import { useI18n } from "react-intlayer/server";
|
|
190
|
+
|
|
191
|
+
const ServerComponentExample = () => {
|
|
192
|
+
const t = useI18n("server-component");
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div>
|
|
196
|
+
<h1>{t("title")}</h1> {/* Отобразить заголовок */}
|
|
197
|
+
<p>{t("description")}</p> {/* Отобразить описание */}
|
|
198
|
+
</div>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
```jsx fileName="src/components/ServerComponentExample.jsx" codeFormat="esm"
|
|
204
|
+
import { useI18n } from "react-intlayer/server";
|
|
205
|
+
|
|
206
|
+
const ServerComponentExample = () => {
|
|
207
|
+
const t = useI18n("server-component");
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<div>
|
|
211
|
+
<h1>{t("title")}</h1> {/* Отобразить заголовок */}
|
|
212
|
+
<p>{t("description")}</p> {/* Отобразить описание */}
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
```jsx fileName="src/components/ServerComponentExample.cjs" codeFormat="commonjs"
|
|
219
|
+
const { useI18n } = require("react-intlayer/server");
|
|
220
|
+
|
|
221
|
+
const ServerComponentExample = () => {
|
|
222
|
+
const t = useI18n("server-component");
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<div>
|
|
226
|
+
<h1>{t("title")}</h1>
|
|
227
|
+
<p>{t("description")}</p>
|
|
228
|
+
</div>
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Обработка атрибутов
|
|
234
|
+
|
|
235
|
+
При локализации атрибутов обращайтесь к значениям перевода соответствующим образом:
|
|
236
|
+
|
|
237
|
+
```jsx
|
|
238
|
+
<!-- Для атрибутов доступности (например, aria-label) используйте .value, так как требуются чистые строки -->
|
|
239
|
+
<button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Дополнительные ресурсы
|
|
243
|
+
|
|
244
|
+
- **Визуальный редактор Intlayer**: Для более интуитивного управления контентом обратитесь к документации по визуальному редактору [здесь](https://github.com/aymericzip/intlayer/blob/main/docs/docs/ru/intlayer_visual_editor.md).
|
|
245
|
+
|
|
246
|
+
Этот раздел специально охватывает интеграцию хука `useI18n` в React-приложениях, упрощая процесс локализации и обеспечивая согласованность контента между различными локалями.
|
|
247
|
+
|
|
248
|
+
## История документации
|
|
249
|
+
|
|
250
|
+
- 6.0.0 - 2025-06-29: Первоначальное написание документации по хуку `useI18n`
|
package/docs/zh/configuration.md
CHANGED
|
@@ -268,10 +268,12 @@ module.exports = config;
|
|
|
268
268
|
- **prefixDefault**:
|
|
269
269
|
|
|
270
270
|
- _类型_: `boolean`
|
|
271
|
-
- _默认值_: `
|
|
271
|
+
- _默认值_: `false`
|
|
272
272
|
- _描述_: 是否在 URL 中包含默认语言环境。
|
|
273
|
-
- _示例_: `
|
|
274
|
-
- _注意_:
|
|
273
|
+
- _示例_: `true`
|
|
274
|
+
- _注意_:
|
|
275
|
+
- 如果 `true` 且 `defaultLocale = 'en'`: path = `/en/dashboard` 或 `/fr/dashboard`
|
|
276
|
+
- 如果 `false` 且 `defaultLocale = 'en'`: path = `/dashboard` 或 `/fr/dashboard`
|
|
275
277
|
|
|
276
278
|
- **basePath**:
|
|
277
279
|
|
|
@@ -279,7 +281,11 @@ module.exports = config;
|
|
|
279
281
|
- _默认值_: `''`
|
|
280
282
|
- _描述_: 应用程序 URL 的基础路径。
|
|
281
283
|
- _示例_: `'/my-app'`
|
|
282
|
-
- _注意_:
|
|
284
|
+
- _注意_:
|
|
285
|
+
- 如果应用程序托管在 `https://example.com/my-app`
|
|
286
|
+
- 基础路径是 `'/my-app'`
|
|
287
|
+
- URL 将是 `https://example.com/my-app/en`
|
|
288
|
+
- 如果基础路径未设置,URL 将是 `https://example.com/en`
|
|
283
289
|
|
|
284
290
|
- **serverSetCookie**:
|
|
285
291
|
|
|
@@ -291,11 +297,42 @@ module.exports = config;
|
|
|
291
297
|
- _注意_: 控制是否在每个请求上设置语言环境 Cookie 或从不设置。
|
|
292
298
|
|
|
293
299
|
- **noPrefix**:
|
|
300
|
+
|
|
294
301
|
- _类型_: `boolean`
|
|
295
302
|
- _默认值_: `false`
|
|
296
303
|
- _描述_: 是否从 URL 中省略语言环境前缀。
|
|
297
304
|
- _示例_: `true`
|
|
298
|
-
- _注意_:
|
|
305
|
+
- _注意_:
|
|
306
|
+
- 如果 `true`: URL 中没有前缀
|
|
307
|
+
- 如果 `false`: URL 中有前缀
|
|
308
|
+
- 使用 `basePath = '/my-app'` 的示例:
|
|
309
|
+
- 如果 `noPrefix = false`: URL 将是 `https://example.com/my-app/en`
|
|
310
|
+
- 如果 `noPrefix = true`: URL 将是 `https://example.com`
|
|
311
|
+
|
|
312
|
+
- **detectLocaleOnPrefetchNoPrefix**:
|
|
313
|
+
|
|
314
|
+
- _类型_: `boolean`
|
|
315
|
+
- _默认值_: `false`
|
|
316
|
+
- _描述_: 控制是否在 Next.js 预取请求期间进行语言环境检测。
|
|
317
|
+
- _示例_: `true`
|
|
318
|
+
- _注意_: 此设置影响 Next.js 处理语言环境预取的方式:
|
|
319
|
+
- **示例场景:**
|
|
320
|
+
- 用户的浏览器语言是 `'fr'`
|
|
321
|
+
- 当前页面是 `/fr/about`
|
|
322
|
+
- 链接预取 `/about`
|
|
323
|
+
- **使用 `detectLocaleOnPrefetchNoPrefix: true`:**
|
|
324
|
+
- 预取从浏览器检测到 `'fr'` 语言环境
|
|
325
|
+
- 将预取重定向到 `/fr/about`
|
|
326
|
+
- **使用 `detectLocaleOnPrefetchNoPrefix: false` (默认):**
|
|
327
|
+
- 预取使用默认语言环境
|
|
328
|
+
- 将预取重定向到 `/en/about` (假设 `'en'` 是默认值)
|
|
329
|
+
- **何时使用 `true`:**
|
|
330
|
+
- 您的应用程序使用非本地化的内部链接 (例如 `<a href="/about">`)
|
|
331
|
+
- 您希望在常规请求和预取请求之间保持一致的语言环境检测行为
|
|
332
|
+
- **何时使用 `false` (默认):**
|
|
333
|
+
- 您的应用程序使用带语言环境前缀的链接 (例如 `<a href="/fr/about">`)
|
|
334
|
+
- 您希望优化预取性能
|
|
335
|
+
- 您希望避免潜在的重定向循环
|
|
299
336
|
|
|
300
337
|
---
|
|
301
338
|
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2024-08-11
|
|
3
|
+
updatedAt: 2025-06-29
|
|
4
|
+
title: useI18n Hook 文档 | react-intlayer
|
|
5
|
+
description: 学习如何在 react-intlayer 包中使用 useI18n Hook
|
|
6
|
+
keywords:
|
|
7
|
+
- useI18n
|
|
8
|
+
- i18n
|
|
9
|
+
- 翻译
|
|
10
|
+
- 词典
|
|
11
|
+
- Intlayer
|
|
12
|
+
- 国际化
|
|
13
|
+
- 文档
|
|
14
|
+
- Next.js
|
|
15
|
+
- JavaScript
|
|
16
|
+
- React
|
|
17
|
+
slugs:
|
|
18
|
+
- doc
|
|
19
|
+
- packages
|
|
20
|
+
- react-intlayer
|
|
21
|
+
- useI18n
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# React 集成:`useI18n` Hook 文档
|
|
25
|
+
|
|
26
|
+
本节详细介绍如何在 React 应用中使用 `useI18n` Hook,实现高效的内容本地化。
|
|
27
|
+
|
|
28
|
+
## 在 React 中导入 `useI18n`
|
|
29
|
+
|
|
30
|
+
可以根据不同的上下文,将 `useI18n` Hook 导入并集成到 React 应用中,具体如下:
|
|
31
|
+
|
|
32
|
+
- **客户端组件:**
|
|
33
|
+
|
|
34
|
+
```typescript codeFormat="typescript"
|
|
35
|
+
import { useI18n } from "react-intlayer"; // 在客户端 React 组件中使用
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```javascript codeFormat="esm"
|
|
39
|
+
import { useI18n } from "react-intlayer"; // 在客户端 React 组件中使用
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```javascript codeFormat="commonjs"
|
|
43
|
+
const { useI18n } = require("react-intlayer"); // 在客户端 React 组件中使用
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **服务器组件:**
|
|
47
|
+
|
|
48
|
+
```typescript codeFormat="commonjs"
|
|
49
|
+
import { useI18n } from "react-intlayer/server"; // 在服务器端 React 组件中使用
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```javascript codeFormat="esm"
|
|
53
|
+
import { useI18n } from "react-intlayer/server"; // 在服务器端 React 组件中使用
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```javascript codeFormat="commonjs"
|
|
57
|
+
const { useI18n } = require("react-intlayer/server"); // 在服务器端 React 组件中使用
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 参数
|
|
61
|
+
|
|
62
|
+
此钩子接受两个参数:
|
|
63
|
+
|
|
64
|
+
1. **`namespace`**:用于限定翻译键的字典命名空间。
|
|
65
|
+
2. **`locale`**(可选):期望使用的语言环境。如果未指定,则默认使用上下文的语言环境。
|
|
66
|
+
|
|
67
|
+
## 字典
|
|
68
|
+
|
|
69
|
+
所有字典键必须在内容声明文件中声明,以增强类型安全并防止错误。[配置说明请参见此处](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/get_started.md)。
|
|
70
|
+
|
|
71
|
+
## React 中的使用示例
|
|
72
|
+
|
|
73
|
+
以下是在 React 组件中使用 `useI18n` 钩子的示例:
|
|
74
|
+
|
|
75
|
+
```tsx fileName="src/App.tsx" codeFormat="typescript"
|
|
76
|
+
import type { FC } from "react";
|
|
77
|
+
import { ClientComponentExample, ServerComponentExample } from "@components";
|
|
78
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
79
|
+
import { useI18n, IntlayerServerProvider } from "react-intlayer/server";
|
|
80
|
+
import { Locales } from "intlayer";
|
|
81
|
+
|
|
82
|
+
const App: FC<{ locale: Locales }> = ({ locale }) => {
|
|
83
|
+
const t = useI18n("home-page", locale);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<>
|
|
87
|
+
<p>{t("introduction")}</p>
|
|
88
|
+
<IntlayerProvider locale={locale}>
|
|
89
|
+
<ClientComponentExample />
|
|
90
|
+
</IntlayerProvider>
|
|
91
|
+
<IntlayerServerProvider locale={locale}>
|
|
92
|
+
<ServerComponentExample />
|
|
93
|
+
</IntlayerServerProvider>
|
|
94
|
+
</>
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```jsx fileName="src/app.jsx" codeFormat="esm"
|
|
100
|
+
import { ClientComponentExample, ServerComponentExample } from "@components";
|
|
101
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
102
|
+
import { IntlayerServerProvider, useI18n } from "react-intlayer/server";
|
|
103
|
+
|
|
104
|
+
const App = ({ locale }) => {
|
|
105
|
+
const t = useI18n("home-page", locale);
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<>
|
|
109
|
+
<p>{t("introduction")}</p>
|
|
110
|
+
<IntlayerProvider locale={locale}>
|
|
111
|
+
<ClientComponentExample />
|
|
112
|
+
</IntlayerProvider>
|
|
113
|
+
<IntlayerServerProvider locale={locale}>
|
|
114
|
+
<ServerComponentExample />
|
|
115
|
+
</IntlayerServerProvider>
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```jsx fileName="src/app.cjs" codeFormat="commonjs"
|
|
122
|
+
const { IntlayerProvider } = require("react-intlayer");
|
|
123
|
+
const { IntlayerServerProvider, useI18n } = require("react-intlayer/server");
|
|
124
|
+
|
|
125
|
+
const App = ({ locale }) => {
|
|
126
|
+
const t = useI18n("home-page", locale);
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<>
|
|
130
|
+
<p>{t("introduction")}</p>
|
|
131
|
+
<IntlayerProvider locale={locale}>
|
|
132
|
+
<ClientComponentExample />
|
|
133
|
+
</IntlayerProvider>
|
|
134
|
+
<IntlayerServerProvider locale={locale}>
|
|
135
|
+
<ServerComponentExample />
|
|
136
|
+
</IntlayerServerProvider>
|
|
137
|
+
</>
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
```tsx fileName="src/components/ComponentExample.tsx" codeFormat="typescript"
|
|
143
|
+
import type { FC } from "react";
|
|
144
|
+
import { useI18n } from "react-intlayer";
|
|
145
|
+
|
|
146
|
+
const ComponentExample: FC = () => {
|
|
147
|
+
const t = useI18n("component-example");
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div>
|
|
151
|
+
<h1>{t("title")}</h1> {/* 显示标题 */}
|
|
152
|
+
<p>{t("description")}</p> {/* 显示描述 */}
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```jsx fileName="src/components/ComponentExample.jsx" codeFormat="esm"
|
|
159
|
+
import { useI18n } from "react-intlayer";
|
|
160
|
+
|
|
161
|
+
const ComponentExample = () => {
|
|
162
|
+
const t = useI18n("component-example");
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div>
|
|
166
|
+
<h1>{t("title")}</h1> {/* 显示标题 */}
|
|
167
|
+
<p>{t("description")}</p> {/* 显示描述 */}
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
```jsx fileName="src/components/ComponentExample.cjs" codeFormat="commonjs"
|
|
174
|
+
const { useI18n } = require("react-intlayer");
|
|
175
|
+
|
|
176
|
+
const ComponentExample = () => {
|
|
177
|
+
const t = useI18n("component-example");
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<div>
|
|
181
|
+
<h1>{t("title")}</h1> {/* 显示标题 */}
|
|
182
|
+
<p>{t("description")}</p> {/* 显示描述 */}
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
};
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```tsx fileName="src/components/ServerComponentExample.tsx" codeFormat="typescript"
|
|
189
|
+
import { useI18n } from "react-intlayer/server";
|
|
190
|
+
|
|
191
|
+
const ServerComponentExample = () => {
|
|
192
|
+
const t = useI18n("server-component");
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div>
|
|
196
|
+
<h1>{t("title")}</h1> {/* 显示标题 */}
|
|
197
|
+
<p>{t("description")}</p> {/* 显示描述 */}
|
|
198
|
+
</div>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
```jsx fileName="src/components/ServerComponentExample.jsx" codeFormat="esm"
|
|
204
|
+
import { useI18n } from "react-intlayer/server";
|
|
205
|
+
|
|
206
|
+
const ServerComponentExample = () => {
|
|
207
|
+
const t = useI18n("server-component");
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<div>
|
|
211
|
+
<h1>{t("title")}</h1> {/* 显示标题 */}
|
|
212
|
+
<p>{t("description")}</p> {/* 显示描述 */}
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
```jsx fileName="src/components/ServerComponentExample.cjs" codeFormat="commonjs"
|
|
219
|
+
const { useI18n } = require("react-intlayer/server");
|
|
220
|
+
|
|
221
|
+
const ServerComponentExample = () => {
|
|
222
|
+
const t = useI18n("server-component");
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<div>
|
|
226
|
+
<h1>{t("title")}</h1>
|
|
227
|
+
<p>{t("description")}</p>
|
|
228
|
+
</div>
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## 属性处理
|
|
234
|
+
|
|
235
|
+
在本地化属性时,请适当地访问翻译值:
|
|
236
|
+
|
|
237
|
+
```jsx
|
|
238
|
+
<!-- 对于无障碍属性(例如 aria-label),请使用 .value,因为需要纯字符串 -->
|
|
239
|
+
<button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## 额外资源
|
|
243
|
+
|
|
244
|
+
- **Intlayer 可视化编辑器**:为了获得更直观的内容管理体验,请参阅可视化编辑器文档 [这里](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_visual_editor.md)。
|
|
245
|
+
|
|
246
|
+
本节特别涵盖了在 React 应用中集成 `useI18n` 钩子,简化本地化流程并确保不同语言环境下内容的一致性。
|
|
247
|
+
|
|
248
|
+
## 文档历史
|
|
249
|
+
|
|
250
|
+
- 6.0.0 - 2025-06-29:`useI18n` 钩子文档的初始编写
|
|
@@ -17,7 +17,7 @@ keywords:
|
|
|
17
17
|
slugs:
|
|
18
18
|
- doc
|
|
19
19
|
- faq
|
|
20
|
-
-
|
|
20
|
+
- customized-locale-list
|
|
21
21
|
---
|
|
22
22
|
|
|
23
23
|
# Ist es möglich, einen Sprachtyp wie Englisch zu blockieren? Ich füge Englisch in meine Wörterbücher ein, möchte Englisch aber noch nicht auf der Webseite verfügbar machen
|