Creation:2026-06-12Last update:2026-06-12
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "发布集合字典功能"v9.0.02026/6/12
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
集合
集合(Collection)是一组共享相同字典键(key)但声明不同项目索引(item)的内容文件。Intlayer 会在构建时将它们合并为一个有序列表。
声明集合项目
每个文件代表一个项目。item 字段是它在列表中的位置(从 1 开始)。
faq.1.content.ts
复制代码
复制代码到剪贴板
import { t, type Dictionary } from "intlayer";
const dictionary = {
key: "faq",
item: 1,
content: {
question: t({ en: "What is Intlayer?", fr: "Qu'est-ce qu'Intlayer ?" }),
answer: t({ en: "An i18n toolkit.", fr: "Une boîte à outils i18n." }),
},
} satisfies Dictionary;
export default dictionary;faq.2.content.ts
复制代码
复制代码到剪贴板
import { t, type Dictionary } from "intlayer";
const dictionary = {
key: "faq",
item: 2,
content: {
question: t({ en: "Is it free?", fr: "Est-ce gratuit ?" }),
answer: t({ en: "Yes, open-source.", fr: "Oui, open-source." }),
},
} satisfies Dictionary;
export default dictionary;使用集合
所有项目
FAQ.tsx
复制代码
复制代码到剪贴板
import { useIntlayer } from "react-intlayer";export const FAQ = () => { const items = useIntlayer("faq"); // { question: string; answer: string }[] return ( <ul> {items.map((item, index) => ( <li key={index}> <strong>{item.question}</strong> <p>{item.answer}</p> </li> ))} </ul> );};按索引获取单个项目
tsx
复制代码
复制代码到剪贴板
const faq2 = useIntlayer("faq", { item: 2 });// → { question: string; answer: string }使用显式语言环境获取单个项目
tsx
复制代码
复制代码到剪贴板
const faq2Zh = useIntlayer("faq", { item: 2, locale: "zh" });典型使用场景
- 常见问题 (FAQ) 列表
- 定价层级 / 资费计划
- 轮播图 / 幻灯片内容
- 分步说明步骤