Intlayerの今後のリリースに関する通知を受け取る
    作成:2024-08-11最終更新:2025-06-29

    React 統合: useIntlayer フック ドキュメント

    このセクションでは、React アプリケーション内で useIntlayer フックを使用する方法について詳しく説明し、効率的なコンテンツのローカライズを可能にします。

    React での useIntlayer のインポート

    useIntlayer フックは、コンテキストに応じてインポートすることで React アプリケーションに統合できます。

    • クライアントコンポーネント:

      typescript
      import { useIntlayer } from "react-intlayer"; // クライアントサイドの React コンポーネントで使用
    • サーバーコンポーネント:

    パラメーター

    このフックは2つのパラメーターを受け取ります:

    1. key: ローカライズされたコンテンツを取得するための辞書キー。
    2. locale(オプション): 希望するロケール。指定しない場合はコンテキストのロケールがデフォルトで使用されます。

    辞書

    すべての辞書キーは型安全性を高め、エラーを防ぐためにコンテンツ宣言ファイル内で宣言されている必要があります。セットアップ手順はこちらで確認できます。

    Reactでの使用例

    Reactコンポーネント内でのuseIntlayerフックの使用例:

    src/app.tsx
    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>    </>  );};
    src/components/ComponentExample.tsx
    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>  );};
    src/components/ServerComponentExample.tsx
    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>  );};

    属性の取り扱い

    属性をローカライズする際は、コンテンツの値に適切にアクセスしてください:

    jsx
    <button title={content.buttonTitle.value}>{content.buttonText}</button>

    追加リソース

    • Intlayer ビジュアルエディター: より直感的なコンテンツ管理体験のために、ビジュアルエディターのドキュメントはこちらを参照してください。

    このセクションは、Reactアプリケーションにおける useIntlayer フックの統合に特化しており、ローカリゼーションプロセスを簡素化し、異なるロケール間でのコンテンツの一貫性を確保します。

    属性の取り扱い

    属性をローカライズする際は、コンテンツの値に適切にアクセスしてください:

    jsx
    <button title={content.buttonTitle.value}>{content.buttonText}</button>

    追加リソース

    • Intlayer ビジュアルエディター: より直感的なコンテンツ管理体験のために、ビジュアルエディターのドキュメントはこちらをご参照ください。

    このセクションは特に、Reactアプリケーションにおける useIntlayer フックの統合に焦点を当てており、ローカライズプロセスを簡素化し、異なるロケール間でのコンテンツの一貫性を確保します。

    ドキュメント履歴

    • 5.5.10 - 2025-06-29: 履歴初期化
    Intlayerの今後のリリースに関する通知を受け取る