--- createdAt: 2026-06-12 updatedAt: 2026-06-12 title: 集合 description: 在 Intlayer 内容文件中使用 item 元数据字段来构建本地化项目的有序集合,这些项目在运行时可以通过索引进行选择。 keywords: - 集合 - 内容列表 - 动态内容 - Intlayer - 国际化 slugs: - doc - concept - collections history: - version: 9.0.0 date: 2026-06-12 changes: "发布集合字典功能" author: aymericzip --- # 集合 **集合**(Collection)是一组共享相同字典键(`key`)但声明不同项目索引(`item`)的内容文件。Intlayer 会在构建时将它们合并为一个有序列表。 ## 声明集合项目 每个文件代表一个项目。`item` 字段是它在列表中的位置(从 1 开始)。 ```ts fileName="faq.1.content.ts" contentDeclarationFormat={["typescript", "esm", "commonjs"]} 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; ``` ```ts fileName="faq.2.content.ts" contentDeclarationFormat={["typescript", "esm", "commonjs"]} 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; ``` ## 使用集合 ### 所有项目 ```tsx fileName="FAQ.tsx" import { useIntlayer } from "react-intlayer"; export const FAQ = () => { const items = useIntlayer("faq"); // { question: string; answer: string }[] return ( ); }; ``` ```tsx fileName="FAQ.tsx" import { useIntlayer } from "next-intlayer"; export const FAQ = () => { const items = useIntlayer("faq"); // { question: string; answer: string }[] return ( ); }; ``` ```vue fileName="FAQ.vue" ``` ```svelte fileName="FAQ.svelte" ``` ```tsx fileName="FAQ.tsx" import { useIntlayer } from "preact-intlayer"; export const FAQ = () => { const items = useIntlayer("faq"); // { question: string; answer: string }[] return ( ); }; ``` ```tsx fileName="FAQ.tsx" import { useIntlayer } from "solid-intlayer"; import { For } from "solid-js"; export const FAQ = () => { const items = useIntlayer("faq"); // { question: string; answer: string }[] return ( ); }; ``` ```typescript fileName="faq.component.ts" import { Component } from "@angular/core"; import { useIntlayer } from "angular-intlayer"; @Component({ selector: "app-faq", template: ` `, }) export class FAQComponent { items = useIntlayer("faq"); } ``` ```javascript fileName="faq.js" import { useIntlayer } from "vanilla-intlayer"; const faq = useIntlayer("faq"); faq.forEach((item) => { console.log(item.question); console.log(item.answer); }); ``` ### 按索引获取单个项目 ```tsx const faq2 = useIntlayer("faq", { item: 2 }); // → { question: string; answer: string } ``` ### 使用显式语言环境获取单个项目 ```tsx const faq2Zh = useIntlayer("faq", { item: 2, locale: "zh" }); ``` ## 典型使用场景 - 常见问题 (FAQ) 列表 - 定价层级 / 资费计划 - 轮播图 / 幻灯片内容 - 分步说明步骤