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

    动态记录

    动态记录(dynamic record)是指其唯一身份标识不是顺序索引或命名变体,而是在 meta 字段中声明的任意键值对集合的内容文件。Intlayer 将在运行时使用这些字段作为选择器,从而可以定位 CMS 记录、用户特定文案或任何在构建时未知其键的内容。

    声明动态记录

    product-copy.abc.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product-copy",
      meta: {
        id: "prod_abc",
        userId: "user_123",
      },
      content: {
        name: t({ en: "Widget Pro", fr: "Widget Pro" }),
        description: t({ en: "The best widget.", fr: "Le meilleur widget." }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    product-copy.abcd.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product-copy",
      meta: {
        id: "prod_abcd",
        userId: "user_123",
      },
      content: {
        name: t({ en: "Widget Lite", fr: "Widget Lite" }),
        description: t({ en: "A lighter option.", fr: "Une option plus légère." }),
      },
    } satisfies Dictionary;
    
    export default dictionary;

    使用动态记录

    选择器中所有的 meta 字段都是必填的。省略任何字段都将返回 null 并导致 TypeScript 报错。

    ProductCopy.tsx
    import { useIntlayer } from "react-intlayer";
    
    export const ProductCopy = ({
      productId,
      userId,
    }: {
      productId: string;
      userId: string;
    }) => {
      const content = useIntlayer("product-copy", { id: productId, userId });
      // TypeScript 会强制要求同时提供 `id` 和 `userId`。
    
      if (!content) return null;
    
      return <p>{content.description}</p>;
    };

    使用显式语言环境

    tsx
    const content = useIntlayer("product-copy", {  id: "prod_abc",  userId: "user_123",  locale: "zh",});

    缺少 meta 字段 — 编译时错误

    ts
    // 类型错误:缺少 `userId`const content = useIntlayer("product-copy", { id: "prod_abc" });

    加载模式 (loading mode)

    动态记录通常采用延迟加载方式。在字典中设置 importMode 来控制此行为:

    ts
    const dictionary = {
      key: "product-copy",
      importMode: "fetch", // 或 "dynamic"
      meta: { id: "prod_abc", userId: "user_123" },
      content: { … },
    } satisfies Dictionary;
    
    export default dictionary;

    关于 staticdynamicfetch 模式的详细信息,请参阅分包优化

    典型使用场景

    • 在 CMS 中管理的单件商品营销文案
    • 用户特定或账户特定的内容
    • 在运行时通过不透明 ID 标识的任何内容