Auteur:
    Création:2025-02-07Dernière mise à jour:2025-06-29

    Contenu Conditionnel / Condition dans Intlayer

    Comment Fonctionne la Condition

    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;

    Configuration du Contenu Conditionnel

    Pour configurer le contenu conditionnel dans votre projet Intlayer, créez un module de contenu qui inclut vos définitions conditionnelles. Voici des exemples dans différents formats.

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "mon contenu lorsque c'est vrai",
          false: "mon contenu lorsque c'est faux",
          fallback: "mon contenu lorsque la condition échoue", // Optionnel
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    Si aucun fallback n'est déclaré, la dernière clé déclarée sera prise comme fallback si la condition n'est pas validée.

    Utilisation du Contenu Conditionnel avec 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;

    Ressources Supplémentaires

    Pour plus d'informations détaillées sur la configuration et l'utilisation, consultez les ressources suivantes :

    Ces ressources offrent des informations supplémentaires sur la configuration et l'utilisation d'Intlayer dans divers environnements et frameworks.