Ajukan pertanyaan Anda dan dapatkan ringkasan dokumen dengan merujuk halaman ini dan penyedia AI pilihan Anda
Riwayat Versi
- "Inisialisasi riwayat"v5.5.1029/6/2025
Konten halaman ini diterjemahkan menggunakan AI.
Lihat versi terakhir dari konten aslinya dalam bahasa InggrisJika Anda memiliki ide untuk meningkatkan dokumentasi ini, silakan berkontribusi dengan mengajukan pull request di GitHub.
Tautan GitHub ke dokumentasiSalin Markdown dokumentasi ke clipboard
Penanaman / Referensi Sub Konten
Cara Kerja Penanaman
Di Intlayer, penanaman dicapai melalui fungsi nest, yang memungkinkan Anda merujuk dan menggunakan kembali konten dari kamus lain. Alih-alih menggandakan konten, Anda dapat menunjuk ke modul konten yang sudah ada berdasarkan kuncinya.
Menyiapkan Nesting
Untuk menyiapkan nesting di proyek Intlayer Anda, Anda terlebih dahulu menentukan konten dasar yang ingin Anda gunakan kembali. Kemudian, di modul konten terpisah, Anda menggunakan fungsi nest untuk mengimpor konten tersebut.
Kamus Dasar
Di bawah ini adalah contoh kamus dasar untuk bersarang dalam kamus lain:
Salin kode ke clipboard
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;
Referencing with Nest
Sekarang, buat modul konten lain yang menggunakan fungsi nest untuk mereferensikan konten di atas. Anda dapat mereferensikan seluruh konten atau nilai nested tertentu:
Salin kode ke clipboard
import { nest, type Dictionary } from "intlayer";
const myNestingContent = {
key: "key_of_my_second_dictionary",
content: {
// Mereferensikan seluruh kamus:
fullNestedContent: nest("key_of_my_first_dictionary"),
// Mereferensikan nilai nested tertentu:
partialNestedContent: nest(
"key_of_my_first_dictionary",
"subContent.contentNumber"
),
},
} satisfies Dictionary;
export default myNestingContent;
Sebagai parameter kedua, Anda dapat menentukan path ke nilai nested dalam konten tersebut. Ketika tidak ada path yang disediakan, seluruh konten dari kamus yang direferensikan akan dikembalikan.
Menggunakan Nesting dengan React Intlayer
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:
Salin kode ke clipboard
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:
Salin kode ke clipboard
"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:
Salin kode ke clipboard
To use nested content in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. Here's an example:
Salin kode ke clipboard
To use nested content in Preact components, retrieve it via the useIntlayer hook. Here's an example:
Salin kode ke clipboard
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:
Salin kode ke clipboard
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:
Salin kode ke clipboard
To use nested content with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:
Salin kode ke clipboard
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;
Sumber Daya Tambahan
Untuk informasi lebih rinci tentang konfigurasi dan penggunaan, lihat sumber daya berikut:
Sumber daya ini memberikan wawasan lebih lanjut tentang pengaturan dan penggunaan Intlayer di berbagai lingkungan dan dengan berbagai framework.
