Autor:
    Erstellung:2025-02-07Letzte Aktualisierung:2025-06-29

    Verschachtelung / Sub-Content-Referenzierung

    Wie Verschachtelung funktioniert

    In Intlayer wird die Verschachtelung durch die Funktion nest erreicht, die es Ihnen ermöglicht, Inhalte aus einem anderen Wörterbuch zu referenzieren und wiederzuverwenden. Anstatt Inhalte zu duplizieren, können Sie auf ein bestehendes Inhaltsmodul mit seinem Schlüssel verweisen.

    Nesting einrichten

    Um Nesting in Ihrem Intlayer-Projekt einzurichten, definieren Sie zunächst den Basisinhalt, den Sie wiederverwenden möchten. Dann verwenden Sie in einem separaten Content-Modul die Funktion nest, um diesen Inhalt zu importieren.

    Basis-Dictionary

    Nachfolgend ist ein Beispiel eines Basis-Dictionary zum Verschachteln in einem anderen Dictionary:

    firstDictionary.content.ts
    import { type Dictionary } from "intlayer";
    
    const firstDictionary = {
      key: "key_of_my_first_dictionary",
      content: {
        content: "content",
        subContent: {
          contentNumber: 0,
          contentString: "string",
        },
      },
    } satisfies Dictionary;
    
    export default firstDictionary;
    

    Referencing mit Nest

    Erstellen Sie nun ein weiteres Content-Modul, das die nest-Funktion verwendet, um auf den obigen Inhalt zu verweisen. Sie können auf den gesamten Inhalt oder auf einen bestimmten verschachtelten Wert verweisen:

    secondDictionary.content.ts
    import { nest, type Dictionary } from "intlayer";
    
    const myNestingContent = {
      key: "key_of_my_second_dictionary",
      content: {
        // Referenziert das gesamte Dictionary:
        fullNestedContent: nest("key_of_my_first_dictionary"),
        // Referenziert einen bestimmten verschachtelten Wert:
        partialNestedContent: nest(
          "key_of_my_first_dictionary",
          "subContent.contentNumber"
        ),
      },
    } satisfies Dictionary;
    
    export default myNestingContent;
    

    Als zweiter Parameter können Sie einen Pfad zu einem verschachtelten Wert innerhalb dieses Inhalts angeben. Wenn kein Pfad angegeben wird, wird der gesamte Inhalt des referenzierten Dictionaries zurückgegeben.

    Einrichtung der Verschachtelung

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

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const NestComponent: FC = () => {
      const { fullNestedContent, partialNestedContent } = useIntlayer(
        "key_of_my_second_dictionary"
      );
    
      return (
        <div>
          <p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
          <p>Partial Nested Value: {partialNestedContent}</p>
        </div>
      );
    };
    
    export default NestComponent;
    

    To use nested content in Next.js Client Components, retrieve it via the useIntlayer hook. Here's an example:

    **/*.tsx
    "use client";
    
    import type { FC } from "react";
    import { useIntlayer } from "next-intlayer";
    
    const NestComponent: FC = () => {
      const { fullNestedContent, partialNestedContent } = useIntlayer(
        "key_of_my_second_dictionary"
      );
    
      return (
        <div>
          <p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
          <p>Partial Nested Value: {partialNestedContent}</p>
        </div>
      );
    };
    
    export default NestComponent;
    

    To use nested content in Vue components, retrieve it via the useIntlayer hook. Here's an example:

    **/*.vue
    <script setup lang="ts">
    import { useIntlayer } from "vue-intlayer";
    
    const { fullNestedContent, partialNestedContent } = useIntlayer(
      "key_of_my_second_dictionary"
    );
    </script>
    
    <template>
      <div>
        <p>Full Nested Content: {{ JSON.stringify(fullNestedContent) }}</p>
        <p>Partial Nested Value: {{ partialNestedContent }}</p>
      </div>
    </template>
    

    To use nested content in Svelte components, retrieve it via the useIntlayer hook. The store is accessed with $. Here's an example:

    **/*.svelte
    <script lang="ts">
    import { useIntlayer } from "svelte-intlayer";
    
    const content = useIntlayer("key_of_my_second_dictionary");
    </script>
    
    <div>
      <p>Full Nested Content: {JSON.stringify($content.fullNestedContent)}</p>
      <p>Partial Nested Value: {$content.partialNestedContent}</p>
    </div>
    

    To use nested content in Preact components, retrieve it via the useIntlayer hook. Here's an example:

    **/*.tsx
    import type { FC } from "preact";
    import { useIntlayer } from "preact-intlayer";
    
    const NestComponent: FC = () => {
      const { fullNestedContent, partialNestedContent } = useIntlayer(
        "key_of_my_second_dictionary"
      );
    
      return (
        <div>
          <p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
          <p>Partial Nested Value: {partialNestedContent}</p>
        </div>
      );
    };
    
    export default NestComponent;
    

    To use nested content in SolidJS components, retrieve it via the useIntlayer hook. Here's an example:

    **/*.tsx
    import type { Component } from "solid-js";
    import { useIntlayer } from "solid-intlayer";
    
    const NestComponent: Component = () => {
      const { fullNestedContent, partialNestedContent } = useIntlayer(
        "key_of_my_second_dictionary"
      );
    
      return (
        <div>
          <p>Full Nested Content: {JSON.stringify(fullNestedContent)}</p>
          <p>Partial Nested Value: {partialNestedContent}</p>
        </div>
      );
    };
    
    export default NestComponent;
    

    To use nested content in Angular components, retrieve it via the useIntlayer hook. Here's an example:

    app.component.ts
    import { Component } from "@angular/core";
    import { useIntlayer } from "angular-intlayer";
    
    @Component({
      selector: "app-nest",
      template: `
        <div>
          <p>
            Full Nested Content: {{ JSON.stringify(content().fullNestedContent) }}
          </p>
          <p>Partial Nested Value: {{ content().partialNestedContent }}</p>
        </div>
      `,
    })
    export class NestComponent {
      content = useIntlayer("key_of_my_second_dictionary");
      JSON = JSON;
    }
    

    To use nested content with vanilla-intlayer, retrieve it via the useIntlayer hook. Here's an example:

    **/*.ts
    import { installIntlayer, useIntlayer } from "vanilla-intlayer";
    
    installIntlayer();
    
    const content = useIntlayer("key_of_my_second_dictionary").onChange(
      (newContent) => {
        document.getElementById("nested")!.textContent =
          newContent.partialNestedContent;
      }
    );
    
    // Initial render
    document.getElementById("nested")!.textContent = content.partialNestedContent;
    

    Zusätzliche Ressourcen

    Für detailliertere Informationen zur Konfiguration und Nutzung lesen Sie die folgenden Ressourcen:

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