@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,254 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2024-08-11
|
|
3
|
+
updatedAt: 2025-06-29
|
|
4
|
+
title: useI18n Hook Documentation | react-intlayer
|
|
5
|
+
description: Learn how to use the useI18n hook in the react-intlayer package
|
|
6
|
+
keywords:
|
|
7
|
+
- useI18n
|
|
8
|
+
- i18n
|
|
9
|
+
- translation
|
|
10
|
+
- dictionary
|
|
11
|
+
- Intlayer
|
|
12
|
+
- internationalisation
|
|
13
|
+
- documentation
|
|
14
|
+
- Next.js
|
|
15
|
+
- JavaScript
|
|
16
|
+
- React
|
|
17
|
+
slugs:
|
|
18
|
+
- doc
|
|
19
|
+
- packages
|
|
20
|
+
- react-intlayer
|
|
21
|
+
- useI18n
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# React Integration: `useI18n` Hook Documentation
|
|
25
|
+
|
|
26
|
+
This section provides detailed guidance on how to use the `useI18n` hook within React applications, enabling efficient content localisation.
|
|
27
|
+
|
|
28
|
+
## Importing `useI18n` in React
|
|
29
|
+
|
|
30
|
+
The `useI18n` hook can be imported and integrated into React applications according to the context as follows:
|
|
31
|
+
|
|
32
|
+
- **Client Components:**
|
|
33
|
+
|
|
34
|
+
```typescript codeFormat="typescript"
|
|
35
|
+
import { useI18n } from "react-intlayer"; // Use in client-side React components
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```javascript codeFormat="esm"
|
|
39
|
+
import { useI18n } from "react-intlayer"; // Use in client-side React components
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```javascript codeFormat="commonjs"
|
|
43
|
+
const { useI18n } = require("react-intlayer"); // Use in client-side React components
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **Server Components:**
|
|
47
|
+
|
|
48
|
+
```typescript codeFormat="commonjs"
|
|
49
|
+
import { useI18n } from "react-intlayer/server"; // Use in server-side React components
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```javascript codeFormat="esm"
|
|
53
|
+
import { useI18n } from "react-intlayer/server"; // Use in server-side React components
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```javascript codeFormat="commonjs"
|
|
57
|
+
const { useI18n } = require("react-intlayer/server"); // Use in server-side React components
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Parameters
|
|
61
|
+
|
|
62
|
+
This hook accepts two parameters:
|
|
63
|
+
|
|
64
|
+
1. **`namespace`**: A dictionary namespace to scope the translation keys.
|
|
65
|
+
2. **`locale`** (optional): The desired locale. If not specified, the context's locale will be used as default.
|
|
66
|
+
|
|
67
|
+
## Dictionary
|
|
68
|
+
|
|
69
|
+
All dictionary keys must be declared within content declaration files to enhance type safety and prevent errors. [Configuration instructions can be found here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/{{locale}}/dictionary/get_started.md).
|
|
70
|
+
|
|
71
|
+
## Usage Examples in React
|
|
72
|
+
|
|
73
|
+
Examples of using the `useI18n` hook within React components:
|
|
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> {/* Display the title */}
|
|
152
|
+
<p>{t("description")}</p> {/* Display the description */}
|
|
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> {/* Display the title */}
|
|
167
|
+
<p>{t("description")}</p> {/* Display the description */}
|
|
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> {/* Display the title */}
|
|
182
|
+
<p>{t("description")}</p> {/* Display the description */}
|
|
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> {/* Display the title */}
|
|
197
|
+
<p>{t("description")}</p> {/* Display the description */}
|
|
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> {/* Display the title */}
|
|
212
|
+
<p>{t("description")}</p> {/* Display the description */}
|
|
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
|
+
## Attribute Handling
|
|
234
|
+
|
|
235
|
+
When localising attributes, access the translation values appropriately:
|
|
236
|
+
|
|
237
|
+
```jsx
|
|
238
|
+
<button title={t("buttonTitle.value")}>{t("buttonText")}</button>;
|
|
239
|
+
|
|
240
|
+
{
|
|
241
|
+
/* For accessibility attributes (e.g., aria-label), use .value since pure strings are required */
|
|
242
|
+
}
|
|
243
|
+
<button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>;
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Additional Resources
|
|
247
|
+
|
|
248
|
+
- **Intlayer Visual Editor**: For a more intuitive content management experience, refer to the visual editor documentation [here](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_visual_editor.md).
|
|
249
|
+
|
|
250
|
+
This section specifically covers the integration of the `useI18n` hook in React applications, simplifying the localisation process and ensuring content consistency across different locales.
|
|
251
|
+
|
|
252
|
+
## Documentation History
|
|
253
|
+
|
|
254
|
+
- 6.0.0 - 29-06-2025: Initial writing of `useI18n` hook documentation
|
package/docs/es/configuration.md
CHANGED
|
@@ -268,10 +268,12 @@ Configuraciones que controlan el comportamiento del middleware, incluyendo cómo
|
|
|
268
268
|
- **prefixDefault**:
|
|
269
269
|
|
|
270
270
|
- _Tipo_: `boolean`
|
|
271
|
-
- _Por defecto_: `
|
|
271
|
+
- _Por defecto_: `false`
|
|
272
272
|
- _Descripción_: Indica si se debe incluir la configuración regional predeterminada en la URL.
|
|
273
|
-
- _Ejemplo_: `
|
|
274
|
-
- _Nota_:
|
|
273
|
+
- _Ejemplo_: `true`
|
|
274
|
+
- _Nota_:
|
|
275
|
+
- Si `true` y `defaultLocale = 'en'`: path = `/en/dashboard` o `/fr/dashboard`
|
|
276
|
+
- Si `false` y `defaultLocale = 'en'`: path = `/dashboard` o `/fr/dashboard`
|
|
275
277
|
|
|
276
278
|
- **basePath**:
|
|
277
279
|
|
|
@@ -279,7 +281,11 @@ Configuraciones que controlan el comportamiento del middleware, incluyendo cómo
|
|
|
279
281
|
- _Por defecto_: `''`
|
|
280
282
|
- _Descripción_: La ruta base para las URLs de la aplicación.
|
|
281
283
|
- _Ejemplo_: `'/my-app'`
|
|
282
|
-
- _Nota_:
|
|
284
|
+
- _Nota_:
|
|
285
|
+
- Si la aplicación está alojada en `https://example.com/my-app`
|
|
286
|
+
- La ruta base es `'/my-app'`
|
|
287
|
+
- La URL será `https://example.com/my-app/en`
|
|
288
|
+
- Si la ruta base no está configurada, la URL será `https://example.com/en`
|
|
283
289
|
|
|
284
290
|
- **serverSetCookie**:
|
|
285
291
|
|
|
@@ -291,11 +297,42 @@ Configuraciones que controlan el comportamiento del middleware, incluyendo cómo
|
|
|
291
297
|
- _Nota_: Controla si la cookie de configuración regional se establece en cada solicitud o nunca.
|
|
292
298
|
|
|
293
299
|
- **noPrefix**:
|
|
300
|
+
|
|
294
301
|
- _Tipo_: `boolean`
|
|
295
302
|
- _Por defecto_: `false`
|
|
296
303
|
- _Descripción_: Indica si se debe omitir el prefijo de configuración regional en las URLs.
|
|
297
304
|
- _Ejemplo_: `true`
|
|
298
|
-
- _Nota_:
|
|
305
|
+
- _Nota_:
|
|
306
|
+
- Si `true`: Sin prefijo en la URL
|
|
307
|
+
- Si `false`: Prefijo en la URL
|
|
308
|
+
- Ejemplo con `basePath = '/my-app'`:
|
|
309
|
+
- Si `noPrefix = false`: La URL será `https://example.com/my-app/en`
|
|
310
|
+
- Si `noPrefix = true`: La URL será `https://example.com`
|
|
311
|
+
|
|
312
|
+
- **detectLocaleOnPrefetchNoPrefix**:
|
|
313
|
+
|
|
314
|
+
- _Tipo_: `boolean`
|
|
315
|
+
- _Por defecto_: `false`
|
|
316
|
+
- _Descripción_: Controla si la detección de configuración regional ocurre durante las solicitudes de precarga de Next.js.
|
|
317
|
+
- _Ejemplo_: `true`
|
|
318
|
+
- _Nota_: Esta configuración afecta cómo Next.js maneja la precarga de configuración regional:
|
|
319
|
+
- **Escenario de ejemplo:**
|
|
320
|
+
- El idioma del navegador del usuario es `'fr'`
|
|
321
|
+
- La página actual es `/fr/about`
|
|
322
|
+
- El enlace precarga `/about`
|
|
323
|
+
- **Con `detectLocaleOnPrefetchNoPrefix: true`:**
|
|
324
|
+
- La precarga detecta la configuración regional `'fr'` desde el navegador
|
|
325
|
+
- Redirige la precarga a `/fr/about`
|
|
326
|
+
- **Con `detectLocaleOnPrefetchNoPrefix: false` (por defecto):**
|
|
327
|
+
- La precarga usa la configuración regional predeterminada
|
|
328
|
+
- Redirige la precarga a `/en/about` (asumiendo que `'en'` es la predeterminada)
|
|
329
|
+
- **Cuándo usar `true`:**
|
|
330
|
+
- Tu aplicación usa enlaces internos no localizados (ej. `<a href="/about">`)
|
|
331
|
+
- Quieres comportamiento consistente de detección de configuración regional entre solicitudes normales y de precarga
|
|
332
|
+
- **Cuándo usar `false` (por defecto):**
|
|
333
|
+
- Tu aplicación usa enlaces con prefijo de configuración regional (ej. `<a href="/fr/about">`)
|
|
334
|
+
- Quieres optimizar el rendimiento de precarga
|
|
335
|
+
- Quieres evitar bucles de redirección potenciales
|
|
299
336
|
|
|
300
337
|
---
|
|
301
338
|
|
package/docs/es/locale_mapper.md
CHANGED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2024-08-11
|
|
3
|
+
updatedAt: 2025-06-29
|
|
4
|
+
title: Documentación del Hook useI18n | react-intlayer
|
|
5
|
+
description: Aprende cómo usar el hook useI18n en el paquete react-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- useI18n
|
|
8
|
+
- i18n
|
|
9
|
+
- traducción
|
|
10
|
+
- diccionario
|
|
11
|
+
- Intlayer
|
|
12
|
+
- internacionalización
|
|
13
|
+
- documentación
|
|
14
|
+
- Next.js
|
|
15
|
+
- JavaScript
|
|
16
|
+
- React
|
|
17
|
+
slugs:
|
|
18
|
+
- doc
|
|
19
|
+
- packages
|
|
20
|
+
- react-intlayer
|
|
21
|
+
- useI18n
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Integración en React: Documentación del Hook `useI18n`
|
|
25
|
+
|
|
26
|
+
Esta sección proporciona una guía detallada sobre cómo usar el hook `useI18n` dentro de aplicaciones React, permitiendo una localización eficiente del contenido.
|
|
27
|
+
|
|
28
|
+
## Importando `useI18n` en React
|
|
29
|
+
|
|
30
|
+
El hook `useI18n` puede ser importado e integrado en aplicaciones React según el contexto de la siguiente manera:
|
|
31
|
+
|
|
32
|
+
- **Componentes Cliente:**
|
|
33
|
+
|
|
34
|
+
```typescript codeFormat="typescript"
|
|
35
|
+
import { useI18n } from "react-intlayer"; // Usar en componentes React del lado del cliente
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```javascript codeFormat="esm"
|
|
39
|
+
import { useI18n } from "react-intlayer"; // Usar en componentes React del lado del cliente
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```javascript codeFormat="commonjs"
|
|
43
|
+
const { useI18n } = require("react-intlayer"); // Usar en componentes React del lado del cliente
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- **Componentes Servidor:**
|
|
47
|
+
|
|
48
|
+
```typescript codeFormat="commonjs"
|
|
49
|
+
import { useI18n } from "react-intlayer/server"; // Usar en componentes React del lado del servidor
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```javascript codeFormat="esm"
|
|
53
|
+
import { useI18n } from "react-intlayer/server"; // Usar en componentes React del lado del servidor
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```javascript codeFormat="commonjs"
|
|
57
|
+
const { useI18n } = require("react-intlayer/server"); // Usar en componentes React del lado del servidor
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Parámetros
|
|
61
|
+
|
|
62
|
+
Este hook acepta dos parámetros:
|
|
63
|
+
|
|
64
|
+
1. **`namespace`**: Un espacio de nombres del diccionario para delimitar las claves de traducción.
|
|
65
|
+
2. **`locale`** (opcional): La configuración regional deseada. Si no se especifica, se usará la configuración regional del contexto por defecto.
|
|
66
|
+
|
|
67
|
+
## Diccionario
|
|
68
|
+
|
|
69
|
+
Todas las claves del diccionario deben declararse dentro de archivos de declaración de contenido para mejorar la seguridad de tipos y prevenir errores. [Las instrucciones de configuración se pueden encontrar aquí](https://github.com/aymericzip/intlayer/blob/main/docs/docs/es/dictionary/get_started.md).
|
|
70
|
+
|
|
71
|
+
## Ejemplos de uso en React
|
|
72
|
+
|
|
73
|
+
Ejemplos de uso del hook `useI18n` dentro de componentes 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> {/* Mostrar el título */}
|
|
152
|
+
<p>{t("description")}</p> {/* Mostrar la descripción */}
|
|
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> {/* Mostrar el título */}
|
|
167
|
+
<p>{t("description")}</p> {/* Mostrar la descripción */}
|
|
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> {/* Mostrar el título */}
|
|
182
|
+
<p>{t("description")}</p> {/* Mostrar la descripción */}
|
|
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> {/* Mostrar el título */}
|
|
197
|
+
<p>{t("description")}</p> {/* Mostrar la descripción */}
|
|
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> {/* Mostrar el título */}
|
|
212
|
+
<p>{t("description")}</p> {/* Mostrar la descripción */}
|
|
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
|
+
## Manejo de Atributos
|
|
234
|
+
|
|
235
|
+
Al localizar atributos, accede a los valores de traducción de manera adecuada:
|
|
236
|
+
|
|
237
|
+
```jsx
|
|
238
|
+
<!-- Para atributos de accesibilidad (por ejemplo, aria-label), usa .value ya que se requieren cadenas puras -->
|
|
239
|
+
<button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Recursos Adicionales
|
|
243
|
+
|
|
244
|
+
- **Editor Visual de Intlayer**: Para una experiencia de gestión de contenido más intuitiva, consulta la documentación del editor visual [aquí](https://github.com/aymericzip/intlayer/blob/main/docs/docs/es/intlayer_visual_editor.md).
|
|
245
|
+
|
|
246
|
+
Esta sección cubre específicamente la integración del hook `useI18n` en aplicaciones React, simplificando el proceso de localización y asegurando la consistencia del contenido a través de diferentes locales.
|
|
247
|
+
|
|
248
|
+
## Historial de la Documentación
|
|
249
|
+
|
|
250
|
+
- 6.0.0 - 2025-06-29: Escritura inicial de la documentación del hook `useI18n`
|
package/docs/fr/configuration.md
CHANGED
|
@@ -268,10 +268,12 @@ Paramètres qui contrôlent le comportement du middleware, y compris la gestion
|
|
|
268
268
|
- **prefixDefault** :
|
|
269
269
|
|
|
270
270
|
- _Type_ : `boolean`
|
|
271
|
-
- _Par défaut_ : `
|
|
272
|
-
- _Description_ : Indique s
|
|
273
|
-
- _Exemple_ : `
|
|
274
|
-
- _Remarque_ :
|
|
271
|
+
- _Par défaut_ : `false`
|
|
272
|
+
- _Description_ : Indique s'il faut inclure la langue par défaut dans l'URL.
|
|
273
|
+
- _Exemple_ : `true`
|
|
274
|
+
- _Remarque_ :
|
|
275
|
+
- Si `true` et `defaultLocale = 'en'` : path = `/en/dashboard` ou `/fr/dashboard`
|
|
276
|
+
- Si `false` et `defaultLocale = 'en'` : path = `/dashboard` ou `/fr/dashboard`
|
|
275
277
|
|
|
276
278
|
- **basePath** :
|
|
277
279
|
|
|
@@ -279,7 +281,11 @@ Paramètres qui contrôlent le comportement du middleware, y compris la gestion
|
|
|
279
281
|
- _Par défaut_ : `''`
|
|
280
282
|
- _Description_ : Le chemin de base pour les URL de l'application.
|
|
281
283
|
- _Exemple_ : `'/my-app'`
|
|
282
|
-
- _Remarque_ :
|
|
284
|
+
- _Remarque_ :
|
|
285
|
+
- Si l'application est hébergée sur `https://example.com/my-app`
|
|
286
|
+
- Le chemin de base est `'/my-app'`
|
|
287
|
+
- L'URL sera `https://example.com/my-app/en`
|
|
288
|
+
- Si le chemin de base n'est pas défini, l'URL sera `https://example.com/en`
|
|
283
289
|
|
|
284
290
|
- **serverSetCookie** :
|
|
285
291
|
|
|
@@ -291,11 +297,42 @@ Paramètres qui contrôlent le comportement du middleware, y compris la gestion
|
|
|
291
297
|
- _Remarque_ : Contrôle si le cookie de langue est défini à chaque requête ou jamais.
|
|
292
298
|
|
|
293
299
|
- **noPrefix** :
|
|
300
|
+
|
|
301
|
+
- _Type_ : `boolean`
|
|
302
|
+
- _Par défaut_ : `false`
|
|
303
|
+
- _Description_ : Indique s'il faut omettre le préfixe de langue dans les URL.
|
|
304
|
+
- _Exemple_ : `true`
|
|
305
|
+
- _Remarque_ :
|
|
306
|
+
- Si `true` : Pas de préfixe dans l'URL
|
|
307
|
+
- Si `false` : Préfixe dans l'URL
|
|
308
|
+
- Exemple avec `basePath = '/my-app'` :
|
|
309
|
+
- Si `noPrefix = false` : L'URL sera `https://example.com/my-app/en`
|
|
310
|
+
- Si `noPrefix = true` : L'URL sera `https://example.com`
|
|
311
|
+
|
|
312
|
+
- **detectLocaleOnPrefetchNoPrefix** :
|
|
313
|
+
|
|
294
314
|
- _Type_ : `boolean`
|
|
295
315
|
- _Par défaut_ : `false`
|
|
296
|
-
- _Description_ :
|
|
316
|
+
- _Description_ : Contrôle si la détection de langue se produit lors des requêtes de préchargement Next.js.
|
|
297
317
|
- _Exemple_ : `true`
|
|
298
|
-
- _Remarque_ :
|
|
318
|
+
- _Remarque_ : Ce paramètre affecte la façon dont Next.js gère le préchargement de langue :
|
|
319
|
+
- **Scénario d'exemple :**
|
|
320
|
+
- La langue du navigateur de l'utilisateur est `'fr'`
|
|
321
|
+
- La page actuelle est `/fr/about`
|
|
322
|
+
- Le lien précharge `/about`
|
|
323
|
+
- **Avec `detectLocaleOnPrefetchNoPrefix: true` :**
|
|
324
|
+
- Le préchargement détecte la langue `'fr'` depuis le navigateur
|
|
325
|
+
- Redirige le préchargement vers `/fr/about`
|
|
326
|
+
- **Avec `detectLocaleOnPrefetchNoPrefix: false` (par défaut) :**
|
|
327
|
+
- Le préchargement utilise la langue par défaut
|
|
328
|
+
- Redirige le préchargement vers `/en/about` (en supposant que `'en'` est la langue par défaut)
|
|
329
|
+
- **Quand utiliser `true` :**
|
|
330
|
+
- Votre application utilise des liens internes non localisés (ex. `<a href="/about">`)
|
|
331
|
+
- Vous voulez un comportement de détection de langue cohérent entre les requêtes normales et de préchargement
|
|
332
|
+
- **Quand utiliser `false` (par défaut) :**
|
|
333
|
+
- Votre application utilise des liens avec préfixe de langue (ex. `<a href="/fr/about">`)
|
|
334
|
+
- Vous voulez optimiser les performances de préchargement
|
|
335
|
+
- Vous voulez éviter les boucles de redirection potentielles
|
|
299
336
|
|
|
300
337
|
---
|
|
301
338
|
|