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
React 集成:useDictionary 钩子文档
本节提供了在 React 应用中使用 useDictionary 钩子的详细指导,使得无需视觉编辑器即可高效处理本地化内容。
React 中的示例用法
以下是如何在 React 组件中使用 useDictionary 钩子的示例:
./ComponentExample.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useDictionary } from "react-intlayer";
import componentContent from "./component.content";
const ComponentExample: FC = () => {
const { title, content } = useDictionary(componentContent);
return (
<div>
<h1>{title}</h1>
<p>{content}</p>
</div>
);
};服务器集成
如果您在 IntlayerProvider 之外使用 useDictionary 钩子,则在渲染组件时必须显式提供 locale 作为参数:
./ServerComponentExample.tsx
复制代码
复制代码到剪贴板
import type { FC } from "react";
import { useDictionary } from "react-intlayer/server";
import clientComponentExampleContent from "./component.content";
const ServerComponentExample: FC<{ locale: string }> = ({ locale }) => {
const { content } = useDictionary(clientComponentExampleContent, locale);
return (
<div>
<h1>{content.title}</h1>
<p>{content.content}</p>
</div>
);
};