מחבר:
הפנו את עוזר ה-AI המועדף עליכם למסמך הזהChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Init doc"v9.5.519.9.2026
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתעריכת המסמך הזה
אם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודנוצר:2026-09-19עדכון אחרון:2026-09-19
העתקה
העתקת ה-Markdown של המסמך ללוח
useLocale Hook Documentation
The useLocale hook from astro-intlayer provides access to the current request locale, the configured default locale, and all available locales in Astro applications.
It behaves consistently across server-rendered .astro frontmatter and client-side <script> blocks.
Usage
In Component Frontmatter (Server-Rendered)
src/layouts/Layout.astro
העתקת קוד
העתקת הקוד ללוח
In Client <script> (Interactive)
src/components/LocaleSwitcher.astro
העתקת קוד
העתקת הקוד ללוח
Return Values
The hook returns an object of type UseLocaleResult:
הצגת כל תוכן הטבלה
פתיחת הטבלה בחלון כדי לראות את כל תוכן הנתונים בבירור
| Property | Type | Description |
|---|---|---|
locale | DeclaredLocales | The active locale. |
defaultLocale | DeclaredLocales | The default fallback locale configured in intlayer.config.ts. |
availableLocales | DeclaredLocales[] | Array of all supported locales configured for the project. |
setLocale | (locale: LocalesValues) => void | Function to update the locale. (Interactive in client <script>, warns during SSR). |
subscribe | (callback: () => void) => () => void | Subscribes to client-side locale changes. |
Server vs Client Behavior
- During SSR / Server Rendering: A request is rendered once with fixed parameters. Calling
setLocale()during a server render has no effect and emits a warning; locale switching should be performed on the client or by navigating to the target locale URL. - In Client Scripts:
setLocaleupdates the client store and updates persisted cookies or local storage according to your Intlayer configuration.
