שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "עדכון לארכיטקטורת Next.js >= 9.4.0"v9.4.022.8.2026
- "כתיבה ראשונית של תיעוד ההוק `useI18n`"v6.0.029.6.2025
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
שילוב ב-React: תיעוד ההוק useI18n
סעיף זה מספק הנחיות מפורטות כיצד להשתמש בהוק useI18n ביישומי React, ומאפשר לוקליזציה יעילה של תוכן.
ייבוא useI18n ב-React
ניתן לייבא את ההוק useI18n ולשלבו ביישומי React בהתאם לקונטקסט באופן הבא:
רכיבי לקוח (Client Components):
typescriptהעתקת קודהעתקת הקוד ללוח
רכיבי שרת (Server Components):
פרמטרים
הוק זה מקבל שני פרמטרים:
namespace: מרחב שמות של מילון לתחימת מפתחות התרגום.locale(אופציונלי): השפה הרצויה. אם לא צוינה, שפת הקונטקסט תשמש כברירת מחדל.
מילון
כל מפתחות המילון חייבים להיות מוצהרים בקובצי הצהרת תוכן כדי לשפר את בטיחות הטיפוסים ולמנוע שגיאות. הוראות הגדרה ניתן למצוא כאן.
דוגמאות שימוש ב-React
דוגמאות לשימוש בהוק useI18n בתוך רכיבי React:
העתקת הקוד ללוח
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 />
</>
);
};
העתקת הקוד ללוח
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>
</>
);
};
העתקת הקוד ללוח
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>
);
};
העתקת הקוד ללוח
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)
בעת התאמת מאפיינים לשפה מסוימת, גש לערכי התרגום בהתאם:
העתקת הקוד ללוח
משאבים נוספים
- עורך ויזואלי של Intlayer: לחוויית ניהול תוכן אינטואיטיבית יותר, עיין בתיעוד העורך הוויזואלי כאן.
סעיף זה מכסה באופן ספציפי את השילוב של ההוק useI18n ביישומי React, תוך פישוט תהליך הלוקליזציה והבטחת עקביות התוכן בין שפות שונות.
