Задайте питання та отримайте підсумок документа, вказавши цю сторінку та обраного вами постачальника штучного інтелекту
Історія версій
- "Ініціалізація історії"v5.5.1029.06.2025
Вміст цієї сторінки перекладено за допомогою штучного інтелекту.
Переглянути останню версію оригінального вмісту англійськоюЯкщо у вас є ідея щодо покращення цієї документації, будь ласка, долучіться, надіславши pull request на GitHub.
Посилання на документацію на GitHubСкопіювати документацію у форматі Markdown в буфер обміну
Вкладення / Посилання на підконтент
Як працює вкладення
У Intlayer вкладення реалізується за допомогою функції nest, яка дозволяє посилатися на контент з іншого словника та повторно його використовувати. Замість дублювання контенту ви можете вказати на існуючий модуль контенту за його ключем.
Налаштування вкладення
Щоб налаштувати вкладення у вашому проекті Intlayer, спочатку визначте базовий контент, який ви хочете повторно використовувати. Потім у окремому модулі контенту використовуйте функцію nest для імпорту цього контенту.
Базовий словник
Нижче наведено приклад базового словника для вкладення в інший словник:
Скопіюйте код у буфер обміну
import { type Dictionary } from "intlayer";
const firstDictionary = {
key: "key_of_my_first_dictionary",
content: {
content: "content",
subContent: {
contentNumber: 0,
contentString: "string",
},
},
} satisfies Dictionary;
export default firstDictionary;
Посилання з Nest
Тепер створіть ще один модуль контенту, який використовує функцію nest для посилання на вищезазначений контент. Ви можете посилатися на весь контент або на конкретне вкладене значення:
Скопіюйте код у буфер обміну
import { nest, type Dictionary } from "intlayer";
const myNestingContent = {
key: "key_of_my_second_dictionary",
content: {
// Посилається на весь словник:
fullNestedContent: nest("key_of_my_first_dictionary"),
// Посилається на конкретне вкладене значення:
partialNestedContent: nest(
"key_of_my_first_dictionary",
"subContent.contentNumber"
),
},
} satisfies Dictionary;
export default myNestingContent;
Як другий параметр, ви можете вказати шлях до вкладеного значення в цьому контенті. Якщо шлях не вказаний, повертається весь контент посилаючого словника.
Налаштування вкладення
To use nested content in a React component, leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified key. Here's an example of how to use it:
Скопіюйте код у буфер обміну
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const NestComponent: FC = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
<p>Partial Nested Value: {partialNestedContent}</p>
</div>
);
};
export default NestComponent;
To use nested content in Next.js Client Components, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
"use client";
import type { FC } from "react";
import { useIntlayer } from "next-intlayer";
const NestComponent: FC = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
<p>Partial Nested Value: {partialNestedContent}</p>
</div>
);
};
export default NestComponent;
To use nested content in Vue components, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
To use nested content in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. Here's an example:
Скопіюйте код у буфер обміну
To use nested content in Preact components, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
import type { FC } from "preact";
import { useIntlayer } from "preact-intlayer";
const NestComponent: FC = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
<p>Partial Nested Value: {partialNestedContent}</p>
</div>
);
};
export default NestComponent;
To use nested content in SolidJS components, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
import type { Component } from "solid-js";
import { useIntlayer } from "solid-intlayer";
const NestComponent: Component = () => {
const { fullNestedContent, partialNestedContent } = useIntlayer(
"key_of_my_second_dictionary"
);
return (
<div>
<p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
<p>Partial Nested Value: {partialNestedContent}</p>
</div>
);
};
export default NestComponent;
To use nested content in Angular components, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
To use nested content with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:
Скопіюйте код у буфер обміну
import { installIntlayer, useIntlayer } from "vanilla-intlayer";
installIntlayer();
const content = useIntlayer("key_of_my_second_dictionary").onChange(
(newContent) => {
document.getElementById("nested")!.textContent =
newContent.partialNestedContent;
}
);
// Initial render
document.getElementById("nested")!.textContent = content.partialNestedContent;
Додаткові ресурси
Для детальнішої інформації щодо конфігурації та використання зверніться до наступних ресурсів:
Ці ресурси надають додаткові відомості щодо налаштування та використання Intlayer в різних середовищах та з різними фреймворками.
