Creation:2025-02-07Last update:2025-06-29
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
此页面的内容已使用 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
条件内容 / Intlayer 中的条件
条件如何工作
在 Intlayer 中,通过 cond 函数实现条件内容,该函数将特定条件(通常是布尔值)映射到其对应的内容。这种方法使您能够根据给定条件动态选择内容。当与 React Intlayer 或 Next Intlayer 集成时,会根据运行时提供的条件自动选择适当的内容。
设置条件内容
要在您的 Intlayer 项目中设置条件内容,请创建一个包含条件定义的内容模块。以下是各种格式的示例。
**/*.content.ts
复制代码
复制代码到剪贴板
import { cond, type Dictionary } from "intlayer";
const myConditionalContent = {
key: "my_key",
content: {
myCondition: cond({
true: "当条件为真时的内容",
false: "当条件为假时的内容",
fallback: "当条件不满足时的内容", // 可选
}),
},
} satisfies Dictionary;
export default myConditionalContent;如果未声明 fallback,当条件不满足时将使用最后声明的键作为 fallback。
在 React Intlayer 中使用条件内容
要在 React 组件中使用条件内容,请从 react-intlayer 包中导入并使用 useIntlayer 钩子。此钩子会获取指定键的内容,并允许您传入条件以选择适当的输出。
**/*.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const ConditionalComponent: FC = () => {
const { myCondition } = useIntlayer("my_key");
return (
<div>
<p>
{
/* 输出: 当条件为真时的内容 */
myCondition(true)
}
</p>
<p>
{
/* 输出: 当条件为假时的内容 */
myCondition(false)
}
</p>
<p>
{
/* 输出: 当条件不满足时的内容 */
myCondition("")
}
</p>
<p>
{
/* 输出: 当条件不满足时的内容 */
myCondition(undefined)
}
</p>
</div>
);
};
export default ConditionalComponent;其他资源
有关配置和使用的更多详细信息,请参考以下资源:
这些资源提供了在各种环境和框架中设置和使用 Intlayer 的进一步见解。