Autor:
    Erstellung:2024-08-11Letzte Aktualisierung:2025-06-29

    Enumeration / Pluralisierung

    Wie Enumeration funktioniert

    To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const CarComponent: FC = () => {
    const { numberOfCar } = useIntlayer("car_count");
    
    return (
      <div>
        <p>
          {
            numberOfCar(0) // Output: No cars
          }
        </p>
        <p>
          {
            numberOfCar(6) // Output: Some cars
          }
        </p>
        <p>
          {
            numberOfCar(20) // Output: Many cars
          }
        </p>
        <p>
          {
            numberOfCar(0.01) // Output: Fallback value
          }
        </p>
      </div>
    );
    };

    Einrichtung der Enumeration

    Um eine Enumeration in Ihrem Intlayer-Projekt einzurichten, müssen Sie ein Inhaltsmodul erstellen, das Enumeration-Definitionen enthält. Hier ist ein Beispiel für eine einfache Enumeration der Anzahl von Autos:

    **/*.content.ts
    import { enu, type Dictionary } from "intlayer";
    
    const carEnumeration = {
      key: "car_count",
      content: {
        numberOfCar: enu({
          "<-1": "Weniger als minus ein Auto",
          "-1": "Minus ein Auto",
          "0": "Keine Autos",
          "1": "Ein Auto",
          ">5": "Einige Autos",
          ">19": "Viele Autos",
          "fallback": "Fallback-Wert", // Optional
        }),
      },
    } satisfies Dictionary;
    
    export default carEnumeration;

    In diesem Beispiel ordnet enu verschiedene Bedingungen spezifischen Inhalten zu. Wenn es in einer React-Komponente verwendet wird, kann Intlayer automatisch den passenden Inhalt basierend auf der übergebenen Variable auswählen.

    Die Reihenfolge der Deklaration ist in Intlayer-Enumerationen wichtig. Die erste gültige Deklaration wird verwendet. Wenn mehrere Bedingungen zutreffen, stellen Sie sicher, dass sie korrekt sortiert sind, um unerwartetes Verhalten zu vermeiden.
    Wenn kein Fallback deklariert ist, gibt die Funktion undefined zurück, falls keine Schlüssel übereinstimmen.

    Verwendung von Enumeration mit React Intlayer

    To use enumeration in a React component, you can leverage the useIntlayer hook from the react-intlayer package. This hook retrieves the correct content based on the specified ID. Here's an example of how to use it:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const CarComponent: FC = () => {
    const { numberOfCar } = useIntlayer("car_count");
    
    return (
      <div>
        <p>
          {
            numberOfCar(0) // Output: No cars
          }
        </p>
        <p>
          {
            numberOfCar(6) // Output: Some cars
          }
        </p>
        <p>
          {
            numberOfCar(20) // Output: Many cars
          }
        </p>
        <p>
          {
            numberOfCar(0.01) // Output: Fallback value
          }
        </p>
      </div>
    );
    };

    Combining Enumeration with Insert for Ordinal Numbers

    A common use case is displaying ordinal numbers (1st, 2nd, 3rd, etc.). You can combine enu with insert to create dynamic ordinal content:

    **/*.content.ts
    import { enu, insert, type Dictionary } from "intlayer";
    
    const rankingContent = {
      key: "ranking_component",
      content: {
        ordinal: enu({
          1: insert("{{count}}st place"),
          2: insert("{{count}}nd place"),
          3: insert("{{count}}rd place"),
          fallback: insert("{{count}}th place"),
        }),
      },
    } satisfies Dictionary;
    
    export default rankingContent;

    Using Ordinal Enumeration

    To use this in a React component, call the enumeration with the last digit of the number to get the correct suffix, then pass the full count as the insertion value:

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const RankingComponent: FC<{ count: number }> = ({ count }) => {
    const { ordinal } = useIntlayer("ranking_component");
    
    // Get the last digit to determine the correct suffix
    const lastDigit = Math.abs(count) % 10;
    
    return (
      <div>
        <p>
          {
            ordinal(lastDigit)({ count }) // e.g., "5th place" for count=5
          }
        </p>
      </div>
    );
    };

    Additional Resources

    For more detailed information on configuration and usage, refer to the following resources:

    These resources provide further insights into the setup and usage of Intlayer in different environments and with various frameworks.