作者:
    Creation:2026-06-12Last update:2026-06-12

    集合

    集合(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) 列表
    • 定价层级 / 资费计划
    • 轮播图 / 幻灯片内容
    • 分步说明步骤