@intlayer/docs 7.5.12 → 7.5.13
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/uk/compiler_vs_declarative_i18n.md +224 -0
- package/blog/uk/i18n_using_next-i18next.md +1086 -0
- package/blog/uk/i18n_using_next-intl.md +760 -0
- package/blog/uk/index.md +69 -0
- package/blog/uk/internationalization_and_SEO.md +273 -0
- package/blog/uk/intlayer_with_i18next.md +211 -0
- package/blog/uk/intlayer_with_next-i18next.md +202 -0
- package/blog/uk/intlayer_with_next-intl.md +203 -0
- package/blog/uk/intlayer_with_react-i18next.md +200 -0
- package/blog/uk/intlayer_with_react-intl.md +202 -0
- package/blog/uk/intlayer_with_vue-i18n.md +206 -0
- package/blog/uk/l10n_platform_alternative/Lokalise.md +80 -0
- package/blog/uk/l10n_platform_alternative/crowdin.md +80 -0
- package/blog/uk/l10n_platform_alternative/phrase.md +78 -0
- package/blog/uk/list_i18n_technologies/CMS/drupal.md +143 -0
- package/blog/uk/list_i18n_technologies/CMS/wix.md +167 -0
- package/blog/uk/list_i18n_technologies/CMS/wordpress.md +189 -0
- package/blog/uk/list_i18n_technologies/frameworks/angular.md +125 -0
- package/blog/uk/list_i18n_technologies/frameworks/flutter.md +128 -0
- package/blog/uk/list_i18n_technologies/frameworks/react-native.md +217 -0
- package/blog/uk/list_i18n_technologies/frameworks/react.md +155 -0
- package/blog/uk/list_i18n_technologies/frameworks/svelte.md +145 -0
- package/blog/uk/list_i18n_technologies/frameworks/vue.md +144 -0
- package/blog/uk/next-i18next_vs_next-intl_vs_intlayer.md +1499 -0
- package/blog/uk/nextjs-multilingual-seo-comparison.md +360 -0
- package/blog/uk/rag_powered_documentation_assistant.md +288 -0
- package/blog/uk/react-i18next_vs_react-intl_vs_intlayer.md +164 -0
- package/blog/uk/vue-i18n_vs_intlayer.md +279 -0
- package/blog/uk/what_is_internationalization.md +167 -0
- package/dist/cjs/generated/frequentQuestions.entry.cjs +20 -0
- package/dist/cjs/generated/frequentQuestions.entry.cjs.map +1 -1
- package/dist/esm/generated/frequentQuestions.entry.mjs +20 -0
- package/dist/esm/generated/frequentQuestions.entry.mjs.map +1 -1
- package/dist/types/generated/frequentQuestions.entry.d.ts +1 -0
- package/dist/types/generated/frequentQuestions.entry.d.ts.map +1 -1
- package/docs/ar/configuration.md +6 -1
- package/docs/ar/dictionary/content_file.md +6 -1
- package/docs/de/configuration.md +6 -1
- package/docs/de/dictionary/content_file.md +6 -1
- package/docs/en/configuration.md +6 -1
- package/docs/en/dictionary/content_file.md +6 -1
- package/docs/en-GB/configuration.md +6 -1
- package/docs/en-GB/dictionary/content_file.md +3 -1
- package/docs/es/configuration.md +6 -1
- package/docs/es/dictionary/content_file.md +6 -1
- package/docs/fr/configuration.md +6 -1
- package/docs/fr/dictionary/content_file.md +3 -1
- package/docs/hi/configuration.md +6 -1
- package/docs/hi/dictionary/content_file.md +3 -1
- package/docs/id/configuration.md +6 -1
- package/docs/id/dictionary/content_file.md +3 -1
- package/docs/it/configuration.md +6 -1
- package/docs/it/dictionary/content_file.md +3 -1
- package/docs/ja/configuration.md +6 -1
- package/docs/ja/dictionary/content_file.md +3 -1
- package/docs/ko/configuration.md +6 -1
- package/docs/ko/dictionary/content_file.md +3 -1
- package/docs/pl/configuration.md +3 -1
- package/docs/pl/dictionary/content_file.md +3 -1
- package/docs/pt/configuration.md +6 -1
- package/docs/pt/dictionary/content_file.md +3 -1
- package/docs/ru/configuration.md +6 -1
- package/docs/ru/dictionary/content_file.md +6 -1
- package/docs/tr/configuration.md +6 -1
- package/docs/tr/dictionary/content_file.md +3 -1
- package/docs/uk/CI_CD.md +198 -0
- package/docs/uk/autoFill.md +307 -0
- package/docs/uk/bundle_optimization.md +185 -0
- package/docs/uk/cli/build.md +64 -0
- package/docs/uk/cli/ci.md +137 -0
- package/docs/uk/cli/configuration.md +63 -0
- package/docs/uk/cli/debug.md +46 -0
- package/docs/uk/cli/doc-review.md +43 -0
- package/docs/uk/cli/doc-translate.md +132 -0
- package/docs/uk/cli/editor.md +28 -0
- package/docs/uk/cli/fill.md +130 -0
- package/docs/uk/cli/index.md +190 -0
- package/docs/uk/cli/init.md +84 -0
- package/docs/uk/cli/list.md +90 -0
- package/docs/uk/cli/list_projects.md +128 -0
- package/docs/uk/cli/live.md +41 -0
- package/docs/uk/cli/login.md +157 -0
- package/docs/uk/cli/pull.md +78 -0
- package/docs/uk/cli/push.md +98 -0
- package/docs/uk/cli/sdk.md +71 -0
- package/docs/uk/cli/test.md +76 -0
- package/docs/uk/cli/transform.md +65 -0
- package/docs/uk/cli/version.md +24 -0
- package/docs/uk/cli/watch.md +37 -0
- package/docs/uk/configuration.md +742 -0
- package/docs/uk/dictionary/condition.md +237 -0
- package/docs/uk/dictionary/content_file.md +1134 -0
- package/docs/uk/dictionary/enumeration.md +245 -0
- package/docs/uk/dictionary/file.md +232 -0
- package/docs/uk/dictionary/function_fetching.md +212 -0
- package/docs/uk/dictionary/gender.md +273 -0
- package/docs/uk/dictionary/insertion.md +187 -0
- package/docs/uk/dictionary/markdown.md +383 -0
- package/docs/uk/dictionary/nesting.md +273 -0
- package/docs/uk/dictionary/translation.md +332 -0
- package/docs/uk/formatters.md +595 -0
- package/docs/uk/how_works_intlayer.md +256 -0
- package/docs/uk/index.md +175 -0
- package/docs/uk/interest_of_intlayer.md +297 -0
- package/docs/uk/intlayer_CMS.md +569 -0
- package/docs/uk/intlayer_visual_editor.md +292 -0
- package/docs/uk/intlayer_with_angular.md +710 -0
- package/docs/uk/intlayer_with_astro.md +256 -0
- package/docs/uk/intlayer_with_create_react_app.md +1258 -0
- package/docs/uk/intlayer_with_express.md +429 -0
- package/docs/uk/intlayer_with_fastify.md +446 -0
- package/docs/uk/intlayer_with_lynx+react.md +548 -0
- package/docs/uk/intlayer_with_nestjs.md +283 -0
- package/docs/uk/intlayer_with_next-i18next.md +640 -0
- package/docs/uk/intlayer_with_next-intl.md +456 -0
- package/docs/uk/intlayer_with_nextjs_page_router.md +1541 -0
- package/docs/uk/intlayer_with_nuxt.md +711 -0
- package/docs/uk/intlayer_with_react_router_v7.md +600 -0
- package/docs/uk/intlayer_with_react_router_v7_fs_routes.md +669 -0
- package/docs/uk/intlayer_with_svelte_kit.md +579 -0
- package/docs/uk/intlayer_with_tanstack.md +818 -0
- package/docs/uk/intlayer_with_vite+preact.md +1748 -0
- package/docs/uk/intlayer_with_vite+react.md +1449 -0
- package/docs/uk/intlayer_with_vite+solid.md +302 -0
- package/docs/uk/intlayer_with_vite+svelte.md +520 -0
- package/docs/uk/intlayer_with_vite+vue.md +1113 -0
- package/docs/uk/introduction.md +222 -0
- package/docs/uk/locale_mapper.md +242 -0
- package/docs/uk/mcp_server.md +211 -0
- package/docs/uk/packages/express-intlayer/t.md +465 -0
- package/docs/uk/packages/intlayer/getEnumeration.md +159 -0
- package/docs/uk/packages/intlayer/getHTMLTextDir.md +121 -0
- package/docs/uk/packages/intlayer/getLocaleLang.md +81 -0
- package/docs/uk/packages/intlayer/getLocaleName.md +135 -0
- package/docs/uk/packages/intlayer/getLocalizedUrl.md +338 -0
- package/docs/uk/packages/intlayer/getMultilingualUrls.md +359 -0
- package/docs/uk/packages/intlayer/getPathWithoutLocale.md +75 -0
- package/docs/uk/packages/intlayer/getPrefix.md +213 -0
- package/docs/uk/packages/intlayer/getTranslation.md +190 -0
- package/docs/uk/packages/intlayer/getTranslationContent.md +189 -0
- package/docs/uk/packages/next-intlayer/t.md +365 -0
- package/docs/uk/packages/next-intlayer/useDictionary.md +276 -0
- package/docs/uk/packages/next-intlayer/useIntlayer.md +263 -0
- package/docs/uk/packages/next-intlayer/useLocale.md +166 -0
- package/docs/uk/packages/react-intlayer/t.md +311 -0
- package/docs/uk/packages/react-intlayer/useDictionary.md +295 -0
- package/docs/uk/packages/react-intlayer/useI18n.md +250 -0
- package/docs/uk/packages/react-intlayer/useIntlayer.md +251 -0
- package/docs/uk/packages/react-intlayer/useLocale.md +210 -0
- package/docs/uk/per_locale_file.md +345 -0
- package/docs/uk/plugins/sync-json.md +398 -0
- package/docs/uk/readme.md +265 -0
- package/docs/uk/releases/v6.md +305 -0
- package/docs/uk/releases/v7.md +624 -0
- package/docs/uk/roadmap.md +346 -0
- package/docs/uk/testing.md +204 -0
- package/docs/vi/configuration.md +6 -1
- package/docs/vi/dictionary/content_file.md +6 -1
- package/docs/zh/configuration.md +6 -1
- package/docs/zh/dictionary/content_file.md +6 -1
- package/frequent_questions/ar/error-vite-env-only.md +77 -0
- package/frequent_questions/de/error-vite-env-only.md +77 -0
- package/frequent_questions/en/error-vite-env-only.md +77 -0
- package/frequent_questions/en-GB/error-vite-env-only.md +77 -0
- package/frequent_questions/es/error-vite-env-only.md +76 -0
- package/frequent_questions/fr/error-vite-env-only.md +77 -0
- package/frequent_questions/hi/error-vite-env-only.md +77 -0
- package/frequent_questions/id/error-vite-env-only.md +77 -0
- package/frequent_questions/it/error-vite-env-only.md +77 -0
- package/frequent_questions/ja/error-vite-env-only.md +77 -0
- package/frequent_questions/ko/error-vite-env-only.md +77 -0
- package/frequent_questions/pl/error-vite-env-only.md +77 -0
- package/frequent_questions/pt/error-vite-env-only.md +77 -0
- package/frequent_questions/ru/error-vite-env-only.md +77 -0
- package/frequent_questions/tr/error-vite-env-only.md +77 -0
- package/frequent_questions/uk/SSR_Next_no_[locale].md +104 -0
- package/frequent_questions/uk/array_as_content_declaration.md +72 -0
- package/frequent_questions/uk/build_dictionaries.md +58 -0
- package/frequent_questions/uk/build_error_CI_CD.md +74 -0
- package/frequent_questions/uk/bun_set_up.md +53 -0
- package/frequent_questions/uk/customized_locale_list.md +64 -0
- package/frequent_questions/uk/domain_routing.md +113 -0
- package/frequent_questions/uk/error-vite-env-only.md +77 -0
- package/frequent_questions/uk/esbuild_error.md +29 -0
- package/frequent_questions/uk/get_locale_cookie.md +142 -0
- package/frequent_questions/uk/intlayer_command_undefined.md +155 -0
- package/frequent_questions/uk/locale_incorect_in_url.md +73 -0
- package/frequent_questions/uk/package_version_error.md +181 -0
- package/frequent_questions/uk/static_rendering.md +44 -0
- package/frequent_questions/uk/translated_path_url.md +55 -0
- package/frequent_questions/uk/unknown_command.md +97 -0
- package/frequent_questions/vi/error-vite-env-only.md +77 -0
- package/frequent_questions/zh/error-vite-env-only.md +77 -0
- package/legal/uk/privacy_notice.md +83 -0
- package/legal/uk/terms_of_service.md +55 -0
- package/package.json +6 -6
- package/src/generated/frequentQuestions.entry.ts +20 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-08-23
|
|
3
|
+
updatedAt: 2025-08-23
|
|
4
|
+
title: Документація: функція `t` у `react-intlayer`
|
|
5
|
+
description: Дивіться, як використовувати функцію t у пакеті react-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- t
|
|
8
|
+
- переклад
|
|
9
|
+
- Intlayer
|
|
10
|
+
- Інтернаціоналізація
|
|
11
|
+
- Документація
|
|
12
|
+
- Next.js
|
|
13
|
+
- JavaScript
|
|
14
|
+
- React
|
|
15
|
+
slugs:
|
|
16
|
+
- doc
|
|
17
|
+
- packages
|
|
18
|
+
- react-intlayer
|
|
19
|
+
- t
|
|
20
|
+
history:
|
|
21
|
+
- version: 5.5.10
|
|
22
|
+
date: 2025-06-29
|
|
23
|
+
changes: Ініціалізація історії
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
# Документація: функція `t` у `react-intlayer`
|
|
27
|
+
|
|
28
|
+
Функція `t` у пакеті `react-intlayer` — це базовий інструмент для вбудованої інтернаціоналізації у вашому React-додатку. Вона дозволяє визначати переклади безпосередньо в компонентах, що спрощує відображення локалізованого контенту залежно від поточної локалі.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Огляд
|
|
33
|
+
|
|
34
|
+
Функція `t` використовується для надання перекладів для різних локалей безпосередньо у ваших компонентах. Передаючи об'єкт, що містить переклади для кожної підтримуваної локалі, `t` повертає відповідний переклад на основі поточного контексту локалі у вашій React-аплікації.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Основні можливості
|
|
39
|
+
|
|
40
|
+
- **Вбудовані переклади**: Ідеально підходить для швидкого, вбудованого тексту, який не вимагає окремого оголошення контенту.
|
|
41
|
+
- **Автоматичний вибір локалі**: Автоматично повертає переклад, що відповідає поточній локалі.
|
|
42
|
+
- **Підтримка TypeScript**: Забезпечує типобезпеку та автодоповнення при використанні з TypeScript.
|
|
43
|
+
- **Легка інтеграція**: Працює безшовно в React-компонентах.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Сигнатура функції
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
t<T extends string>(content: Record<LocalesValues, T>, locale?: Locales): string
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Параметри
|
|
54
|
+
|
|
55
|
+
- `translations`: Об'єкт, де ключі — коди локалей (наприклад, `en`, `fr`, `es`), а значення — відповідні перекладені рядки.
|
|
56
|
+
|
|
57
|
+
### Повертає
|
|
58
|
+
|
|
59
|
+
- Рядок, що містить перекладений контент для поточної локалі.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Приклади використання
|
|
64
|
+
|
|
65
|
+
### Базове використання `t` в компоненті
|
|
66
|
+
|
|
67
|
+
```tsx fileName="src/components/ComponentExample.tsx" codeFormat="typescript"
|
|
68
|
+
import type { FC } from "react";
|
|
69
|
+
import { t } from "react-intlayer";
|
|
70
|
+
|
|
71
|
+
export const ComponentExample: FC = () => {
|
|
72
|
+
return (
|
|
73
|
+
<div>
|
|
74
|
+
<p>
|
|
75
|
+
{t({
|
|
76
|
+
uk: "Це приклад компонента",
|
|
77
|
+
en: "This is an example of a component",
|
|
78
|
+
fr: "Ceci est un exemple de composant",
|
|
79
|
+
es: "Este es un ejemplo de componente",
|
|
80
|
+
})}
|
|
81
|
+
</p>
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```jsx fileName="src/components/ComponentExample.mjx" codeFormat="esm"
|
|
88
|
+
import { t } from "react-intlayer";
|
|
89
|
+
|
|
90
|
+
const ComponentExample = () => {
|
|
91
|
+
return (
|
|
92
|
+
<div>
|
|
93
|
+
<p>
|
|
94
|
+
{t({
|
|
95
|
+
uk: "Це приклад компонента",
|
|
96
|
+
en: "This is an example of a component",
|
|
97
|
+
fr: "Ceci est un exemple de composant",
|
|
98
|
+
es: "Este es un ejemplo de componente",
|
|
99
|
+
})}
|
|
100
|
+
</p>
|
|
101
|
+
</div>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```jsx fileName="src/components/ComponentExample.csx" codeFormat="commonjs"
|
|
107
|
+
const { t } = require("react-intlayer");
|
|
108
|
+
|
|
109
|
+
const ComponentExample = () => {
|
|
110
|
+
return (
|
|
111
|
+
<div>
|
|
112
|
+
<p>
|
|
113
|
+
{t({
|
|
114
|
+
uk: "Це приклад компонента",
|
|
115
|
+
en: "This is an example of a component",
|
|
116
|
+
fr: "Ceci est un exemple de composant",
|
|
117
|
+
es: "Este es un ejemplo de componente",
|
|
118
|
+
})}
|
|
119
|
+
</p>
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Вбудовані переклади в атрибутах
|
|
126
|
+
|
|
127
|
+
Функція `t` особливо корисна для вбудованих перекладів у JSX-атрибутах. При локалізації атрибутів, таких як `alt`, `title`, `href` або `aria-label`, ви можете використовувати `t` безпосередньо в атрибуті.
|
|
128
|
+
|
|
129
|
+
```jsx
|
|
130
|
+
<button
|
|
131
|
+
aria-label={t({
|
|
132
|
+
uk: "Відправити",
|
|
133
|
+
en: "Submit",
|
|
134
|
+
fr: "Soumettre",
|
|
135
|
+
es: "Enviar",
|
|
136
|
+
})}
|
|
137
|
+
>
|
|
138
|
+
{t({
|
|
139
|
+
uk: "Відправити",
|
|
140
|
+
en: "Submit",
|
|
141
|
+
fr: "Soumettre",
|
|
142
|
+
es: "Enviar",
|
|
143
|
+
})}
|
|
144
|
+
<img
|
|
145
|
+
src="/path/to/image"
|
|
146
|
+
alt={t({
|
|
147
|
+
uk: "Чудовий пейзаж",
|
|
148
|
+
en: "A beautiful scenery",
|
|
149
|
+
fr: "Un beau paysage",
|
|
150
|
+
es: "Un hermoso paisaje",
|
|
151
|
+
})}
|
|
152
|
+
/>
|
|
153
|
+
</button>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Розширені теми
|
|
159
|
+
|
|
160
|
+
### Інтеграція з TypeScript
|
|
161
|
+
|
|
162
|
+
Функція `t` є типобезпечною при використанні з TypeScript, що гарантує наявність усіх необхідних локалей.
|
|
163
|
+
|
|
164
|
+
```typescript codeFormat="typescript"
|
|
165
|
+
import { t, type IConfigLocales } from "react-intlayer";
|
|
166
|
+
|
|
167
|
+
const translations: IConfigLocales<string> = {
|
|
168
|
+
uk: "Ласкаво просимо",
|
|
169
|
+
en: "Welcome",
|
|
170
|
+
fr: "Bienvenue",
|
|
171
|
+
es: "Bienvenido",
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const greeting = t(translations);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
```javascript codeFormat="esm"
|
|
178
|
+
import { t, type IConfigLocales } from "react-intlayer";
|
|
179
|
+
|
|
180
|
+
/** @type {import('react-intlayer').IConfigLocales<string>} */
|
|
181
|
+
const translations = {
|
|
182
|
+
uk: "Ласкаво просимо",
|
|
183
|
+
en: "Welcome",
|
|
184
|
+
fr: "Bienvenue",
|
|
185
|
+
es: "Bienvenido",
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const greeting = t(translations);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
```javascript codeFormat="commonjs"
|
|
192
|
+
const { t, type IConfigLocales } = require("react-intlayer");
|
|
193
|
+
|
|
194
|
+
/** @type {import('react-intlayer').IConfigLocales<string>} */
|
|
195
|
+
const translations = {
|
|
196
|
+
uk: "Ласкаво просимо",
|
|
197
|
+
en: "Welcome",
|
|
198
|
+
fr: "Bienvenue",
|
|
199
|
+
es: "Bienvenido",
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const greeting = t(translations);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Визначення локалі та контекст
|
|
206
|
+
|
|
207
|
+
У `react-intlayer` поточна локаль керується через `IntlayerProvider`. Переконайтеся, що цей провайдер обгортає ваші компоненти та що проп `locale` передається правильно.
|
|
208
|
+
|
|
209
|
+
#### Приклад:
|
|
210
|
+
|
|
211
|
+
```tsx fileName="src/app.tsx" codeFormat="typescript"
|
|
212
|
+
import type { FC } from "react";
|
|
213
|
+
import type { Locales } from "intlayer";
|
|
214
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
215
|
+
|
|
216
|
+
const App: FC<{ locale: Locales }> = ({ locale }) => (
|
|
217
|
+
<IntlayerProvider locale={locale}>
|
|
218
|
+
{/* Ваші компоненти тут */}
|
|
219
|
+
</IntlayerProvider>
|
|
220
|
+
);
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
```jsx fileName="src/app.mjx" codeFormat="esm"
|
|
224
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
225
|
+
|
|
226
|
+
const App = ({ locale }) => (
|
|
227
|
+
<IntlayerProvider locale={locale}>
|
|
228
|
+
{/* Ваші компоненти тут */}
|
|
229
|
+
</IntlayerProvider>
|
|
230
|
+
);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
```jsx fileName="src/app.csx" codeFormat="commonjs"
|
|
234
|
+
const { IntlayerProvider } = require("react-intlayer");
|
|
235
|
+
|
|
236
|
+
const App = ({ locale }) => (
|
|
237
|
+
<IntlayerProvider locale={locale}>
|
|
238
|
+
{/* Ваші компоненти тут */}
|
|
239
|
+
</IntlayerProvider>
|
|
240
|
+
);
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Поширені помилки та усунення несправностей
|
|
246
|
+
|
|
247
|
+
### `t` повертає undefined або неправильний переклад
|
|
248
|
+
|
|
249
|
+
- **Причина**: Поточна локаль неправильно встановлена або для поточної локалі відсутній переклад.
|
|
250
|
+
- **Рішення**:
|
|
251
|
+
- Переконайтеся, що `IntlayerProvider` правильно налаштований з відповідною `locale`.
|
|
252
|
+
- Переконайтеся, що ваш об'єкт перекладів містить усі необхідні локалі.
|
|
253
|
+
|
|
254
|
+
### Відсутні переклади в TypeScript
|
|
255
|
+
|
|
256
|
+
- **Причина**: Об'єкт перекладів не відповідає вимогам необхідних локалей, що призводить до помилок TypeScript.
|
|
257
|
+
- **Рішення**: Використовуйте тип `IConfigLocales` для забезпечення повноти ваших перекладів.
|
|
258
|
+
|
|
259
|
+
```typescript codeFormat="typescript"
|
|
260
|
+
const translations: IConfigLocales<string> = {
|
|
261
|
+
en: "Text",
|
|
262
|
+
fr: "Texte",
|
|
263
|
+
// es: 'Texto', // Відсутність 'es' спричинить помилку TypeScript
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const text = t(translations);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
```javascript codeFormat="esm"
|
|
270
|
+
const translations = {
|
|
271
|
+
en: "Text",
|
|
272
|
+
fr: "Texte",
|
|
273
|
+
// es: 'Texto', // Відсутність 'es' спричинить помилку TypeScript
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const text = t(translations);
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
```javascript codeFormat="commonjs"
|
|
280
|
+
const { t, type IConfigLocales } = require("react-intlayer");
|
|
281
|
+
|
|
282
|
+
/** @type {import('react-intlayer').IConfigLocales<string>} */
|
|
283
|
+
const translations = {
|
|
284
|
+
en: "Text",
|
|
285
|
+
fr: "Texte",
|
|
286
|
+
// es: 'Texto', // Відсутність 'es' спричинить помилку TypeScript
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const text = t(translations);
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## Поради для ефективного використання
|
|
295
|
+
|
|
296
|
+
1. **Використовуйте `t` для простих вбудованих перекладів**: Ідеально підходить для перекладу невеликих фрагментів тексту безпосередньо в ваших компонентах.
|
|
297
|
+
2. **Віддавайте перевагу `useIntlayer` для структурованого контенту**: Для складніших перекладів та повторного використання контенту визначайте його в declaration files і використовуйте `useIntlayer`.
|
|
298
|
+
3. **Послідовне передавання локалі**: Переконайтеся, що локаль послідовно передається по всьому застосунку через `IntlayerProvider`.
|
|
299
|
+
4. **Використовуйте TypeScript**: Застосовуйте типи TypeScript, щоб виявляти відсутні переклади та гарантувати типобезпеку.
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
## Висновок
|
|
304
|
+
|
|
305
|
+
Функція `t` у `react-intlayer` — потужний і зручний інструмент для керування вбудованими перекладами у ваших React-застосунках. Ефективно інтегруючи її, ви розширюєте можливості інтернаціоналізації свого додатка та забезпечуєте кращий досвід для користувачів по всьому світу.
|
|
306
|
+
|
|
307
|
+
Для докладнішого опису використання та просунутих можливостей зверніться до [документації react-intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/intlayer_visual_editor.md).
|
|
308
|
+
|
|
309
|
+
---
|
|
310
|
+
|
|
311
|
+
**Примітка**: Не забудьте належним чином налаштувати `IntlayerProvider`, щоб поточна локаль правильно передавалася вашим компонентам. Це критично важливо для того, щоб функція `t` повертала коректні переклади.
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-02-07
|
|
3
|
+
updatedAt: 2025-06-29
|
|
4
|
+
title: Хук useDictionary — документація React Intlayer
|
|
5
|
+
description: Повний посібник із використання хуку useDictionary у React-застосунках з Intlayer для ефективної роботи з локалізованим контентом без візуального редактора.
|
|
6
|
+
keywords:
|
|
7
|
+
- useDictionary
|
|
8
|
+
- React
|
|
9
|
+
- hook
|
|
10
|
+
- intlayer
|
|
11
|
+
- localization
|
|
12
|
+
- i18n
|
|
13
|
+
- dictionary
|
|
14
|
+
- translation
|
|
15
|
+
slugs:
|
|
16
|
+
- doc
|
|
17
|
+
- package
|
|
18
|
+
- react-intlayer
|
|
19
|
+
- useDictionary
|
|
20
|
+
history:
|
|
21
|
+
- version: 5.5.10
|
|
22
|
+
date: 2025-06-29
|
|
23
|
+
changes: Ініціалізація історії
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
# Інтеграція з React: Документація хуку `useDictionary`
|
|
27
|
+
|
|
28
|
+
У цьому розділі наведено докладні вказівки щодо використання хуку `useDictionary` у React-застосунках, що дозволяє ефективно працювати з локалізованим контентом без використання візуального редактора.
|
|
29
|
+
|
|
30
|
+
## Імпорт `useDictionary` у React
|
|
31
|
+
|
|
32
|
+
Хук `useDictionary` можна інтегрувати в React‑додатки, імпортуючи його відповідно до контексту:
|
|
33
|
+
|
|
34
|
+
- **Клієнтський компонент:**
|
|
35
|
+
|
|
36
|
+
```typescript codeFormat="typescript"
|
|
37
|
+
import { useDictionary } from "react-intlayer"; // Використовується в клієнтських React-компонентах
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```javascript codeFormat="esm"
|
|
41
|
+
import { useDictionary } from "react-intlayer"; // Використовується в клієнтських React-компонентах
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```javascript codeFormat="commonjs"
|
|
45
|
+
const { useDictionary } = require("react-intlayer"); // Використовується в клієнтських React-компонентах
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- **Серверний компонент:**
|
|
49
|
+
|
|
50
|
+
```typescript codeFormat="typescript"
|
|
51
|
+
import { useDictionary } from "react-intlayer/server"; // Використовується в серверних React-компонентах
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```javascript codeFormat="esm"
|
|
55
|
+
import { useDictionary } from "react-intlayer/server"; // Використовується в серверних React-компонентах
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```javascript codeFormat="commonjs"
|
|
59
|
+
const { useDictionary } = require("react-intlayer/server"); // Використовується в серверних React-компонентах
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Параметри
|
|
63
|
+
|
|
64
|
+
Хук приймає два параметри:
|
|
65
|
+
|
|
66
|
+
1. **`dictionary`**: Оголошений об'єкт словника, що містить локалізований вміст для конкретних ключів.
|
|
67
|
+
2. **`locale`** (необов'язково): Бажана локаль. За замовчуванням використовується локаль поточного контексту, якщо не вказано.
|
|
68
|
+
|
|
69
|
+
## Словник
|
|
70
|
+
|
|
71
|
+
Усі об'єкти словника повинні бути оголошені у структурованих файлах вмісту, щоб забезпечити типобезпеку та запобігти помилкам під час виконання. Інструкції з налаштування можна знайти [тут](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/dictionary/content_file.md). Ось приклад декларації вмісту:
|
|
72
|
+
|
|
73
|
+
```typescript fileName="./component.content.ts" contentDeclarationFormat="typescript"
|
|
74
|
+
import { t, type Dictionary } from "intlayer";
|
|
75
|
+
|
|
76
|
+
const componentContent = {
|
|
77
|
+
key: "component-example",
|
|
78
|
+
content: {
|
|
79
|
+
title: t({
|
|
80
|
+
uk: "Приклад клієнтського компонента",
|
|
81
|
+
en: "Client Component Example",
|
|
82
|
+
fr: "Exemple de composant client",
|
|
83
|
+
es: "Ejemplo de componente cliente",
|
|
84
|
+
}),
|
|
85
|
+
content: t({
|
|
86
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
87
|
+
en: "This is the content of a client component example",
|
|
88
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
89
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
90
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
91
|
+
}),
|
|
92
|
+
},
|
|
93
|
+
} satisfies Dictionary;
|
|
94
|
+
|
|
95
|
+
export default componentContent;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```javascript fileName="./component.content.mjs" contentDeclarationFormat="esm"
|
|
99
|
+
import { t } from "intlayer";
|
|
100
|
+
|
|
101
|
+
/** @type {import('intlayer').Dictionary} */
|
|
102
|
+
const componentContent = {
|
|
103
|
+
key: "component-example",
|
|
104
|
+
content: {
|
|
105
|
+
title: t({
|
|
106
|
+
uk: "Приклад клієнтського компонента",
|
|
107
|
+
en: "Client Component Example",
|
|
108
|
+
fr: "Exemple de composant client",
|
|
109
|
+
es: "Ejemplo de componente cliente",
|
|
110
|
+
}),
|
|
111
|
+
content: t({
|
|
112
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
113
|
+
en: "This is the content of a client component example",
|
|
114
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
115
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
116
|
+
}),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default componentContent;
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```javascript fileName="./component.content.cjs" contentDeclarationFormat="commonjs"
|
|
124
|
+
const { t } = require("intlayer");
|
|
125
|
+
|
|
126
|
+
// Тип даних: словник Intlayer
|
|
127
|
+
/** @type {import('intlayer').Dictionary} */
|
|
128
|
+
const componentContent = {
|
|
129
|
+
key: "component-example",
|
|
130
|
+
content: {
|
|
131
|
+
title: t({
|
|
132
|
+
uk: "Приклад клієнтського компонента",
|
|
133
|
+
en: "Client Component Example",
|
|
134
|
+
fr: "Exemple de composant client",
|
|
135
|
+
es: "Ejemplo de componente cliente",
|
|
136
|
+
}),
|
|
137
|
+
content: t({
|
|
138
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
139
|
+
en: "This is the content of a client component example",
|
|
140
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
141
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
142
|
+
}),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
module.exports = componentContent;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```json fileName="./component.content.json" contentDeclarationFormat="json"
|
|
150
|
+
{
|
|
151
|
+
"$schema": "https://intlayer.org/schema.json",
|
|
152
|
+
"key": "component-example",
|
|
153
|
+
"content": {
|
|
154
|
+
"title": {
|
|
155
|
+
"nodeType": "translation",
|
|
156
|
+
"translation": {
|
|
157
|
+
"uk": "Приклад клієнтського компонента",
|
|
158
|
+
"en": "Client Component Example",
|
|
159
|
+
"fr": "Exemple de composant client",
|
|
160
|
+
"es": "Ejemplo de componente cliente"
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
"content": {
|
|
164
|
+
"nodeType": "translation",
|
|
165
|
+
"translation": {
|
|
166
|
+
"uk": "Це вміст прикладу клієнтського компонента",
|
|
167
|
+
"en": "This is the content of a client component example",
|
|
168
|
+
"fr": "Ceci est le contenu d'un exemple de composant client",
|
|
169
|
+
"es": "Este es el contenido de un ejemplo de componente cliente"
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Приклад використання в React
|
|
177
|
+
|
|
178
|
+
Нижче наведено приклад того, як використовувати хук `useDictionary` у React-компоненті:
|
|
179
|
+
|
|
180
|
+
```tsx fileName="./ComponentExample.tsx" codeFormat="typescript"
|
|
181
|
+
import type { FC } from "react";
|
|
182
|
+
import { useDictionary } from "react-intlayer";
|
|
183
|
+
import componentContent from "./component.content";
|
|
184
|
+
|
|
185
|
+
const ComponentExample: FC = () => {
|
|
186
|
+
const { title, content } = useDictionary(componentContent);
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<div>
|
|
190
|
+
<h1>{title}</h1>
|
|
191
|
+
<p>{content}</p>
|
|
192
|
+
</div>
|
|
193
|
+
);
|
|
194
|
+
};
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```jsx fileName="./ComponentExample.mjx" codeFormat="esm"
|
|
198
|
+
import { useDictionary } from "react-intlayer";
|
|
199
|
+
import componentContent from "./component.content";
|
|
200
|
+
|
|
201
|
+
const ComponentExample = () => {
|
|
202
|
+
const { title, content } = useDictionary(componentContent);
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<div>
|
|
206
|
+
<h1>{title}</h1>
|
|
207
|
+
<p>{content}</p>
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```jsx fileName="./ComponentExample.csx" codeFormat="commonjs"
|
|
214
|
+
const { useDictionary } = require("react-intlayer");
|
|
215
|
+
const componentContent = require("./component.content");
|
|
216
|
+
|
|
217
|
+
const ComponentExample = () => {
|
|
218
|
+
const { title, content } = useDictionary(componentContent);
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<div>
|
|
222
|
+
<h1>{title}</h1>
|
|
223
|
+
<p>{content}</p>
|
|
224
|
+
</div>
|
|
225
|
+
);
|
|
226
|
+
};
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Інтеграція на сервері
|
|
230
|
+
|
|
231
|
+
Якщо ви використовуєте хук `useDictionary` поза межами `IntlayerProvider`, локаль має бути явно передана як параметр під час рендерингу компонента:
|
|
232
|
+
|
|
233
|
+
```tsx fileName="./ServerComponentExample.tsx" codeFormat="typescript"
|
|
234
|
+
import type { FC } from "react";
|
|
235
|
+
import { useDictionary } from "react-intlayer/server";
|
|
236
|
+
import clientComponentExampleContent from "./component.content";
|
|
237
|
+
|
|
238
|
+
const ServerComponentExample: FC<{ locale: string }> = ({ locale }) => {
|
|
239
|
+
const { content } = useDictionary(clientComponentExampleContent, locale);
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<div>
|
|
243
|
+
<h1>{content.title}</h1>
|
|
244
|
+
<p>{content.content}</p>
|
|
245
|
+
</div>
|
|
246
|
+
);
|
|
247
|
+
};
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
```jsx fileName="./ServerComponentExample.mjx" codeFormat="esm"
|
|
251
|
+
import { useDictionary } from "react.intlayer/server";
|
|
252
|
+
import componentContent from "./component.content";
|
|
253
|
+
|
|
254
|
+
const ServerComponentExample = ({ locale }) => {
|
|
255
|
+
const { content } = useDictionary(componentContent, locale);
|
|
256
|
+
|
|
257
|
+
return (
|
|
258
|
+
<div>
|
|
259
|
+
<h1>{content.title}</h1>
|
|
260
|
+
<p>{content.content}</p>
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
};
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
```jsx fileName="./ServerComponentExample.csx" codeFormat="commonjs"
|
|
267
|
+
const { useDictionary } = require("react.intlayer/server");
|
|
268
|
+
const componentContent = require("./component.content");
|
|
269
|
+
|
|
270
|
+
const ServerComponentExample = ({ locale }) => {
|
|
271
|
+
const { content } = useDictionary(componentContent, locale);
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<div>
|
|
275
|
+
<h1>{content.title}</h1>
|
|
276
|
+
<p>{content.content}</p>
|
|
277
|
+
</div>
|
|
278
|
+
);
|
|
279
|
+
};
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Примітки щодо атрибутів
|
|
283
|
+
|
|
284
|
+
На відміну від інтеграцій з візуальними редакторами, атрибути на кшталт `buttonTitle.value` тут не застосовуються. Натомість безпосередньо звертайтеся до локалізованих рядків, як оголошено у вашому контенті.
|
|
285
|
+
|
|
286
|
+
```jsx
|
|
287
|
+
<button title={content.title}>{content.content}</button>
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## Додаткові поради
|
|
291
|
+
|
|
292
|
+
- **Type Safety**: Завжди використовуйте `Dictionary` для визначення ваших словників, щоб забезпечити Type Safety.
|
|
293
|
+
- **Localization Updates**: Під час оновлення контенту переконайтеся, що всі локалі узгоджені, щоб уникнути відсутніх перекладів.
|
|
294
|
+
|
|
295
|
+
Ця документація зосереджена на інтеграції хука `useDictionary`, надаючи оптимізований підхід до керування локалізованим контентом без покладання на функціональність візуальних редакторів.
|