Creation:2026-09-19Last update:2026-09-19
Reference this doc to your favorite AI assistantChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Init doc"v9.5.59/19/2026
Edit this doc
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy
Copy doc Markdown to clipboard
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
Copy code
Copy the code to the clipboard
In Client <script> (Interactive)
src/components/LocaleSwitcher.astro
Copy code
Copy the code to the clipboard
Return Values
The hook returns an object of type UseLocaleResult:
Show all table content
Open the table in a modal to view all data content clearly
| 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.
