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
动态记录
动态记录(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;关于 static、dynamic 和 fetch 模式的详细信息,请参阅分包优化。
典型使用场景
- 在 CMS 中管理的单件商品营销文案
- 用户特定或账户特定的内容
- 在运行时通过不透明 ID 标识的任何内容