@intlayer/docs 7.5.12 → 7.5.14
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/per-component_vs_centralized_i18n.md +248 -0
- package/blog/de/per-component_vs_centralized_i18n.md +248 -0
- package/blog/en/_per-component_vs_centralized_i18n.md +252 -0
- package/blog/en/per-component_vs_centralized_i18n.md +248 -0
- package/blog/en-GB/per-component_vs_centralized_i18n.md +247 -0
- package/blog/es/per-component_vs_centralized_i18n.md +245 -0
- package/blog/fr/per-component_vs_centralized_i18n.md +245 -0
- package/blog/hi/per-component_vs_centralized_i18n.md +249 -0
- package/blog/id/per-component_vs_centralized_i18n.md +248 -0
- package/blog/it/per-component_vs_centralized_i18n.md +247 -0
- package/blog/ja/per-component_vs_centralized_i18n.md +247 -0
- package/blog/ko/per-component_vs_centralized_i18n.md +246 -0
- package/blog/pl/per-component_vs_centralized_i18n.md +247 -0
- package/blog/pt/per-component_vs_centralized_i18n.md +246 -0
- package/blog/ru/per-component_vs_centralized_i18n.md +251 -0
- package/blog/tr/per-component_vs_centralized_i18n.md +244 -0
- 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/per-component_vs_centralized_i18n.md +248 -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/blog/vi/per-component_vs_centralized_i18n.md +246 -0
- package/blog/zh/per-component_vs_centralized_i18n.md +248 -0
- package/dist/cjs/common.cjs.map +1 -1
- package/dist/cjs/generated/blog.entry.cjs +20 -0
- package/dist/cjs/generated/blog.entry.cjs.map +1 -1
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/cjs/generated/frequentQuestions.entry.cjs +20 -0
- package/dist/cjs/generated/frequentQuestions.entry.cjs.map +1 -1
- package/dist/cjs/generated/legal.entry.cjs.map +1 -1
- package/dist/esm/common.mjs.map +1 -1
- package/dist/esm/generated/blog.entry.mjs +20 -0
- package/dist/esm/generated/blog.entry.mjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/esm/generated/frequentQuestions.entry.mjs +20 -0
- package/dist/esm/generated/frequentQuestions.entry.mjs.map +1 -1
- package/dist/esm/generated/legal.entry.mjs.map +1 -1
- package/dist/types/generated/blog.entry.d.ts +1 -0
- package/dist/types/generated/blog.entry.d.ts.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 +9 -9
- package/src/generated/blog.entry.ts +20 -0
- package/src/generated/frequentQuestions.entry.ts +20 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-08-23
|
|
3
|
+
updatedAt: 2025-08-23
|
|
4
|
+
title: Інструкція по використанню хуку useIntlayer | next-intlayer
|
|
5
|
+
description: Дізнайтеся, як використовувати хук useIntlayer для пакета next-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- useIntlayer
|
|
8
|
+
- словник
|
|
9
|
+
- ключ
|
|
10
|
+
- Intlayer
|
|
11
|
+
- Internationalization
|
|
12
|
+
- Документація
|
|
13
|
+
- Next.js
|
|
14
|
+
- JavaScript
|
|
15
|
+
- React
|
|
16
|
+
slugs:
|
|
17
|
+
- doc
|
|
18
|
+
- packages
|
|
19
|
+
- next-intlayer
|
|
20
|
+
- useIntlayer
|
|
21
|
+
history:
|
|
22
|
+
- version: 5.5.10
|
|
23
|
+
date: 2025-06-29
|
|
24
|
+
changes: Ініціалізація історії
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
# Інтеграція з Next.js: документація хуку `useIntlayer`
|
|
28
|
+
|
|
29
|
+
Хук `useIntlayer` призначений для застосунків Next.js для ефективного отримання та керування локалізованим контентом. Ця документація зосереджена на тому, як використовувати хук у проектах Next.js, забезпечуючи належні практики локалізації.
|
|
30
|
+
|
|
31
|
+
## Імпорт `useIntlayer` в Next.js
|
|
32
|
+
|
|
33
|
+
Залежно від того, чи працюєте ви з компонентами на боці клієнта чи на боці сервера в додатку Next.js, хук `useIntlayer` можна імпортувати таким чином:
|
|
34
|
+
|
|
35
|
+
- **Клієнтський компонент:**
|
|
36
|
+
|
|
37
|
+
```typescript codeFormat="typescript"
|
|
38
|
+
import { useIntlayer } from "next-intlayer"; // Використовується в компонентах на боці клієнта
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```javascript codeFormat="esm"
|
|
42
|
+
import { useIntlayer } from "next-intlayer"; // Використовується в компонентах на боці клієнта
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```javascript codeFormat="commonjs"
|
|
46
|
+
const { useIntlayer } = require("next-intlayer"); // Використовується в компонентах на боці клієнта
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- **Серверний компонент:**
|
|
50
|
+
|
|
51
|
+
```tsx codeFormat="typescript"
|
|
52
|
+
import { useIntlayer } from "next-intlayer/server"; // Використовується в компонентах на боці сервера
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```javascript codeFormat="esm"
|
|
56
|
+
import { useIntlayer } from "next-intlayer/server"; // Використовується у серверних компонентах
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```javascript codeFormat="commonjs"
|
|
60
|
+
const { useIntlayer } = require("next-intlayer/server"); // Використовується у серверних компонентах
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Параметри
|
|
64
|
+
|
|
65
|
+
1. **`key`**: Рядковий ідентифікатор ключа словника, з якого потрібно отримати вміст.
|
|
66
|
+
2. **`locale`** (необов'язковий): Конкретна локаль для використання. Якщо пропущено, хук за замовчуванням використовує локаль, встановлену в клієнтському або серверному контексті.
|
|
67
|
+
|
|
68
|
+
## Файли словника
|
|
69
|
+
|
|
70
|
+
Критично, щоб всі ключі вмісту були визначені у файлах декларації контенту, щоб уникнути помилок під час виконання та забезпечити безпеку типів. Такий підхід також полегшує інтеграцію з TypeScript для валідації на етапі компіляції.
|
|
71
|
+
|
|
72
|
+
Інструкції щодо налаштування файлів декларації контенту доступні [тут](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/dictionary/content_file.md).
|
|
73
|
+
|
|
74
|
+
## Приклад використання в Next.js
|
|
75
|
+
|
|
76
|
+
Ось як ви можете реалізувати хук `useIntlayer` на сторінці Next.js, щоб динамічно завантажувати локалізований контент залежно від поточної локалі застосунку:
|
|
77
|
+
|
|
78
|
+
```tsx fileName="src/pages/[locale]/index.tsx" codeFormat="typescript"
|
|
79
|
+
import { ClientComponentExample } from "@components/ClientComponentExample";
|
|
80
|
+
import { ServerComponentExample } from "@components/ServerComponentExample";
|
|
81
|
+
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
|
|
82
|
+
import { useIntlayer, IntlayerServerProvider } from "next-intlayer/server";
|
|
83
|
+
|
|
84
|
+
const HomePage: NextPageIntlayer = async ({ params }) => {
|
|
85
|
+
const { locale } = await params;
|
|
86
|
+
|
|
87
|
+
const content = useIntlayer("homepage", locale);
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<>
|
|
91
|
+
<p>{content.introduction}</p>
|
|
92
|
+
<IntlayerClientProvider locale={locale}>
|
|
93
|
+
<ClientComponentExample />
|
|
94
|
+
</IntlayerClientProvider>
|
|
95
|
+
<IntlayerServerProvider locale={locale}>
|
|
96
|
+
<ServerComponentExample />
|
|
97
|
+
</IntlayerServerProvider>
|
|
98
|
+
</>
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```jsx fileName="src/pages/[locale]/index.csx" codeFormat="esm"
|
|
104
|
+
import { ClientComponentExample } from "@components/ClientComponentExample";
|
|
105
|
+
import { ServerComponentExample } from "@components/ServerComponentExample";
|
|
106
|
+
import { IntlayerClientProvider } from "next-intlayer";
|
|
107
|
+
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
|
|
108
|
+
|
|
109
|
+
const HomePage = ({ locale }) => {
|
|
110
|
+
const content = useIntlayer("homepage", locale);
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<>
|
|
114
|
+
<p>{content.introduction}</p>
|
|
115
|
+
<IntlayerClientProvider locale={locale}>
|
|
116
|
+
<ClientComponentExample />
|
|
117
|
+
</IntlayerClientProvider>
|
|
118
|
+
<IntlayerServerProvider locale={locale}>
|
|
119
|
+
<ServerComponentExample />
|
|
120
|
+
</IntlayerServerProvider>
|
|
121
|
+
</>
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```jsx fileName="src/components/ClientComponentExample.csx" codeFormat="commonjs"
|
|
127
|
+
const {
|
|
128
|
+
ClientComponentExample,
|
|
129
|
+
} = require("@components/ClientComponentExample");
|
|
130
|
+
const {
|
|
131
|
+
ServerComponentExample,
|
|
132
|
+
} = require("@components/ServerComponentExample");
|
|
133
|
+
const { IntlayerClientProvider } = require("next-intlayer");
|
|
134
|
+
const { useIntlayer } = require("next-intlayer/server");
|
|
135
|
+
|
|
136
|
+
const HomePage = ({ locale }) => {
|
|
137
|
+
const content = useIntlayer("homepage", locale);
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<>
|
|
141
|
+
<p>{content.introduction}</p>
|
|
142
|
+
<IntlayerClientProvider locale={locale}>
|
|
143
|
+
<ClientComponentExample />
|
|
144
|
+
</IntlayerClientProvider>
|
|
145
|
+
<IntlayerServerProvider locale={locale}>
|
|
146
|
+
<ServerComponentExample />
|
|
147
|
+
</IntlayerServerProvider>
|
|
148
|
+
</>
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
```tsx fileName="src/components/ClientComponentExample.tsx" codeFormat="typescript"
|
|
154
|
+
"use-client";
|
|
155
|
+
|
|
156
|
+
import type { FC } from "react";
|
|
157
|
+
import { useIntlayer } from "next-intlayer";
|
|
158
|
+
|
|
159
|
+
const ClientComponentExample: FC = () => {
|
|
160
|
+
const content = useIntlayer("component-content");
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<div>
|
|
164
|
+
<h1>{content.title}</h1>
|
|
165
|
+
<p>{content.description}</p>
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```jsx fileName="src/components/ClientComponentExample.msx" codeFormat="esm"
|
|
172
|
+
"use-client";
|
|
173
|
+
|
|
174
|
+
import { useIntlayer } from "next-intlayer";
|
|
175
|
+
|
|
176
|
+
const ServerComponentExample = () => {
|
|
177
|
+
const content = useIntlayer("component-content");
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<div>
|
|
181
|
+
<h1>{content.title}</h1>
|
|
182
|
+
<p>{content.description}</p>
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
};
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```jsx fileName="src/components/ClientComponentExample.csx" codeFormat="commonjs"
|
|
189
|
+
"use-client";
|
|
190
|
+
|
|
191
|
+
const { useIntlayer } = require("next-intlayer");
|
|
192
|
+
|
|
193
|
+
const ServerComponentExample = () => {
|
|
194
|
+
const content = useIntlayer("component-content");
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<div>
|
|
198
|
+
<h1>{content.title}</h1>
|
|
199
|
+
<p>{content.description}</p>
|
|
200
|
+
</div>
|
|
201
|
+
);
|
|
202
|
+
};
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
```tsx fileName="src/components/ServerComponentExample.tsx" codeFormat="typescript"
|
|
206
|
+
import type { FC } from "react";
|
|
207
|
+
import { useIntlayer } from "next-intlayer/server";
|
|
208
|
+
|
|
209
|
+
const ServerComponentExample: FC = () => {
|
|
210
|
+
const content = useIntlayer("component-content");
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<div>
|
|
214
|
+
<h1>{content.title}</h1>
|
|
215
|
+
<p>{content.description}</p>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
```jsx fileName="src/components/ServerComponentExample.mjx" codeFormat="esm"
|
|
222
|
+
import { useIntlayer } from "next-intlayer/server";
|
|
223
|
+
|
|
224
|
+
const ServerComponentExample = () => {
|
|
225
|
+
const content = useIntlayer("component-content");
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<div>
|
|
229
|
+
<h1>{content.title}</h1>
|
|
230
|
+
<p>{content.description}</p>
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
};
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```jsx fileName="src/components/ServerComponentExample.csx" codeFormat="commonjs"
|
|
237
|
+
const { useIntlayer } = require("next-intlayer/server");
|
|
238
|
+
|
|
239
|
+
const ServerComponentExample = () => {
|
|
240
|
+
const content = useIntlayer("component-content");
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<div>
|
|
244
|
+
<h1>{content.title}</h1>
|
|
245
|
+
<p>{content.description}</p>
|
|
246
|
+
</div>
|
|
247
|
+
);
|
|
248
|
+
};
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Локалізація атрибутів
|
|
252
|
+
|
|
253
|
+
Щоб локалізувати атрибути, такі як `alt`, `title`, `href`, `aria-label` тощо, переконайтеся, що ви правильно посилаєтеся на вміст:
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
<img src={content.image.src.value} alt={content.image.alt.value} />
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Додаткова інформація
|
|
260
|
+
|
|
261
|
+
- **Intlayer Visual Editor**: Дізнайтеся, як використовувати візуальний редактор для зручнішого керування контентом [тут](https://github.com/aymericzip/intlayer/blob/main/docs/docs/uk/intlayer_visual_editor.md).
|
|
262
|
+
|
|
263
|
+
Ця документація описує використання хуку `useIntlayer` саме в середовищах Next.js, пропонуючи надійне рішення для керування локалізацією у ваших застосунках Next.js.
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-08-23
|
|
3
|
+
updatedAt: 2025-10-09
|
|
4
|
+
title: Документація хуку useLocale | next-intlayer
|
|
5
|
+
description: Дивіться, як використовувати хук useLocale у пакеті next-intlayer
|
|
6
|
+
keywords:
|
|
7
|
+
- useLocale
|
|
8
|
+
- dictionary
|
|
9
|
+
- key
|
|
10
|
+
- Intlayer
|
|
11
|
+
- Internationalization
|
|
12
|
+
- Documentation
|
|
13
|
+
- Next.js
|
|
14
|
+
- JavaScript
|
|
15
|
+
- React
|
|
16
|
+
slugs:
|
|
17
|
+
- doc
|
|
18
|
+
- packages
|
|
19
|
+
- next-intlayer
|
|
20
|
+
- useLocale
|
|
21
|
+
history:
|
|
22
|
+
- version: 6.2.0
|
|
23
|
+
date: 2025-10-09
|
|
24
|
+
changes: Додано документацію для хуку `useLocale` з опцією `onLocaleChange`
|
|
25
|
+
- version: 5.5.10
|
|
26
|
+
date: 2025-06-29
|
|
27
|
+
changes: Ініціалізація історії
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
# Інтеграція з Next.js: документація хуку `useLocale` для `next-intlayer`
|
|
31
|
+
|
|
32
|
+
This section offers detailed documentation on the `useLocale` hook tailored for Next.js applications within the `next-intlayer` library. It is designed to handle locale changes and routing efficiently.
|
|
33
|
+
|
|
34
|
+
## Імпорт `useLocale` у Next.js
|
|
35
|
+
|
|
36
|
+
Щоб використовувати хук `useLocale` у вашому Next.js застосунку, імпортуйте його, як показано нижче:
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
import { useLocale } from "next-intlayer"; // Використовується для керування локалями та маршрутизацією в Next.js
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Використання
|
|
43
|
+
|
|
44
|
+
Ось як реалізувати хук `useLocale` у компоненті Next.js:
|
|
45
|
+
|
|
46
|
+
```tsx fileName="src/components/LocaleSwitcher.tsx" codeFormat="typescript"
|
|
47
|
+
"use client";
|
|
48
|
+
|
|
49
|
+
import type { FC } from "react";
|
|
50
|
+
import { Locales } from "intlayer";
|
|
51
|
+
import { useLocale } from "next-intlayer";
|
|
52
|
+
|
|
53
|
+
const LocaleSwitcher: FC = () => {
|
|
54
|
+
const { locale, defaultLocale, availableLocales, setLocale } = useLocale();
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div>
|
|
58
|
+
<h1>Поточна локаль: {locale}</h1>
|
|
59
|
+
<p>Локаль за замовчуванням: {defaultLocale}</p>
|
|
60
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
61
|
+
{availableLocales.map((loc) => (
|
|
62
|
+
<option key={loc} value={loc}>
|
|
63
|
+
{loc}
|
|
64
|
+
</option>
|
|
65
|
+
))}
|
|
66
|
+
</select>
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```jsx fileName="src/components/LocaleSwitcher.mjx" codeFormat="esm"
|
|
73
|
+
"use client";
|
|
74
|
+
|
|
75
|
+
import { Locales } from "intlayer";
|
|
76
|
+
import { useLocale } from "next-intlayer";
|
|
77
|
+
|
|
78
|
+
const LocaleSwitcher = () => {
|
|
79
|
+
const { locale, defaultLocale, availableLocales, setLocale } = useLocale();
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<div>
|
|
83
|
+
<h1>Поточна локаль: {locale}</h1>
|
|
84
|
+
<p>Мова за замовчуванням: {defaultLocale}</p>
|
|
85
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
86
|
+
{availableLocales.map((loc) => (
|
|
87
|
+
<option key={loc} value={loc}>
|
|
88
|
+
{loc}
|
|
89
|
+
</option>
|
|
90
|
+
))}
|
|
91
|
+
</select>
|
|
92
|
+
</div>
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```jsx fileName="src/components/LocaleSwitcher.csx" codeFormat="commonjs"
|
|
98
|
+
"use client";
|
|
99
|
+
|
|
100
|
+
const { Locales } = require("intlayer");
|
|
101
|
+
const { useLocale } = require("next-intlayer");
|
|
102
|
+
|
|
103
|
+
const LocaleSwitcher = () => {
|
|
104
|
+
const { locale, defaultLocale, availableLocales, setLocale } = useLocale();
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<div>
|
|
108
|
+
<h1>Поточна локаль: {locale}</h1>
|
|
109
|
+
<p>Мова за замовчуванням: {defaultLocale}</p>
|
|
110
|
+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
|
|
111
|
+
{availableLocales.map((loc) => (
|
|
112
|
+
<option key={loc} value={loc}>
|
|
113
|
+
{loc}
|
|
114
|
+
</option>
|
|
115
|
+
))}
|
|
116
|
+
</select>
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Параметри
|
|
123
|
+
|
|
124
|
+
Хук `useLocale` приймає такі параметри:
|
|
125
|
+
|
|
126
|
+
- **`onLocaleChange`**: Рядок, який визначає, як має оновлюватися URL під час зміни локалі. Може бути `"replace"`, `"push"` або `"none"`.
|
|
127
|
+
|
|
128
|
+
> Розглянемо приклад:
|
|
129
|
+
>
|
|
130
|
+
> 1. Ви перебуваєте на `/fr/home`
|
|
131
|
+
> 2. Ви переходите на `/fr/about`
|
|
132
|
+
> 3. Ви переключаєтесь на `/es/about`
|
|
133
|
+
> 4. Ви натискаєте кнопку "назад" у браузері
|
|
134
|
+
>
|
|
135
|
+
> Поведінка відрізнятиметься залежно від значення `onLocaleChange`:
|
|
136
|
+
>
|
|
137
|
+
> - `undefined`: (за замовчуванням) Оновлює лише локаль у клієнтському контексті та встановлює cookie, не змінюючи URL.
|
|
138
|
+
> -> Кнопка «назад» перейде на `/fr/home`
|
|
139
|
+
> - `"replace"`: Замінює поточний URL на новий локалізований URL і встановлює cookie.
|
|
140
|
+
> -> Кнопка «назад» перейде на `/es/home`
|
|
141
|
+
> - `"push"`: Додає новий локалізований URL в історію браузера та встановлює cookie.
|
|
142
|
+
> -> Кнопка «назад» перейде на `/fr/about`
|
|
143
|
+
> - `(locale) => void`: Встановлює cookie і викликає кастомну функцію, яка буде запущена при зміні локалі.
|
|
144
|
+
>
|
|
145
|
+
> Опція `undefined` — поведінка за замовчуванням: ми рекомендуємо використовувати компонент `Link` для навігації на нову локаль.
|
|
146
|
+
> Приклад:
|
|
147
|
+
>
|
|
148
|
+
> ```tsx
|
|
149
|
+
> <Link href="/es/about" replace>
|
|
150
|
+
> Про нас
|
|
151
|
+
> </Link>
|
|
152
|
+
> ```
|
|
153
|
+
|
|
154
|
+
## Повернуті значення
|
|
155
|
+
|
|
156
|
+
Коли ви викликаєте хук `useLocale`, він повертає об'єкт, що містить наступні властивості:
|
|
157
|
+
|
|
158
|
+
- **`locale`**: Поточна локаль, як встановлено в контексті React.
|
|
159
|
+
- **`defaultLocale`**: Основна локаль, визначена в конфігурації.
|
|
160
|
+
- **`availableLocales`**: Список усіх доступних локалей, визначених у конфігурації.
|
|
161
|
+
- **`setLocale`**: Функція для зміни локалі застосунку та відповідного оновлення URL. Вона враховує правила префіксів, чи додавати локаль у шлях залежно від конфігурації. Використовує `useRouter` з `next/navigation` для навігаційних функцій, таких як `push` і `refresh`.
|
|
162
|
+
- **`pathWithoutLocale`**: обчислювана властивість, яка повертає шлях без локалі. Корисна для порівняння URL-адрес. Наприклад, якщо поточна локаль — `fr`, а URL — `fr/my_path`, то шлях без локалі буде `/my_path`. Використовує `usePathname` з `next/navigation` для отримання поточного шляху.
|
|
163
|
+
|
|
164
|
+
## Висновок
|
|
165
|
+
|
|
166
|
+
Хук `useLocale` з `next-intlayer` — це ключовий інструмент для керування локалями в додатках Next.js. Він пропонує інтегрований підхід для адаптації вашого застосунку до кількох локалей, безшовно обробляючи збереження локалі, управління станом та модифікації URL.
|