מחבר:
    נוצר:2026-06-12עדכון אחרון:2026-11-03

    וריאציות (Variants)

    וריאציה (variant) היא קבוצה של קובצי תוכן החולקים את אותו מפתח מילון (key), אך כל אחד מהם נושא ערך variant שונה. Intlayer מגיש את הקובץ המתאים בהתבסס על הבורר (selector) המועבר ל-useIntlayer.

    ערך ה-variant יכול לקבל שתי צורות:

    • מחרוזת: חלופה בודדת בעלת שם (מבחני A/B, באנרים עונתיים, feature flags).
    • אובייקט: מבחין מובנה הממוען באמצעות קבוצת שדות (רשומות CMS, ניסוח מותאם משתמש, כל תוכן הממופתח באמצעות מזהה אטום). האובייקט כולו מהווה את הזהות: על הבורר לספק אובייקט זהה כדי לפענח את הרשומה.
    צורת האובייקט מחליפה את שדה ה-meta הישן. בכל מקום שבו כתבת בעבר meta: { id, … }, כתוב כעת variant: { id, … }, ובחר אותו באמצעות { variant: { id, … } }.

    וריאציות בעלות שם (מחרוזת)

    כל קובץ מייצג חלופה בעלת שם אחת. השמטת variant (או הגדרתו ל-"default") מסמנת אותו כ-fallback (ברירת המחדל).

    hero-banner.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "hero-banner",
      variant: "default",
      content: {
        headline: t({
          he: "בנו מהר יותר עם Intlayer",
          en: "Build faster with Intlayer",
          fr: "Développez plus vite avec Intlayer",
        }),
        cta: t({
          he: "להתחיל",
          en: "Get started",
          fr: "Commencer",
        }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    
    hero-banner.black-friday.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "hero-banner",
      variant: "black_friday",
      content: {
        headline: t({
          he: "50% הנחה, היום בלבד",
          en: "50 % off, today only",
          fr: "−50 %, aujourd'hui seulement",
        }),
        cta: t({
          he: "קנה עכשיו",
          en: "Shop now",
          fr: "Acheter maintenant",
        }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    

    וריאציות חלקיות

    וריאציה מצהירה רק על המפתחות שהיא דורסת; השאר עוברים בירושה מרשומת ברירת המחדל.

    hero-banner.summer.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "hero-banner",
      variant: "summer",
      content: {
        headline: t({
          he: "בנו מהר יותר כל הקיץ",
          en: "Build faster all summer",
          fr: "Développez plus vite tout l'été",
        }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    
    tsx
    useIntlayer("hero-banner", { variant: "summer" });
    // → { headline: "בנו מהר יותר כל הקיץ", cta: "להתחיל" }, `cta` מורש
    
    useIntlayer("hero-banner", { variant: "never-declared" });
    // → רשומת ברירת המחדל
    

    לכן עליך להוסיף קובץ variant רק במקומות שבהם הניסוח אכן שונה. מפתח יתפענח ל-null רק כאשר הוא מצהיר על variants אך אין לו רשומת ברירת מחדל.

    צריכת וריאציות בעלות שם

    וריאציית ברירת מחדל

    Hero.tsx
    import { useIntlayer } from "react-intlayer";
    
    export const Hero = () => {
    const { headline, cta } = useIntlayer("hero-banner");
    // → וריאציית ברירת מחדל
    
    return (
      <section>
        <h1>{headline}</h1>
        <a>{cta}</a>
      </section>
    );
    };
    
    Hero.tsx
    import { useIntlayer } from "next-intlayer";
    
    export const Hero = () => {
    const { headline, cta } = useIntlayer("hero-banner");
    // → וריאציית ברירת מחדל
    
    return (
      <section>
        <h1>{headline}</h1>
        <a>{cta}</a>
      </section>
    );
    };
    
    Hero.vue
    <script setup>
    import { useIntlayer } from "vue-intlayer";
    const { headline, cta } = useIntlayer("hero-banner");
    </script>
    
    <template>
    <section>
      <h1>{{ headline }}</h1>
      <a>{{ cta }}</a>
    </section>
    </template>
    
    Hero.svelte
    <script lang="ts">
    import { useIntlayer } from "svelte-intlayer";
    const content = useIntlayer("hero-banner");
    </script>
    
    <section>
    <h1>{$content.headline}</h1>
    <a>{$content.cta}</a>
    </section>
    
    Hero.tsx
    import { useIntlayer } from "preact-intlayer";
    
    export const Hero = () => {
    const { headline, cta } = useIntlayer("hero-banner");
    // → וריאציית ברירת מחדל
    
    return (
      <section>
        <h1>{headline}</h1>
        <a>{cta}</a>
      </section>
    );
    };
    
    Hero.tsx
    import { useIntlayer } from "solid-intlayer";
    
    export const Hero = () => {
    const content = useIntlayer("hero-banner");
    // → וריאציית ברירת מחדל
    
    return (
      <section>
        <h1>{content().headline}</h1>
        <a>{content().cta}</a>
      </section>
    );
    };
    
    hero.component.ts
    import { Component } from "@angular/core";
    import { useIntlayer } from "angular-intlayer";
    
    @Component({
    selector: "app-hero",
    template: `
      <section>
        <h1>{{ content().headline }}</h1>
        <a>{{ content().cta }}</a>
      </section>
    `,
    })
    export class HeroComponent {
    content = useIntlayer("hero-banner");
    }
    
    hero.js
    import { useIntlayer } from "vanilla-intlayer";
    
    const { headline, cta } = useIntlayer("hero-banner");
    
    document.body.innerHTML = `
    <section>
      <h1>${headline}</h1>
      <a>${cta}</a>
    </section>
    `;
    

    וריאציה בעלת שם

    tsx
    const { headline, cta } = useIntlayer("hero-banner", {
      variant: "black_friday",
    });
    

    וריאציה בעלת שם עם שפה מפורשת

    tsx
    const content = useIntlayer("hero-banner", {
      variant: "black_friday",
      locale: "fr",
    });
    

    וריאציות אובייקט (מובנות)

    וריאציית אובייקט ממקמת תוכן לפי קבוצה שרירותית של זוגות מפתח-ערך המוצהרים בשדה variant, מה שמאפשר למדל רשומות CMS, ניסוח מותאם אישית למשתמש, או כל תוכן שהמפתח שלו הוא מזהה אטום. האובייקט כולו מהווה את הזהות: הבורר חייב לספק אובייקט שווה ערך כדי שהרשומה תפוענח.

    product.abc.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product",
      variant: { id: "prod_abc", userId: "user_123" },
      content: {
        name: t({
          he: "וידג'ט פרו",
          en: "Widget Pro",
          fr: "Widget Pro",
        }),
        description: t({
          he: "הווידג'ט הטוב ביותר.",
          en: "The best widget.",
          fr: "Le meilleur widget.",
        }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    
    product.abcd.content.ts
    import { t, type Dictionary } from "intlayer";
    
    const dictionary = {
      key: "product",
      variant: { id: "prod_abcd", userId: "user_123" },
      content: {
        name: t({
          he: "וידג'ט לייט",
          en: "Widget Lite",
          fr: "Widget Lite",
        }),
        description: t({
          he: "אפשרות קלה יותר.",
          en: "A lighter option.",
          fr: "Une option plus légère.",
        }),
      },
    } satisfies Dictionary;
    
    export default dictionary;
    

    צריכת וריאציות אובייקט

    העבר את האובייקט התואם ל-variant. כל שדה שהוצהר במילון חייב להיות מסופק ולהיות שווה; אחרת התוצאה היא null. סדר השדות אינו משנה.

    Product.tsx
    import { useIntlayer } from "react-intlayer";
    
    export const Product = ({
    productId,
    userId,
    }: {
    productId: string;
    userId: string;
    }) => {
    const content = useIntlayer("product", {
      variant: { id: productId, userId },
    });
    
    if (!content) return null;
    
    return <p>{content.description}</p>;
    };
    
    Product.tsx
    import { useIntlayer } from "next-intlayer";
    
    export const Product = ({
    productId,
    userId,
    }: {
    productId: string;
    userId: string;
    }) => {
    const content = useIntlayer("product", {
      variant: { id: productId, userId },
    });
    
    if (!content) return null;
    
    return <p>{content.description}</p>;
    };
    
    Product.vue
    <script setup>
    import { useIntlayer } from "vue-intlayer";
    
    const props = defineProps({
    productId: String,
    userId: String,
    });
    
    const content = useIntlayer("product", {
    variant: { id: props.productId, userId: props.userId },
    });
    </script>
    
    <template>
    <p v-if="content">{{ content.description }}</p>
    </template>
    
    Product.svelte
    <script lang="ts">
    import { useIntlayer } from "svelte-intlayer";
    
    export let productId: string;
    export let userId: string;
    
    const content = useIntlayer("product", {
    variant: { id: productId, userId },
    });
    </script>
    
    {#if $content}
    <p>{$content.description}</p>
    {/if}
    
    Product.tsx
    import { useIntlayer } from "preact-intlayer";
    
    export const Product = ({
    productId,
    userId,
    }: {
    productId: string;
    userId: string;
    }) => {
    const content = useIntlayer("product", {
      variant: { id: productId, userId },
    });
    
    if (!content) return null;
    
    return <p>{content.description}</p>;
    };
    
    Product.tsx
    import { useIntlayer } from "solid-intlayer";
    
    export const Product = (props: {
    productId: string;
    userId: string;
    }) => {
    const content = useIntlayer("product", {
      variant: { id: props.productId, userId: props.userId },
    });
    
    return (
      <>
        {content() && <p>{content().description}</p>}
      </>
    );
    };
    
    product.component.ts
    import { Component, Input, OnInit } from "@angular/core";
    import { useIntlayer } from "angular-intlayer";
    
    @Component({
    selector: "app-product",
    template: `
      @if (content()) {
        <p>{{ content().description }}</p>
      }
    `,
    })
    export class ProductComponent implements OnInit {
    @Input() productId!: string;
    @Input() userId!: string;
    
    content: any;
    
    ngOnInit() {
      this.content = useIntlayer("product", {
        variant: { id: this.productId, userId: this.userId },
      });
    }
    }
    
    product.js
    import { useIntlayer } from "vanilla-intlayer";
    
    const content = useIntlayer("product", {
    variant: { id: "prod_abcd", userId: "user_123" },
    });
    
    if (content) {
    document.body.innerHTML = `<p>${content.description}</p>`;
    }
    

    עם שפה מפורשת

    tsx
    const content = useIntlayer("product", {
      variant: { id: "prod_abc", userId: "user_123" },
      locale: "fr",
    });
    

    שדה חסר, ללא התאמה

    ts
    // מחזיר null: `userId` חסר, לכן האובייקט אינו תואם לווריאציה שהוצהרה
    const content = useIntlayer("product", { variant: { id: "prod_abc" } });
    

    וריאציה סביבתית (Ambient variant)

    ממדי וריאציה מסוימים קבועים עבור סשן שלם, ה-tenant, סוג בית הספר, או רמת התוכנית (plan tier). הם מפוענחים פעם אחת, ואף רכיב אינו צריך להעביר אותם ידנית.

    אל תעטוף את useIntlayer ב-hook משלך כדי להזריק אותם. אופטימיזציית זמן הבנייה משכתבת אך ורק קריאות מילוליות של useIntlayer("key") המיובאות ישירות מחבילת הפריימוורק, כך ששום דבר מאחורי עטיפה אינו נכלל ב-bundle בצורה ממוטבת.

    הצהר על ה-variant פעם אחת ב-provider במקום זאת, בדיוק כמו locale:

    App.tsx
    import { IntlayerProvider } from "react-intlayer";
    
    export const App = ({ locale, schoolType }) => (
    <IntlayerProvider locale={locale} variant={schoolType}>
      <Hero />
    </IntlayerProvider>
    );
    
    layout.tsx
    import { IntlayerProvider } from "next-intlayer/server";
    
    export default async function Layout({ children, params }) {
    const { locale } = await params;
    const schoolType = await getSchoolType();
    
    return (
    <IntlayerProvider locale={locale} variant={schoolType}>
    {children}
    </IntlayerProvider>
    );
    }
    
    layout.tsx
    import { IntlayerServerProvider } from "next-intlayer/server";
    import { IntlayerClientProvider } from "next-intlayer";
    
    export default async function Layout({ children, params }) {
    const { locale } = await params;
    const schoolType = await getSchoolType();
    
    return (
    <IntlayerServerProvider locale={locale} variant={schoolType}>
    <IntlayerClientProvider locale={locale} variant={schoolType}>
    {children}
    </IntlayerClientProvider>
    </IntlayerServerProvider>
    );
    }
    
    main.ts
    import { createApp } from "vue";
    import { installIntlayer } from "vue-intlayer";
    import App from "./App.vue";
    
    const app = createApp(App);
    
    installIntlayer(app, { locale: "en", variant: schoolType });
    
    app.mount("#app");
    
    +layout.svelte
    <script lang="ts">
    import { setupIntlayer } from "svelte-intlayer";
    
    export let schoolType: string;
    
    setupIntlayer("en", schoolType);
    </script>
    
    <slot />
    
    App.tsx
    import { IntlayerProvider } from "preact-intlayer";
    
    export const App = ({ locale, schoolType }) => (
    <IntlayerProvider locale={locale} variant={schoolType}>
      <Hero />
    </IntlayerProvider>
    );
    
    App.tsx
    import { IntlayerProvider } from "solid-intlayer";
    
    export const App = (props) => (
    <IntlayerProvider locale={props.locale} variant={props.schoolType}>
      <Hero />
    </IntlayerProvider>
    );
    
    app.config.ts
    import { ApplicationConfig } from "@angular/core";
    import { provideIntlayer } from "angular-intlayer";
    
    export const appConfig: ApplicationConfig = {
    providers: [provideIntlayer("en", true, schoolType)],
    };
    
    main.js
    import { installIntlayer } from "vanilla-intlayer";
    
    installIntlayer({ locale: "en", variant: schoolType });
    

    כל קריאת מילון מתחת ל-provider מתפענחת כעת מול אותה וריאציה, ובורר באתר הקריאה (call-site) תמיד מנצח:

    tsx
    useIntlayer("hero-banner");
    // → וריאציית ה-provider
    
    useIntlayer("hero-banner", { variant: "summer" });
    // → "summer", מחליף את וריאציית ה-provider (אינו מרחיב אותה)
    

    צורות שימוש (Forms)

    ה-prop‏ variant מקבל שלוש צורות:

    צורהמשמעות
    variant="school1"וריאציה אחת בעלת שם עבור כל מפתח
    variant={["school1", "default"]}שרשרת העדפות סדורה
    variant={{ "hero-banner": "school1", default: "base" }}וריאציה אחת לכל מפתח מילון

    שרשרת העדפות

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

    tsx
    <IntlayerProvider variant={["school1", "school2"]} />
    // `hero-banner` אינו מצהיר על רשומת `school1` אך מצהיר על `school2` → "school2"
    // מפתח שאינו מצהיר על אף אחת מהן                                  → רשומת ברירת המחדל
    

    לפיכך, ["black_friday", "summer"] נקרא כ"בלאק פריידי אם למפתח זה יש כזה, אחרת קיץ, אחרת ברירת מחדל". שרשראות מתקבלות גם באתר הקריאה:

    tsx
    useIntlayer("hero-banner", { variant: ["black_friday", "summer"] });
    
    שים לב שזוהי תמונת ראי של המערך המתקבל על ידי שדה ה-variant בקובץ תוכן: שם מערך מצהיר על רשומה אחת לכל אלמנט, כאן הוא צורך אותן בסדר עדיפות.

    מפה לפי מפתח (Per-key map)

    פנה לכל מפתח מילון בנפרד. רשומת default השמורה מכסה כל מפתח שאינו מצוין ברשימה:

    tsx
    <IntlayerProvider
      variant={{
        "hero-banner": "school1",
        product: ["school1", "default"],
        default: "base",
      }}
    />
    
    ב-provider, אובייקט רגיל נקרא תמיד כמפה לפי מפתח, ולעולם לא כ-object variant (שכן השניים זהים מבנית). כדי להגדיר object variant באופן גלובלי, קנן אותו תחת רשומה: variant={{ default: { id: "prod_abc" } }}.

    מכיוון שמפתחות המפה נבדקים מול מפתחות המילון שהצהרת עליהם, שגיאת כתיב (או object variant שנכתב ישירות, כגון variant={{ id: "prod_abc" }}) מהווה שגיאת זמן הידור (compile-time error).

    מצב טעינה (Loading mode)

    וריאציות אובייקט נטענות לעתים קרובות באופן עצל (lazy). הגדר importMode במילון כדי לשלוט בכך:

    ts
    const dictionary = {
      key: "product",
      importMode: "fetch", // או "dynamic"
      variant: { id: "prod_abc", userId: "user_123" },
      content: {/* … */},
    } satisfies Dictionary;
    
    export default dictionary;
    

    עיין באופטימיזציית bundle לפרטים על מצבי static, dynamic ו-fetch.

    תרחישי שימוש נפוצים

    • מבחני ניסוח A/B המונעים על ידי מפתח ניסוי
    • באנרים עונתיים או פרסומיים
    • הודעות מבוססות feature flags
    • קמפיינים שיווקיים מותאמי שפה
    • ניסוח שיווקי לכל מוצר המנוהל ב-CMS
    • תוכן ספציפי למשתמש או לחשבון
    • כל תוכן הממופתח באמצעות מזהה אטום בזמן ריצה