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

    שילוב ב-React: תיעוד ההוק useI18n

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

    ייבוא useI18n ב-React

    ניתן לייבא את ההוק useI18n ולשלבו ביישומי React בהתאם לקונטקסט באופן הבא:

    • רכיבי לקוח (Client Components):

      typescript
      import { useI18n } from "react-intlayer"; // לשימוש ברכיבי React צד-לקוח
      
    • רכיבי שרת (Server Components):

    פרמטרים

    הוק זה מקבל שני פרמטרים:

    1. namespace: מרחב שמות של מילון לתחימת מפתחות התרגום.
    2. locale (אופציונלי): השפה הרצויה. אם לא צוינה, שפת הקונטקסט תשמש כברירת מחדל.

    מילון

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

    דוגמאות שימוש ב-React

    דוגמאות לשימוש בהוק useI18n בתוך רכיבי React:

    src/App.tsx
    import type { FC } from "react";
    import { ClientComponentExample, ServerComponentExample } from "@components";
    import { IntlayerProvider } from "react-intlayer";
    import { useI18n } from "react-intlayer/server";
    import { Locales } from "intlayer";
    
    const App: FC<{ locale: Locales }> = ({ locale }) => {
      const t = useI18n("home-page", locale);
    
      return (
        <>
          <p>{t("introduction")}</p>
          <IntlayerProvider locale={locale}>
            <ClientComponentExample />
          </IntlayerProvider>
          <ServerComponentExample />
        </>
      );
    };
    
    src/App.tsx
    import type { FC } from "react";
    import { ClientComponentExample, ServerComponentExample } from "@components";
    import { IntlayerProvider } from "react-intlayer";
    import { useI18n, IntlayerServerProvider } from "react-intlayer/server";
    import { Locales } from "intlayer";
    
    const App: FC<{ locale: Locales }> = ({ locale }) => {
      const t = useI18n("home-page", locale);
    
      return (
        <>
          <p>{t("introduction")}</p>
          <IntlayerProvider locale={locale}>
            <ClientComponentExample />
          </IntlayerProvider>
          <IntlayerServerProvider locale={locale}>
            <ServerComponentExample />
          </IntlayerServerProvider>
        </>
      );
    };
    
    src/components/ComponentExample.tsx
    import type { FC } from "react";
    import { useI18n } from "react-intlayer";
    
    const ComponentExample: FC = () => {
      const t = useI18n("component-example");
    
      return (
        <div>
          <h1>{t("title")}</h1> {/* הצגת הכותרת */}
          <p>{t("description")}</p> {/* הצגת התיאור */}
        </div>
      );
    };
    
    src/components/ServerComponentExample.tsx
    import { useI18n } from "react-intlayer/server";
    
    const ServerComponentExample = () => {
      const t = useI18n("server-component");
    
      return (
        <div>
          <h1>{t("title")}</h1> {/* הצגת הכותרת */}
          <p>{t("description")}</p> {/* הצגת התיאור */}
        </div>
      );
    };
    

    טיפול במאפיינים (Attributes)

    בעת התאמת מאפיינים לשפה מסוימת, גש לערכי התרגום בהתאם:

    html
    <!-- עבור מאפייני נגישות (כגון aria-label), השתמש ב-.value מאחר שנדרשות מחרוזות טהורות -->
    <button aria-label={t("button.ariaLabel").value}>{t("button.text")}</button>
    

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

    • עורך ויזואלי של Intlayer: לחוויית ניהול תוכן אינטואיטיבית יותר, עיין בתיעוד העורך הוויזואלי כאן.

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