@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,365 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-08-23
|
|
3
|
+
updatedAt: 2025-08-23
|
|
4
|
+
title: Документація функції t | next-intlayer
|
|
5
|
+
description: Дізнайтеся, як використовувати функцію t у пакеті next-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- t
|
|
8
|
+
- переклад
|
|
9
|
+
- Intlayer
|
|
10
|
+
- next-intlayer
|
|
11
|
+
- інтернаціоналізація
|
|
12
|
+
- документація
|
|
13
|
+
- Next.js
|
|
14
|
+
- JavaScript
|
|
15
|
+
- React
|
|
16
|
+
slugs:
|
|
17
|
+
- doc
|
|
18
|
+
- packages
|
|
19
|
+
- next-intlayer
|
|
20
|
+
- t
|
|
21
|
+
history:
|
|
22
|
+
- version: 5.5.10
|
|
23
|
+
date: 2025-06-29
|
|
24
|
+
changes: Ініціалізовано історію
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
# Документація: `t` Функція в `next-intlayer`
|
|
28
|
+
|
|
29
|
+
Функція `t` у пакеті `next-intlayer` є базовим інструментом для вбудованої інтернаціоналізації у вашому додатку Next.js. Вона дозволяє визначати переклади безпосередньо в компонентах, що спрощує відображення локалізованого вмісту залежно від поточної локалі.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Огляд
|
|
34
|
+
|
|
35
|
+
Функція `t` використовується для надання перекладів для різних локалей безпосередньо у ваших компонентах. Передаючи об'єкт, що містить переклади для кожної підтримуваної локалі, `t` повертає відповідний переклад на основі поточного контексту локалі у вашому застосунку Next.js.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Ключові можливості
|
|
40
|
+
|
|
41
|
+
- **Вбудовані переклади**: Ідеально підходить для швидкого, вбудованого тексту, який не потребує окремого оголошення контенту.
|
|
42
|
+
- **Автоматичний вибір локалі**: Автоматично повертає переклад, що відповідає поточній локалі.
|
|
43
|
+
- **Підтримка TypeScript**: Забезпечує типобезпеку та автозаповнення при використанні з TypeScript.
|
|
44
|
+
- **Легка інтеграція**: Працює безшовно як у клієнтських, так і в серверних компонентах Next.js.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Сигнатура функції
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
t<T extends string>(content: Record<LocalesValues, T>, locale?: Locales): string
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Параметри
|
|
55
|
+
|
|
56
|
+
- `translations`: Об'єкт, де ключі — коди локалей (наприклад, `en`, `fr`, `es`), а значення — відповідні перекладені рядки.
|
|
57
|
+
|
|
58
|
+
### Повертає
|
|
59
|
+
|
|
60
|
+
- Рядок, що представляє перекладений вміст для поточної локалі.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Приклади використання
|
|
65
|
+
|
|
66
|
+
### Використання `t` у клієнтському компоненті
|
|
67
|
+
|
|
68
|
+
Переконайтесь, що у верхній частині файлу компонента зазначено директиву `'use client';` при використанні `t` у клієнтському компоненті.
|
|
69
|
+
|
|
70
|
+
```tsx codeFormat="typescript"
|
|
71
|
+
"use client";
|
|
72
|
+
|
|
73
|
+
import type { FC } from "react";
|
|
74
|
+
import { t } from "next-intlayer";
|
|
75
|
+
|
|
76
|
+
export const ClientComponentExample: FC = () => (
|
|
77
|
+
<p>
|
|
78
|
+
{t({
|
|
79
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
80
|
+
en: "This is the content of a client component example",
|
|
81
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
82
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
83
|
+
es: "Este es el contenido d un ejemplo de componente cliente",
|
|
84
|
+
})}
|
|
85
|
+
</p>
|
|
86
|
+
);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```javascript codeFormat="esm"
|
|
90
|
+
import { t } from "next-intlayer";
|
|
91
|
+
|
|
92
|
+
const ClientComponentExample = () => (
|
|
93
|
+
<p>
|
|
94
|
+
{t({
|
|
95
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
96
|
+
en: "This is the content of a client component example",
|
|
97
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
98
|
+
es: "Este es el contenido d un ejemplo de componente cliente",
|
|
99
|
+
})}
|
|
100
|
+
</p>
|
|
101
|
+
);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```javascript codeFormat="commonjs"
|
|
105
|
+
const { t } = require("next-intlayer");
|
|
106
|
+
|
|
107
|
+
const ClientComponentExample = () => (
|
|
108
|
+
<p>
|
|
109
|
+
{t({
|
|
110
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
111
|
+
en: "This is the content of a client component example",
|
|
112
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
113
|
+
es: "Este es le contenido d un ejemplo de componente cliente",
|
|
114
|
+
})}
|
|
115
|
+
</p>
|
|
116
|
+
);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Використання `t` у серверному компоненті
|
|
120
|
+
|
|
121
|
+
```tsx codeFormat="typescript"
|
|
122
|
+
import type { FC } from "react";
|
|
123
|
+
import { t } from "next-intlayer/server";
|
|
124
|
+
|
|
125
|
+
export const ServerComponentExample: FC = () => (
|
|
126
|
+
<p>
|
|
127
|
+
{t({
|
|
128
|
+
uk: "Це вміст прикладу серверного компонента",
|
|
129
|
+
en: "This is the content of a server component example",
|
|
130
|
+
fr: "Ceci est le contenu d'un exemple de composant serveur",
|
|
131
|
+
es: "Este es el contenido de un ejemplo de componente servidor",
|
|
132
|
+
})}
|
|
133
|
+
</p>
|
|
134
|
+
);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```javascript codeFormat="esm"
|
|
138
|
+
import { t } from "next-intlayer/server";
|
|
139
|
+
|
|
140
|
+
const ServerComponentExample = () => (
|
|
141
|
+
<p>
|
|
142
|
+
{t({
|
|
143
|
+
uk: "Це вміст прикладу серверного компонента",
|
|
144
|
+
en: "This is the content of a server component example",
|
|
145
|
+
fr: "Ceci est le contenu d'un exemple de composant serveur",
|
|
146
|
+
es: "Este es el contenido de un ejemplo de componente servidor",
|
|
147
|
+
})}
|
|
148
|
+
</p>
|
|
149
|
+
);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
```javascript codeFormat="commonjs"
|
|
153
|
+
const { t } = require("next-intlayer/server");
|
|
154
|
+
|
|
155
|
+
const ServerComponentExample = () => (
|
|
156
|
+
<p>
|
|
157
|
+
{t({
|
|
158
|
+
uk: "Це вміст прикладу серверного компонента",
|
|
159
|
+
en: "This is the content of a server component example",
|
|
160
|
+
fr: "Ceci est le contenu d'un exemple de composant serveur",
|
|
161
|
+
es: "Este es el contenido de un ejemplo de componente servidor",
|
|
162
|
+
})}
|
|
163
|
+
</p>
|
|
164
|
+
);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Вбудовані переклади в атрибутах
|
|
168
|
+
|
|
169
|
+
Функція `t` особливо корисна для вбудованих перекладів у JSX-атрибутах.
|
|
170
|
+
При локалізації атрибутів, таких як `alt`, `title`, `href` або `aria-label`, ви можете використовувати `t` безпосередньо в атрибуті.
|
|
171
|
+
|
|
172
|
+
```jsx
|
|
173
|
+
<button
|
|
174
|
+
aria-label={t({
|
|
175
|
+
uk: "Відправити",
|
|
176
|
+
en: "Submit",
|
|
177
|
+
fr: "Soumettre",
|
|
178
|
+
es: "Enviar",
|
|
179
|
+
})}
|
|
180
|
+
>
|
|
181
|
+
{t({
|
|
182
|
+
uk: "Надіслати",
|
|
183
|
+
en: "Submit",
|
|
184
|
+
fr: "Soumettre",
|
|
185
|
+
es: "Enviar",
|
|
186
|
+
})}
|
|
187
|
+
<img
|
|
188
|
+
src="/path/to/image"
|
|
189
|
+
alt={t({
|
|
190
|
+
uk: "Прекрасний пейзаж",
|
|
191
|
+
en: "A beautiful scenery",
|
|
192
|
+
fr: "Un beau paysage",
|
|
193
|
+
es: "Un hermoso paisaje",
|
|
194
|
+
})}
|
|
195
|
+
/>
|
|
196
|
+
</button>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## Додаткові теми
|
|
202
|
+
|
|
203
|
+
### Інтеграція з TypeScript
|
|
204
|
+
|
|
205
|
+
Функція `t` є типобезпечною при використанні з TypeScript і гарантує, що всі необхідні локалі вказані.
|
|
206
|
+
|
|
207
|
+
```typescript codeFormat="typescript"
|
|
208
|
+
import type { IConfigLocales } from "intlayer";
|
|
209
|
+
import { t } from "next-intlayer";
|
|
210
|
+
|
|
211
|
+
const translations: IConfigLocales<string> = {
|
|
212
|
+
uk: "Ласкаво просимо",
|
|
213
|
+
en: "Welcome",
|
|
214
|
+
fr: "Bienvenue",
|
|
215
|
+
es: "Bienvenido",
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const greeting = t(translations);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
```javascript codeFormat="esm"
|
|
222
|
+
import type { IConfigLocales } from "intlayer";
|
|
223
|
+
import { t } from "next-intlayer";
|
|
224
|
+
|
|
225
|
+
/** @type {import('next-intlayer').IConfigLocales<string>} */
|
|
226
|
+
const translations = {
|
|
227
|
+
en: "Welcome",
|
|
228
|
+
fr: "Bienvenue",
|
|
229
|
+
es: "Bienvenido",
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const greeting = t(translations);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```javascript codeFormat="commonjs"
|
|
236
|
+
const { t } = require("next-intlayer");
|
|
237
|
+
|
|
238
|
+
/** @type {import('next-intlayer').IConfigLocales<string>} */
|
|
239
|
+
const translations = {
|
|
240
|
+
en: "Welcome",
|
|
241
|
+
fr: "Bienvenue",
|
|
242
|
+
es: "Bienvenido",
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const greeting = t(translations);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Виявлення локалі та контекст
|
|
249
|
+
|
|
250
|
+
У `next-intlayer` поточна локаль керується через контекстні провайдери: `IntlayerClientProvider` та `IntlayerServerProvider`. Переконайтеся, що ці провайдери обгортають ваші компоненти і що проп `locale` передається правильно.
|
|
251
|
+
|
|
252
|
+
#### Приклад:
|
|
253
|
+
|
|
254
|
+
```tsx codeFormat="typescript"
|
|
255
|
+
import type { FC } from "react";
|
|
256
|
+
import type { Locales } from "intlayer";
|
|
257
|
+
import { IntlayerClientProvider } from "next-intlayer";
|
|
258
|
+
import { IntlayerServerProvider } from "next-intlayer/server";
|
|
259
|
+
|
|
260
|
+
const Page: FC<{ locale: Locales }> = ({ locale }) => (
|
|
261
|
+
<IntlayerServerProvider locale={locale}>
|
|
262
|
+
<IntlayerClientProvider locale={locale}>
|
|
263
|
+
{/* Тут ваші компоненти */}
|
|
264
|
+
</IntlayerClientProvider>
|
|
265
|
+
</IntlayerServerProvider>
|
|
266
|
+
);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
```javascript codeFormat="esm"
|
|
270
|
+
import { IntlayerClientProvider } from "next-intlayer";
|
|
271
|
+
import { IntlayerServerProvider } from "next-intlayer/server";
|
|
272
|
+
|
|
273
|
+
const Page = ({ locale }) => (
|
|
274
|
+
<IntlayerServerProvider locale={locale}>
|
|
275
|
+
<IntlayerClientProvider locale={locale}>
|
|
276
|
+
{/* Тут ваші компоненти */}
|
|
277
|
+
</IntlayerClientProvider>
|
|
278
|
+
</IntlayerServerProvider>
|
|
279
|
+
);
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
```javascript codeFormat="commonjs"
|
|
283
|
+
const { IntlayerClientProvider } = require("next-intlayer");
|
|
284
|
+
const { IntlayerServerProvider } = require("next-intlayer/server");
|
|
285
|
+
|
|
286
|
+
const Page = ({ locale }) => (
|
|
287
|
+
<IntlayerServerProvider locale={locale}>
|
|
288
|
+
<IntlayerClientProvider locale={locale}>
|
|
289
|
+
{/* Ваші компоненти тут */}
|
|
290
|
+
</IntlayerClientProvider>
|
|
291
|
+
</IntlayerServerProvider>
|
|
292
|
+
);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Загальні помилки та усунення несправностей
|
|
298
|
+
|
|
299
|
+
### `t` повертає undefined або неправильний переклад
|
|
300
|
+
|
|
301
|
+
- **Причина**: Поточна локаль неправильно встановлена, або переклад для поточної локалі відсутній.
|
|
302
|
+
- **Рішення**:
|
|
303
|
+
- Переконайтеся, що `IntlayerClientProvider` або `IntlayerServerProvider` правильно налаштовані з відповідною `locale`.
|
|
304
|
+
- Переконайтеся, що ваш об'єкт translations містить усі необхідні локалі.
|
|
305
|
+
|
|
306
|
+
### Відсутні переклади в TypeScript
|
|
307
|
+
|
|
308
|
+
- **Причина**: Об'єкт translations не відповідає вимогам щодо локалей, що призводить до помилок TypeScript.
|
|
309
|
+
- **Рішення**: Використовуйте тип `IConfigLocales`, щоб забезпечити повноту ваших перекладів.
|
|
310
|
+
|
|
311
|
+
```typescript codeFormat="typescript"
|
|
312
|
+
const translations: IConfigLocales<string> = {
|
|
313
|
+
en: "Text",
|
|
314
|
+
fr: "Texte",
|
|
315
|
+
// Відсутній 'es' спричинить помилку TypeScript [!code error]
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const text = t(translations);
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
```javascript codeFormat="esm"
|
|
322
|
+
const translations = {
|
|
323
|
+
en: "Text",
|
|
324
|
+
fr: "Texte",
|
|
325
|
+
// Відсутній 'es' спричинить помилку TypeScript [!code error]
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const text = t(translations);
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
```javascript codeFormat="commonjs"
|
|
332
|
+
const { t } = require("next-intlayer");
|
|
333
|
+
|
|
334
|
+
/** @type {import('next-intlayer').IConfigLocales<string>} */
|
|
335
|
+
const translations = {
|
|
336
|
+
en: "Text",
|
|
337
|
+
fr: "Texte",
|
|
338
|
+
// es: 'Texto', // Відсутній 'es' спричинить помилку TypeScript [!code error]
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const text = t(translations);
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Поради щодо ефективного використання
|
|
347
|
+
|
|
348
|
+
1. **Використовуйте `t` для простих inline-перекладів**: Ідеально підходить для перекладу невеликих фрагментів тексту безпосередньо у ваших компонентах.
|
|
349
|
+
2. **Віддавайте перевагу `useIntlayer` для структурованого контенту**: Для складніших перекладів і повторного використання контенту визначайте його в declaration-файлах і використовуйте `useIntlayer`.
|
|
350
|
+
3. **Послідовне надання локалі**: Переконайтеся, що ваша локаль послідовно передається в усьому додатку через відповідні провайдери.
|
|
351
|
+
4. **Використовуйте TypeScript**: Використовуйте типи TypeScript, щоб виявляти відсутні переклади та забезпечувати типобезпеку.
|
|
352
|
+
|
|
353
|
+
---
|
|
354
|
+
|
|
355
|
+
## Висновок
|
|
356
|
+
|
|
357
|
+
Функція `t` у `next-intlayer` — потужний і зручний інструмент для керування вбудованими перекладами у ваших додатках Next.js. Ефективно інтегрувавши її, ви розширюєте можливості інтернаціоналізації вашого застосунку та забезпечуєте кращий досвід для користувачів по всьому світу.
|
|
358
|
+
|
|
359
|
+
Для детальнішого опису використання та просунутих можливостей див. документацію [next-intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/intlayer_visual_editor.md).
|
|
360
|
+
|
|
361
|
+
///
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
**Примітка**: Не забудьте правильно налаштувати ваші `IntlayerClientProvider` та `IntlayerServerProvider`, щоб поточна локаль коректно передавалася вашим компонентам. Це критично для того, щоб функція `t` повертала правильні переклади.
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-08-23
|
|
3
|
+
updatedAt: 2025-08-23
|
|
4
|
+
title: Документація хука useDictionary | next-intlayer
|
|
5
|
+
description: Дізнайтеся, як використовувати хук useDictionary з пакету next-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- useDictionary
|
|
8
|
+
- словник
|
|
9
|
+
- ключ
|
|
10
|
+
- Intlayer
|
|
11
|
+
- Інтернаціоналізація
|
|
12
|
+
- Документація
|
|
13
|
+
- Next.js
|
|
14
|
+
- JavaScript
|
|
15
|
+
- React
|
|
16
|
+
slugs:
|
|
17
|
+
- doc
|
|
18
|
+
- packages
|
|
19
|
+
- next-intlayer
|
|
20
|
+
- useDictionary
|
|
21
|
+
history:
|
|
22
|
+
- version: 5.5.10
|
|
23
|
+
date: 2025-06-29
|
|
24
|
+
changes: Init history
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
# Інтеграція з React: документація хука `useDictionary`
|
|
28
|
+
|
|
29
|
+
У цьому розділі наведено детальні вказівки щодо використання хука `useDictionary` у React-застосунках, що дозволяє ефективно працювати з локалізованим вмістом без візуального редактора.
|
|
30
|
+
|
|
31
|
+
## Імпорт хука `useDictionary` у React
|
|
32
|
+
|
|
33
|
+
Хук `useDictionary` можна інтегрувати в React-застосунки, імпортуючи його залежно від контексту:
|
|
34
|
+
|
|
35
|
+
- **Клієнтський компонент:**
|
|
36
|
+
|
|
37
|
+
```typescript codeFormat="typescript"
|
|
38
|
+
import { useDictionary } from "next-intlayer"; // Використовується в клієнтських React-компонентах
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```javascript codeFormat="esm"
|
|
42
|
+
import { useDictionary } from "next-intlayer"; // Використовується в клієнтських React-компонентах
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```javascript codeFormat="commonjs"
|
|
46
|
+
const { useDictionary } = require("next-intlayer"); // Використовується в клієнтських React-компонентах
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- **Серверний компонент:**
|
|
50
|
+
|
|
51
|
+
```typescript codeFormat="typescript"
|
|
52
|
+
import { useDictionary } from "next-intlayer/server"; // Використовується в серверних React-компонентах
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```javascript codeFormat="esm"
|
|
56
|
+
import { useDictionary } from "next-intlayer/server"; // Використовується в серверних React-компонентах
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```javascript codeFormat="commonjs"
|
|
60
|
+
const { useDictionary } = require("next-intlayer/server"); // Використовується в серверних React-компонентах
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Параметри
|
|
64
|
+
|
|
65
|
+
Хук приймає два параметри:
|
|
66
|
+
|
|
67
|
+
1. **`dictionary`**: Оголошений об'єкт словника, що містить локалізований вміст для певних ключів.
|
|
68
|
+
2. **`locale`** (необов'язковий): Бажана локаль. За замовчуванням використовується локаль поточного контексту, якщо не вказано.
|
|
69
|
+
|
|
70
|
+
## Словник
|
|
71
|
+
|
|
72
|
+
Усі об'єкти словників мають бути оголошені у структурованих файлах вмісту, щоб забезпечити типобезпеку та запобігти помилкам під час виконання. Інструкції зі встановлення можна знайти [тут](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/dictionary/content_file.md). Ось приклад декларації вмісту:
|
|
73
|
+
|
|
74
|
+
```typescript fileName="component.content.ts" codeFormat="typescript"
|
|
75
|
+
import { t, type Dictionary } from "intlayer";
|
|
76
|
+
|
|
77
|
+
const exampleContent = {
|
|
78
|
+
key: "component-example",
|
|
79
|
+
content: {
|
|
80
|
+
title: t({
|
|
81
|
+
uk: "Приклад клієнтського компонента",
|
|
82
|
+
en: "Client Component Example",
|
|
83
|
+
fr: "Exemple de composant client",
|
|
84
|
+
es: "Ejemplo de componente cliente",
|
|
85
|
+
}),
|
|
86
|
+
content: t({
|
|
87
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
88
|
+
en: "This is the content of a client component example",
|
|
89
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
90
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
91
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
94
|
+
} satisfies Dictionary;
|
|
95
|
+
|
|
96
|
+
export default exampleContent;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```javascript fileName="component.content.mjs" contentDeclarationFormat="esm"
|
|
100
|
+
import { t } from "intlayer";
|
|
101
|
+
|
|
102
|
+
/** @type {import('intlayer').Dictionary} */
|
|
103
|
+
const exampleContent = {
|
|
104
|
+
key: "component-example",
|
|
105
|
+
content: {
|
|
106
|
+
title: t({
|
|
107
|
+
uk: "Приклад клієнтського компонента",
|
|
108
|
+
en: "Client Component Example",
|
|
109
|
+
fr: "Exemple de composant client",
|
|
110
|
+
es: "Ejemplo de componente cliente",
|
|
111
|
+
}),
|
|
112
|
+
content: t({
|
|
113
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
114
|
+
en: "This is the content of a client component example",
|
|
115
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
116
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
117
|
+
}),
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export default exampleContent;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```javascript fileName="component.content.cjs" contentDeclarationFormat="commonjs"
|
|
125
|
+
const { t } = require("intlayer");
|
|
126
|
+
|
|
127
|
+
// Тип: словник (Dictionary) з пакунка intlayer
|
|
128
|
+
/** @type {import('intlayer').Dictionary} */
|
|
129
|
+
const exampleContent = {
|
|
130
|
+
key: "component-example",
|
|
131
|
+
content: {
|
|
132
|
+
title: t({
|
|
133
|
+
uk: "Приклад клієнтського компонента",
|
|
134
|
+
en: "Client Component Example",
|
|
135
|
+
fr: "Exemple de composant client",
|
|
136
|
+
es: "Ejemplo de componente cliente",
|
|
137
|
+
}),
|
|
138
|
+
content: t({
|
|
139
|
+
uk: "Це вміст прикладу клієнтського компонента",
|
|
140
|
+
en: "This is the content of a client component example",
|
|
141
|
+
fr: "Ceci est le contenu d'un exemple de composant client",
|
|
142
|
+
es: "Este es el contenido de un ejemplo de componente cliente",
|
|
143
|
+
}),
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
module.exports = exampleContent;
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Приклад використання в клієнтському React-компоненті
|
|
151
|
+
|
|
152
|
+
Нижче наведено приклад того, як використовувати хук `useDictionary` у React-компоненті:
|
|
153
|
+
|
|
154
|
+
```tsx fileName="ClientComponentExample.tsx" codeFormat="typescript"
|
|
155
|
+
"use client";
|
|
156
|
+
|
|
157
|
+
import type { FC } from "react";
|
|
158
|
+
import { useDictionary } from "next-intlayer";
|
|
159
|
+
import clientComponentExampleContent from "./component.content";
|
|
160
|
+
|
|
161
|
+
const ClientComponentExample: FC = () => {
|
|
162
|
+
const { title, content } = useDictionary(clientComponentExampleContent);
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div>
|
|
166
|
+
<h1>{title}</h1>
|
|
167
|
+
<p>{content}</p>
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
```javascript fileName="ClientComponentExample.mjs" codeFormat="esm"
|
|
174
|
+
"use client";
|
|
175
|
+
|
|
176
|
+
import type { FC } from "react";
|
|
177
|
+
import { useDictionary } from "next-intlayer";
|
|
178
|
+
import exampleContent from "./component.content";
|
|
179
|
+
|
|
180
|
+
const ClientComponentExample: FC = () => {
|
|
181
|
+
const { title, content } = useDictionary(exampleContent);
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<div>
|
|
185
|
+
<h1>{title}</h1>
|
|
186
|
+
<p>{content}</p>
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
```javascript fileName="ClientComponentExample.cjs" codeFormat="commonjs"
|
|
193
|
+
"use client";
|
|
194
|
+
|
|
195
|
+
const { useDictionary } = require("next-intlayer");
|
|
196
|
+
const exampleContent = require("./component.content");
|
|
197
|
+
|
|
198
|
+
const ClientComponentExample = () => {
|
|
199
|
+
const { title, content } = useDictionary(exampleContent);
|
|
200
|
+
|
|
201
|
+
return (
|
|
202
|
+
<div>
|
|
203
|
+
<h1>{title}</h1>
|
|
204
|
+
<p>{content}</p>
|
|
205
|
+
</div>
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Приклад використання в React Server Component
|
|
211
|
+
|
|
212
|
+
Якщо ви використовуєте хук `useDictionary` поза `IntlayerServerProvider`, локаль має бути явно передана як параметр під час рендерингу компонента:
|
|
213
|
+
|
|
214
|
+
```tsx fileName="ServerComponentExample.tsx" codeFormat="typescript"
|
|
215
|
+
import type { FC } from "react";
|
|
216
|
+
import { useDictionary } from "next-intlayer/server";
|
|
217
|
+
import clientComponentExampleContent from "./component.content";
|
|
218
|
+
|
|
219
|
+
const ServerComponentExample: FC = () => {
|
|
220
|
+
const { content } = useDictionary(clientComponentExampleContent);
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<div>
|
|
224
|
+
<h1>{content.title}</h1>
|
|
225
|
+
<p>{content.content}</p>
|
|
226
|
+
</div>
|
|
227
|
+
);
|
|
228
|
+
};
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
```javascript fileName="ServerComponentExample.mjs" codeFormat="esm"
|
|
232
|
+
import { useDictionary } from "next-intlayer/server";
|
|
233
|
+
import clientComponentExampleContent from "./component.content";
|
|
234
|
+
|
|
235
|
+
const ServerComponentExample = () => {
|
|
236
|
+
const { content } = useDictionary(clientComponentExampleContent);
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<div>
|
|
240
|
+
<h1>{content.title}</h1>
|
|
241
|
+
<p>{content.content}</p>
|
|
242
|
+
</div>
|
|
243
|
+
);
|
|
244
|
+
};
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
```javascript fileName="ServerComponentExample.cjs" codeFormat="commonjs"
|
|
248
|
+
import { useDictionary } from "next-intlayer/server";
|
|
249
|
+
import clientComponentExampleContent from "./component.content";
|
|
250
|
+
|
|
251
|
+
const ServerComponentExample = () => {
|
|
252
|
+
const { content } = useDictionary(clientComponentExampleContent);
|
|
253
|
+
|
|
254
|
+
return (
|
|
255
|
+
<div>
|
|
256
|
+
<h1>{content.title}</h1>
|
|
257
|
+
<p>{content.content}</p>
|
|
258
|
+
</div>
|
|
259
|
+
);
|
|
260
|
+
};
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Примітки щодо атрибутів
|
|
264
|
+
|
|
265
|
+
На відміну від інтеграцій, що використовують візуальні редактори, атрибути на кшталт `buttonTitle.value` тут не застосовуються. Натомість звертайтесь безпосередньо до локалізованих рядків, як вони оголошені у вашому контенті.
|
|
266
|
+
|
|
267
|
+
```jsx
|
|
268
|
+
<button title={content.title}>{content.content}</button>
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Додаткові поради
|
|
272
|
+
|
|
273
|
+
- **Type Safety**: Завжди використовуйте `Dictionary` для визначення ваших словників, щоб забезпечити безпеку типів.
|
|
274
|
+
- **Оновлення локалізацій**: При оновленні контенту переконайтеся, що всі locales узгоджені, щоб уникнути відсутніх перекладів.
|
|
275
|
+
|
|
276
|
+
Ця документація зосереджена на інтеграції хука `useDictionary`, що забезпечує оптимізований підхід до керування локалізованим контентом без покладання на функціональність візуального редактора.
|