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

    תוכן מותנה / Condition ב-Intlayer

    כיצד Condition עובד

    ב-Intlayer, תוכן מותנה מושג באמצעות הפונקציה cond, אשר ממפה תנאים ספציפיים (בדרך כלל ערכים בוליאניים) לתוכן המתאים להם. גישה זו מאפשרת לבחור תוכן בצורה דינמית על בסיס תנאי נתון. בעת שילוב עם React Intlayer או Next Intlayer, התוכן המתאים נבחר אוטומטית בהתאם לתנאי המסופק בזמן ריצה (runtime).

    הגדרת תוכן מותנה

    כדי להגדיר תוכן מותנה בפרויקט Intlayer שלך, צור מודול תוכן הכולל את הגדרות התנאי שלך. להלן דוגמאות בפורמטים שונים.

    **/*.content.ts
    import { cond, type Dictionary } from "intlayer";
    
    const myConditionalContent = {
      key: "my_key",
      content: {
        myCondition: cond({
          true: "my content when it's true",
          false: "my content when it's false",
          fallback: "my content when the condition fails", // אופציונלי
        }),
      },
    } satisfies Dictionary;
    
    export default myConditionalContent;
    
    אם לא מוגדר fallback, המפתח האחרון שהוצהר ייבחר כ-fallback אם התנאי אינו מתקיים.

    שימוש בתוכן מותנה

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

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>
            {
              /* פלט: my content when it's true */
              myCondition(true)
            }
          </p>
          <p>
            {
              /* פלט: my content when it's false */
              myCondition(false)
            }
          </p>
          <p>
            {
              /* פלט: my content when the condition fails */
              myCondition("")
            }
          </p>
          <p>
            {
              /* פלט: my content when the condition fails */
              myCondition(undefined)
            }
          </p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

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

    **/*.tsx
    "use client";
    
    import type { FC } from "react";
    import { useIntlayer } from "next-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

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

    **/*.vue
    <script setup lang="ts">
    import { useIntlayer } from "vue-intlayer";
    
    const { myCondition } = useIntlayer("my_key");
    </script>
    
    <template>
      <div>
        <p>{{ myCondition(true) }}</p>
        <p>{{ myCondition(false) }}</p>
      </div>
    </template>
    

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

    **/*.svelte
    <script lang="ts">
    import { useIntlayer } from "svelte-intlayer";
    
    const content = useIntlayer("my_key");
    </script>
    
    <div>
      <p>{$content.myCondition(true)}</p>
      <p>{$content.myCondition(false)}</p>
    </div>
    

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

    **/*.tsx
    import type { FC } from "preact";
    import { useIntlayer } from "preact-intlayer";
    
    const ConditionalComponent: FC = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

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

    **/*.tsx
    import type { Component } from "solid-js";
    import { useIntlayer } from "solid-intlayer";
    
    const ConditionalComponent: Component = () => {
      const { myCondition } = useIntlayer("my_key");
    
      return (
        <div>
          <p>{myCondition(true)}</p>
          <p>{myCondition(false)}</p>
        </div>
      );
    };
    
    export default ConditionalComponent;
    

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

    app.component.ts
    import { Component } from "@angular/core";
    import { useIntlayer } from "angular-intlayer";
    
    @Component({
      selector: "app-conditional",
      template: `
        <div>
          <p>{{ content().myCondition(true) }}</p>
          <p>{{ content().myCondition(false) }}</p>
        </div>
      `,
    })
    export class ConditionalComponent {
      content = useIntlayer("my_key");
    }
    

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

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

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

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

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