intlayer-react について
Intlayer で翻訳対応の React コンポーネントを作るためのスキルです。コンポーネントの近くにコンテンツを宣言し、`useIntlayer` フックで辞書をコンポーネントに接続します。
主な機能
- 01コンポーネント単位のコンテンツ宣言
- 02`t()` による翻訳辞書の作成
- 03`useIntlayer` フックの利用
- 04React 環境の参照情報
活用シーン
- 01React コンポーネントへの翻訳コンテンツ追加
- 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 ファイルで宣言します。
ワークフロー
翻訳されたコンポーネントを作成するには、2 つのファイルが必要です。
- 宣言: 辞書を定義するコンテンツファイル(例:
myComponent.content.ts)。 - 実装:
useIntlayerフックを使用する React コンポーネント(例:MyComponent.tsx)。
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 対応