מחבר:
    נוצר:2025-02-07עדכון אחרון:2026-11-03

    קינון / הפניה לתת-תוכן ב-Intlayer

    כיצד קינון עובד

    ב-Intlayer, קינון מושג באמצעות הפונקציה nest, המאפשרת לך להפנות לתוכן ממילון אחר ולעשות בו שימוש חוזר. במקום לשכפל תוכן, תוכל להצביע על מודול תוכן קיים לפי המפתח שלו.

    הגדרת קינון

    כדי להגדיר קינון בפרויקט Intlayer שלך, תחילה תגדיר את תוכן הבסיס שבו תרצה לעשות שימוש חוזר. לאחר מכן, במודול תוכן נפרד, תשתמש בפונקציה nest כדי לייבא תוכן זה.

    מילון בסיס

    להלן דוגמה למילון בסיס להטמעה במילון אחר:

    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;
    

    הפניה באמצעות Nest

    כעת, צור מודול תוכן נוסף המשתמש בפונקציה nest כדי להפנות לתוכן לעיל. תוכל להפנות לתוכן המלא או לערך מקונן ספציפי:

    secondDictionary.content.ts
    import { nest, type Dictionary } from "intlayer";
    
    const myNestingContent = {
      key: "key_of_my_second_dictionary",
      content: {
        // הפניה למילון המלא:
        fullNestedContent: nest("key_of_my_first_dictionary"),
        // הפניה לערך מקונן ספציפי:
        partialNestedContent: nest(
          "key_of_my_first_dictionary",
          "subContent.contentNumber"
        ),
      },
    } satisfies Dictionary;
    
    export default myNestingContent;
    

    כפרמטר שני, תוכל לציין נתיב לערך מקונן בתוך אותו תוכן. כאשר לא מסופק נתיב, מוחזר התוכן המלא של המילון המופנה.

    שימוש בקינון

    כדי להשתמש בתוכן מקונן ברכיב React, השתמש ב-hook‏ useIntlayer מתוך החבילה react-intlayer. ה-hook שולף את התוכן הנכון על פי המפתח שצוין. להלן דוגמה לשימוש בו:

    **/*.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;
    

    כדי להשתמש בתוכן מקונן ב-Client Components של Next.js, אחזר אותו באמצעות ה-hook‏ useIntlayer. הנה דוגמה:

    **/*.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;
    

    כדי להשתמש בתוכן מקונן ברכיבי Vue, אחזר אותו באמצעות ה-composable‏ useIntlayer. הנה דוגמה:

    **/*.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>
    

    כדי להשתמש בתוכן מקונן ברכיבי Svelte, אחזר אותו באמצעות ה-hook‏ useIntlayer. הגישה ל-store מתבצעת עם $. הנה דוגמה:

    **/*.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>
    

    כדי להשתמש בתוכן מקונן ברכיבי Preact, אחזר אותו באמצעות ה-hook‏ useIntlayer. הנה דוגמה:

    **/*.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;
    

    כדי להשתמש בתוכן מקונן ברכיבי SolidJS, אחזר אותו באמצעות ה-hook‏ useIntlayer. הנה דוגמה:

    **/*.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;
    

    כדי להשתמש בתוכן מקונן ברכיבי Angular, אחזר אותו באמצעות ה-hook‏ useIntlayer. הנה דוגמה:

    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;
    }
    

    כדי להשתמש בתוכן מקונן עם vanilla-intlayer, אחזר אותו באמצעות ה-hook‏ useIntlayer. הנה דוגמה:

    **/*.ts
    import { installIntlayer, useIntlayer } from "vanilla-intlayer";
    
    installIntlayer();
    
    const content = useIntlayer("key_of_my_second_dictionary").onChange(
      (newContent) => {
        document.getElementById("nested")!.textContent =
          newContent.partialNestedContent;
      }
    );
    
    // רינדור ראשוני
    document.getElementById("nested")!.textContent = content.partialNestedContent;
    

    משאבים נוספים

    למידע מפורט יותר על הגדרות ושימוש, עיין במשאבים הבאים:

    משאבים אלה מספקים תובנות נוספות לגבי ההגדרה והשימוש ב-Intlayer בסביבות ובפריימוורקים שונים.