このページとあなたの好きなAIアシスタントを使ってドキュメントを要約します
このページのコンテンツはAIを使用して翻訳されました。
英語の元のコンテンツの最新バージョンを見るこのドキュメントを改善するアイデアがある場合は、GitHubでプルリクエストを送信することで自由に貢献してください。
ドキュメントへのGitHubリンクドキュメントのMarkdownをクリップボードにコピー
React 統合: useIntlayer フック ドキュメント
このセクションでは、React アプリケーション内で useIntlayer フックを使用する方法について詳しく説明し、効率的なコンテンツのローカライズを可能にします。
React での useIntlayer のインポート
useIntlayer フックは、コンテキストに応じてインポートすることで React アプリケーションに統合できます。
クライアントコンポーネント:
typescriptコードをコピーコードをクリップボードにコピー
import { useIntlayer } from "react-intlayer"; // クライアントサイドの React コンポーネントで使用サーバーコンポーネント:
パラメーター
このフックは2つのパラメーターを受け取ります:
key: ローカライズされたコンテンツを取得するための辞書キー。locale(オプション): 希望するロケール。指定しない場合はコンテキストのロケールがデフォルトで使用されます。
辞書
すべての辞書キーは型安全性を高め、エラーを防ぐためにコンテンツ宣言ファイル内で宣言されている必要があります。セットアップ手順はこちらで確認できます。
Reactでの使用例
Reactコンポーネント内でのuseIntlayerフックの使用例:
コードをクリップボードにコピー
import type { FC } from "react";
import { ClientComponentExample, ServerComponentExample } from "@components";
import { IntlayerProvider } from "react-intlayer";
import { useIntlayer, IntlayerServerProvider } from "react-intlayer/server";
import { Locales } from "intlayer";
const App: FC<{ locale: Locales }> = ({ locale }) => {
const content = useIntlayer("homepage", locale);
return (
<>
<p>{content.introduction}</p>
<IntlayerProvider locale={locale}>
<ClientComponentExample />
</IntlayerProvider>
<IntlayerServerProvider locale={locale}>
<ServerComponentExample />
</IntlayerServerProvider>
</>
);
};コードをクリップボードにコピー
import type { FC } from "react";
import { useIntlayer } from "react-intlayer";
const ComponentExample: FC = () => {
const content = useIntlayer("component-example");
return (
<div>
<h1>{content.title}</h1>
<p>{content.description}</p>
</div>
);
};コードをクリップボードにコピー
import { useIntlayer } from "react-intlayer/server";jsx fileName="src/components/ServerComponentExample.csx" codeFormat="commonjs"const { useIntlayer } = require("react-intlayer/server");const ServerComponentExample = () => { const content = useIntlayer("server-component"); return ( <div> <h1>{content.title}</h1> <p>{content.description}</p> </div> );};属性の取り扱い
属性をローカライズする際は、コンテンツの値に適切にアクセスしてください:
コードをクリップボードにコピー
<button title={content.buttonTitle.value}>{content.buttonText}</button>追加リソース
- Intlayer ビジュアルエディター: より直感的なコンテンツ管理体験のために、ビジュアルエディターのドキュメントはこちらを参照してください。
このセクションは、Reactアプリケーションにおける useIntlayer フックの統合に特化しており、ローカリゼーションプロセスを簡素化し、異なるロケール間でのコンテンツの一貫性を確保します。
属性の取り扱い
属性をローカライズする際は、コンテンツの値に適切にアクセスしてください:
コードをクリップボードにコピー
<button title={content.buttonTitle.value}>{content.buttonText}</button>追加リソース
- Intlayer ビジュアルエディター: より直感的なコンテンツ管理体験のために、ビジュアルエディターのドキュメントはこちらをご参照ください。
このセクションは特に、Reactアプリケーションにおける useIntlayer フックの統合に焦点を当てており、ローカライズプロセスを簡素化し、異なるロケール間でのコンテンツの一貫性を確保します。