Autor:
    Erstellung:2026-05-04Letzte Aktualisierung:2026-05-04

    Pluralinhalt / Plural in Intlayer

    Wie Plural funktioniert

    To use plural content inside a React component, retrieve it via the useIntlayer hook and call it with a count. The active locale and the count are combined to pick the matching CLDR category.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const OpeningsComponent: FC<{ count: number }> = ({ count }) => {
    const { totalOpenings } = useIntlayer("total_openings");
    
    return (
      <div>
        <p>{totalOpenings(count)}</p>
      </div>
    );
    };
    
    export default OpeningsComponent;

    Wann man plural vs enu verwendet

    To use plural content inside a React component, retrieve it via the useIntlayer hook and call it with a count. The active locale and the count are combined to pick the matching CLDR category.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const OpeningsComponent: FC<{ count: number }> = ({ count }) => {
    const { totalOpenings } = useIntlayer("total_openings");
    
    return (
      <div>
        <p>{totalOpenings(count)}</p>
      </div>
    );
    };
    
    export default OpeningsComponent;

    Pluralinhalt einrichten

    Um Pluralinhalt in Ihrem Intlayer-Projekt einzurichten, erstellen Sie ein Inhaltsmodul, das den plural-Helfer verwendet. Die Kategorie other ist erforderlich und wird als Fallback verwendet, wenn ein Gebietsschema keine spezifischere Kategorie definiert.

    **/*.content.ts
    import { plural, t, type Dictionary } from "intlayer";
    
    const openingsContent = {
      key: "total_openings",
      content: {
        totalOpenings: t({
          en: plural({
            one: "{{count}} opening",
            other: "{{count}} openings",
          }),
          de: plural({
            one: "{{count}} Stelle",
            other: "{{count}} Stellen",
          }),
        }),
      },
    } satisfies Dictionary;
    
    export default openingsContent;

    Die unterstützten Kategorien sind zero, one, two, few, many, other. Sie müssen nur die Kategorien deklarieren, die Ihre Zielsprache verwendet – Intlayer fällt auf other zurück, wenn keine spezifische Kategorie passt.

    Der Platzhalter {{count}} wird automatisch durch die Anzahl ersetzt, die Sie zur Laufzeit übergeben. Sie können auch andere Platzhalter einschließen (siehe Benutzerdefinierte Platzhalter unten).

    Pluralinhalt mit React Intlayer verwenden

    Um Pluralinhalt in einer React-Komponente zu verwenden, rufen Sie ihn über den Hook useIntlayer ab und rufen Sie ihn mit einer Anzahl auf. Das aktive Gebietsschema und die Anzahl werden kombiniert, um die passende CLDR-Kategorie auszuwählen.

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const OpeningsComponent: FC<{ count: number }> = ({ count }) => {
      const { totalOpenings } = useIntlayer("total_openings");
    
      return (
        <div>
          {/* Auf Englisch:                                  */}
          {/*  count=0  → "0 openings"   (other)           */}
          {/*  count=1  → "1 opening"    (one)             */}
          {/*  count=2  → "2 openings"   (other)           */}
          {/*  count=21 → "21 openings"  (other)           */}
          <p>{totalOpenings(count)}</p>
        </div>
      );
    };
    
    export default OpeningsComponent;

    Sie können die zurückgegebene Funktion auf zwei äquivalente Arten aufrufen:

    tsx
    totalOpenings(21); // Kurzform: nur AnzahltotalOpenings({ count: 21 }); // explizite Form

    Benutzerdefinierte Platzhalter

    Plural-Zeichenfolgen können andere Platzhalter als {{count}} enthalten. Übergeben Sie diese in der Objektform zusammen mit count:

    **/*.content.ts
    import { plural, type Dictionary } from "intlayer";
    
    const inboxContent = {
      key: "inbox_summary",
      content: {
        summary: plural({
          one: "{{name}}, du hast {{count}} neue Nachricht",
          other: "{{name}}, du hast {{count}} neue Nachrichten",
        }),
      },
    } satisfies Dictionary;
    
    export default inboxContent;
    **/*.tsx
    const { summary } = useIntlayer("inbox_summary");
    
    summary({ count: 1, name: "Alice" });
    // → "Alice, du hast 1 neue Nachricht"
    
    summary({ count: 7, name: "Alice" });
    // → "Alice, du hast 7 neue Nachrichten"

    CLDR-Kategorien auf einen Blick

    Verschiedene Sprachen verwenden unterschiedliche Teilmengen der CLDR-Kategorien. Einige häufige Fälle:

    Sprache Verwendete Kategorien
    Englisch (en) one, other
    Französisch (fr) one, many, other
    Russisch (ru) one, few, many, other
    Polnisch (pl) one, few, many, other
    Arabisch (ar) zero, one, two, few, many, other
    Japanisch / Chinesisch nur other

    Sie müssen sich das nicht merken – deklarieren Sie die Kategorien, für die Sie Übersetzungen haben, und Intlayer wird bei Bedarf auf other zurückgreifen.

    Einschränkung

    Im Gegensatz zu anderen Knoten kann der plural-Knoten noch nicht mit untergeordneten Knoten verschachtelt werden.

    Beispiel:

    Gültig:

    ts
        totalOpenings: t({      en: plural({        one: "{{count}} opening",        other: "{{count}} openings",      }),      fr: plural({        one: "{{count}} offre",        other: "{{count}} offres",      }),    }),

    Ungültig:

    ts
    totalOpenings: plural({  one: t({    en: "{{count}} opening",    fr: "{{count}} offre",  }),  other: t({    en: "{{count}} openings",    fr: "{{count}} offres",  }),}),

    Zusätzliche Ressourcen

    Weitere detaillierte Informationen zur Konfiguration und Verwendung finden Sie in den folgenden Ressourcen:

    Diese Ressourcen bieten weitere Einblicke in die Einrichtung und Verwendung von Intlayer in verschiedenen Umgebungen und Frameworks.