Creation:2026-01-21Last update:2026-05-06
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "更新 Solid useIntlayer API 用法以直接访问属性"v8.9.02026/5/4
- "为所有导出项统一文档"v8.0.02026/1/21
此页面的内容已使用 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
useIntlayer Hook 文档
useIntlayer hook 允许你通过键从字典中检索本地化内容。在 Solid 中,该 hook 返回一个响应式的 accessor 函数,会在 locale 变化时自动更新。
用法
tsx
复制代码
复制代码到剪贴板
import { useIntlayer } from "solid-intlayer";const MyComponent = () => { const content = useIntlayer("my-dictionary-key"); return ( <div> <h1>{content.title}</h1> <p>{content.description}</p> </div> );};描述
该 hook 执行以下任务:
- Locale Detection:它使用
IntlayerProvider上下文中的当前 locale。 - Dictionary Injection:它会自动注入与所提供 key 对应的字典内容,使用由 Intlayer 编译器生成的优化声明。
- Reactivity:它返回一个 Solid 的 accessor (
Accessor<T>),当全局 locale 状态改变时会自动重新计算。 - 翻译处理:它根据检测到的 locale 解析内容,处理字典中任何
t()、enu()等定义。
参数
- key:字典的唯一键(在你的内容声明文件中定义)。
- locale(可选):覆盖当前的 locale。
返回值
一个 accessor 函数(() => Content),返回本地化后的内容。