\n\n\n```\n\n \n \n\nTo utilize conditional content in Svelte components, retrieve it via the `useIntlayer` hook. The store is accessed with `$`. Here's an example:\n\n```svelte fileName=\"**/*.svelte\"\n\n\n
\n

{$content.myCondition(true)}

\n

{$content.myCondition(false)}

\n
\n```\n\n
\n \n\nTo utilize conditional content in Preact components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { FC } from \"preact\";\nimport { useIntlayer } from \"preact-intlayer\";\n\nconst ConditionalComponent: FC = () => {\n const { myCondition } = useIntlayer(\"my_key\");\n\n return (\n
\n

{myCondition(true)}

\n

{myCondition(false)}

\n
\n );\n};\n\nexport default ConditionalComponent;\n```\n\n
\n \n\nTo utilize conditional content in SolidJS components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```tsx fileName=\"**/*.tsx\" codeFormat={[\"typescript\", \"esm\"]}\nimport type { Component } from \"solid-js\";\nimport { useIntlayer } from \"solid-intlayer\";\n\nconst ConditionalComponent: Component = () => {\n const { myCondition } = useIntlayer(\"my_key\");\n\n return (\n
\n

{myCondition(true)}

\n

{myCondition(false)}

\n
\n );\n};\n\nexport default ConditionalComponent;\n```\n\n
\n \n\nTo utilize conditional content in Angular components, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"app.component.ts\" codeFormat=\"typescript\"\nimport { Component } from \"@angular/core\";\nimport { useIntlayer } from \"angular-intlayer\";\n\n@Component({\n selector: \"app-conditional\",\n template: `\n
\n

{{ content().myCondition(true) }}

\n

{{ content().myCondition(false) }}

\n
\n `,\n})\nexport class ConditionalComponent {\n content = useIntlayer(\"my_key\");\n}\n```\n\n
\n \n\nTo utilize conditional content with `vanilla-intlayer`, retrieve it via the `useIntlayer` hook. Here's an example:\n\n```typescript fileName=\"**/*.ts\" codeFormat={[\"typescript\", \"esm\"]}\nimport { installIntlayer, useIntlayer } from \"vanilla-intlayer\";\n\ninstallIntlayer();\n\nconst content = useIntlayer(\"my_key\").onChange((newContent) => {\n document.getElementById(\"true-content\")!.textContent =\n newContent.myCondition(true);\n document.getElementById(\"false-content\")!.textContent =\n newContent.myCondition(false);\n});\n\n// Initial render\ndocument.getElementById(\"true-content\")!.textContent =\n content.myCondition(true);\ndocument.getElementById(\"false-content\")!.textContent =\n content.myCondition(false);\n```\n\n \n\n\n## 追加リソース\n\n設定と使用に関する詳細情報については、以下のリソースを参照してください:\n\n- [Intlayer CLI ドキュメント](/ja/doc/concept/cli)\n- [React Intlayer ドキュメント](/ja/doc/environment/create-react-app)\n- [Next Intlayer ドキュメント](/ja/doc/environment/nextjs/15)\n\nこれらのリソースは、さまざまな環境やフレームワークでのIntlayerの設定と使用に関するさらなる洞察を提供します。\n","description":"Intlayerで条件付きコンテンツを使用し、特定の条件に基づいて動的にコンテンツを表示する方法を学びます。このドキュメントに従って、効率的に条件を実装しましょう。","url":"https://intlayer.org/ja/doc/concept/content/condition","datePublished":"2025-02-07","dateModified":"2025-06-29","keywords":"条件内容, 動的レンダリング, 文書, Intlayer, Next.js, JavaScript, React","license":"https://raw.githubusercontent.com/aymericzip/intlayer/refs/heads/main/LICENSE","audience":{"@type":"Audience","audienceType":"開発者、コンテンツマネージャー"}}
    著者:
    作成:2025-02-07最終更新:2025-06-29

    条件付きコンテンツ / Intlayerの条件

    条件の仕組み

    Intlayerでは、条件付きコンテンツはcond関数を使用して実現されます。この関数は特定の条件(通常はブール値)を対応するコンテンツにマッピングします。このアプローチにより、指定された条件に基づいて動的にコンテンツを選択することが可能になります。React IntlayerやNext Intlayerと統合することで、実行時に提供された条件に応じて適切なコンテンツが自動的に選択されます。

    条件付きコンテンツの設定

    Intlayerプロジェクトで条件付きコンテンツを設定するには、条件定義を含むコンテンツモジュールを作成します。以下に、さまざまな形式での例を示します。

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "条件がtrueの場合のコンテンツ",
          false: "条件がfalseの場合のコンテンツ",
          fallback: "条件が失敗した場合のコンテンツ", // オプション
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    フォールバックが宣言されていない場合、条件が検証されない場合は最後に宣言されたキーがフォールバックとして使用されます。

    React Intlayerでの条件付きコンテンツの使用

    To utilize conditional content within a React component, import and use the useIntlayer hook from the react-intlayer package. This hook fetches the content for the specified key and allows you to pass in a condition to select the appropriate output.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const ConditionalComponent: FC = () => {
    const { myCondition } = useIntlayer("my_key");
    
    return (
      <div>
        <p>
          {
            /* Output: my content when it's true */
            myCondition(true)
          }
        </p>
        <p>
          {
            /* Output: my content when it's false */
            myCondition(false)
          }
        </p>
        <p>
          {
            /* Output: my content when the condition fails */
            myCondition("")
          }
        </p>
        <p>
          {
            /* Output: my content when the condition fails */
            myCondition(undefined)
          }
        </p>
      </div>
    );
    };
    
    export default ConditionalComponent;

    追加リソース

    設定と使用に関する詳細情報については、以下のリソースを参照してください:

    これらのリソースは、さまざまな環境やフレームワークでのIntlayerの設定と使用に関するさらなる洞察を提供します。