关于 intlayer-react
用于使用 Intlayer 构建多语言 React 组件的技能。它通过与组件相邻的内容声明文件和 `useIntlayer` Hook,将词典内容接入组件。
主要功能
- 01组件级内容声明
- 02使用 `t()` 编写翻译词典
- 03使用 `useIntlayer` Hook
- 04React 环境参考
适用场景
- 01为 React 组件添加多语言内容
- 02将翻译文件放在组件代码旁
- 03将词典内容接入组件
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ aymericzip/ intlayer --skill intlayer-react安装后在 Claude Code 中输入/intlayer-react确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENCE.mdreferences/environment_create-react-app.mdenvironment_lynx-and-react.mdenvironment_react-native-and-expo.mdenvironment_tanstack-start.mdenvironment_vite-and-react_react-router-v7-fs-routes.mdenvironment_vite-and-react_react-router-v7.mdenvironment_vite-and-react.mdpackages_intlayer_exports.mdpackages_react-intlayer_exports.mdpackages_react-intlayer_IntlayerProvider.mdpackages_react-intlayer_MarkdownRenderer.mdpackages_react-intlayer_t.mdpackages_react-intlayer_useDictionary.mdpackages_react-intlayer_useI18n.mdpackages_react-intlayer_useIntlayer.mdpackages_react-intlayer_useLocale.md详细介绍
Intlayer React 用法
核心理念
Intlayer 倡导组件级内容声明。与其使用庞大的全局翻译文件,不如将内容声明在与使用它们的 React 组件相邻的 *.content.ts 文件中。
工作流
要创建翻译后的组件,需要两个文件:
- **声明:**内容文件(例如,
myComponent.content.ts),用于定义词典。 - **实现:**React 组件(例如,
MyComponent.tsx),使用useIntlayer钩子。
1. 声明内容
使用 t() 创建内容文件以添加翻译。
文件:src/components/MyComponent/myComponent.content.ts
import { t, type Dictionary } from "intlayer";
const content = {
// The 'key' must be unique and matches what you pass to useIntlayer()
key: "my-component",
content: {
text: t({
en: "Welcome",
fr: "Bienvenue",
es: "Hola",
// ... All other locales set in intlayer config file
}),
},
} satisfies Dictionary;
export default content;
Intlayer React 用法
设置
- Vite 和 React
- Create React App
- React Router v7
- React Router v7(fs routes)
- Tanstack Start
- React Native 和 Expo
- Lynx 和 React
useIntlayer 钩子
import { useIntlayer } from "react-intlayer";
const MyComponent = () => {
const content = useIntlayer("my-dictionary-key");
return (
<div>
<h1>
{/* Return react node */}
{content.text}
</h1>
{/* Return string (.value) */}
<img src={content.text.value} alt={content.text.value} />
</div>
);
};
参考
环境
包
评论
支持 Markdown