API Reference

functionsetRequestLocale
typescript
setRequestLocale(url: URL, getConfig?: GetRequestConfigFn): Promise<boolean>

Extracts and validates the locale from the first URL pathname segment, then calls your getConfig function to load messages. Must be awaited before any other call. For SSR, prefer createIntlMiddleware() or runWithLocale() so the complete render stays request-isolated.

functionrunWithLocale
typescript
runWithLocale<R>(url: URL, fn: () => R | Promise<R>, getConfig?: GetRequestConfigFn): Promise<R>

Runs a function inside an isolated per-request context using Node.js AsyncLocalStorage. SSR runtimes without safe request-local storage fail explicitly instead of sharing global state.

functiongetLocale
typescript
getLocale(): string

Returns the current locale string set by setRequestLocale. Throws if called before setting the locale.

functiongetTranslations
typescript
getTranslations<T>(namespace?: string)

Returns a typed t(key, values?) function scoped to the given namespace. The optional values object replaces {curlyOpen}varName{curlyClose} placeholders. Also exposes parser-sanitized t.markup(key, tags | {curlyOpen} values?, tags {curlyClose}) for HTML interpolation and t.raw(key) for accessing raw values without coercion.

functiondefineRequestConfig
typescript
defineRequestConfig(fn: GetRequestConfigFn): GetRequestConfigFn

Registers and returns a message loader. Import the module that declares it before initializing the language. See Custom message loading for complete files.

functiongetLocales
typescript
getLocales(): string[]

Returns the array of locales configured via the integration options or createIntlMiddleware. Returns an empty array if not set.

functionisValidLocale
typescript
isValidLocale(locale: string): boolean

Checks that a locale has valid BCP-47-style syntax and, when a locales list is configured, that it is included in that list.

functiongetMessages
typescript
getMessages(): Record<string, unknown>

Returns the full messages object for the current request. Throws if called before setRequestLocale.

functioncreateIntlMiddleware
typescript
createIntlMiddleware(options: { locales: string[]; defaultLocale?: string; routes?: RoutesMap })

Creates Astro middleware that validates and loads the request locale, then keeps the complete render or rewrite inside a Node.js AsyncLocalStorage context. It also sets locales, defaultLocale, and routes in the intl store. Import from astro-intl/middleware.

functionpath
typescript
path(routeKey: string, options?: { locale?: string; params?: Record<string, string>; encode?: boolean }): string

Generates a localized URL for a named route. Uses the current locale if none is specified, rejects invalid locale segments, and substitutes [param] placeholders with the provided params. Import from astro-intl/routing.

functionswitchLocalePath
typescript
switchLocalePath(currentPath: string | URL, nextLocale: string): string

Converts the current URL to its equivalent in another validated locale. Matches the path against route templates, extracts dynamic params, and rebuilds the URL using the target locale's template. Preserves query strings and hashes. Import from astro-intl/routing.

typeRequestConfig

Type returned by your getRequestConfig function. Contains locale: string and messages: Record.

typeIntlConfig

Configuration type for the integration options. Contains defaultLocale: string, locales: string[], routes?: RoutesMap, and fallbackRoutes?: FallbackRouteInfo[].

typePrimitive

Union type for interpolation values: string | number | boolean | null | undefined. Used as the value type in the values object passed to t() and t.markup().

typeDotPaths<T>

Utility type that generates all valid dot-notation paths for a given messages object. Used internally to provide autocomplete for translation keys.

typeRoutesMap

Type for the routes configuration object. Maps route keys to an object of locale → URL template pairs. Example: { about: { en: "/about", es: "/sobre-nosotros" } }.

functioncreateGetTranslations (React)
typescript
createGetTranslations<UI, DefaultLocale>(ui: UI, defaultLocale: DefaultLocale)

Use an explicit catalog in React islands in your Astro project or in tests. Returns getTranslations(lang, namespace) with t.rich() support returning ReactNode[]. Import from astro-intl/react.

React in Astro
functioncreateGetTranslations (Preact)
typescript
createGetTranslations(ui, defaultLocale)(lang, namespace)

Import this function from astro-intl/preact to translate with an explicit catalog in Preact 10 and 11. t.rich() returns renderable Preact children.

Preact in Astro
functioncreateGetTranslations (SolidJS)
typescript
createGetTranslations(ui, defaultLocale)(lang, namespace)

Import this function from astro-intl/solid to test a SolidJS catalog with an explicit locale. t.rich() returns JSX.Element[] you can render in a component.

SolidJS in Astro
functioncreateGetTranslations (Vue)
typescript
createGetTranslations(ui, defaultLocale)(lang, namespace)

Import this function from astro-intl/vue to translate with an explicit catalog in Vue 3. t.rich() returns VNodeChild[] for Vue render functions.

Vue in Astro
functioncreateGetTranslations (Svelte)
typescript
createGetTranslations<UI, DefaultLocale>(ui: UI, defaultLocale: DefaultLocale)

Use an explicit catalog in Svelte islands in your Astro project or in tests. Returns getTranslations(lang, namespace) with t.rich() support returning RichSegment[]. Import from astro-intl/svelte.

Svelte in Astro
functionrenderRichText
typescript
renderRichText(segments: RichSegment[], options?: { tags?: Record<string, string>; components?: Record<string, (chunks: string) => string> }): string

Converts an array of RichSegment[] into a sanitized HTML string. Text and chunks are escaped, unsafe native tag mappings are rejected, and component callbacks are treated as trusted application code before final sanitization. Import from astro-intl/svelte.

Svelte in Astro
functiongetFallbackRoutes
typescript
getFallbackRoutes(): FallbackRouteInfo[]

Returns the i18n fallback routes collected from Astro 6.1's astro:routes:resolved hook. Returns an empty array on Astro < 6.1 or when no fallback routes are configured. Each entry contains the route pattern, optional pathname, and the locale.

typeFallbackRouteInfo

Type for fallback route entries. Contains pattern: string (the route pattern), pathname?: string (the static pathname if available), and locale: string (the locale this fallback serves).

typet.raw()

Returns the raw translation value without string coercion. Use this to access arrays, objects, and numbers in their native JavaScript type instead of getting [object Object].

typeMessagesDirConfig

Exported type shaped as {curlyOpen} dir: string {curlyClose}. The integration option messagesDir accepts a string directly, not this object. See Configuration options for its limitations.

functionAutoRedirect Component
typescript
<AutoRedirect locales={string[]} defaultLocale={string} />

Astro component that validates its locale props, detects the user's browser language, and redirects to the appropriate encoded localized route. defaultLocale must be included in locales. Import from astro-intl/components.

typeAuto-detect Locale (Static Mode)

Only in the browser, getLocale() can read the language from window.location.pathname when there is no context. This does not load messages or replace initialization during a static build or server rendering.