API Reference

function setRequestLocale

                  
                    setRequestLocale(url: URL, getConfig: Function): Promise<void>
                  
                

Extracts the locale from the URL pathname (first segment) and calls your getConfig function to load messages. Must be awaited before any other call.

function runWithLocale

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

Runs a function inside an isolated per-request context. Uses AsyncLocalStorage on Node.js for concurrency-safe SSR. Falls back to a global variable on runtimes without it (Cloudflare Workers, Deno).

function getLocale

                  
                    getLocale(): string
                  
                

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

function getTranslations

                  
                    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 t.markup(key, tags | {curlyOpen} values?, tags {curlyClose}) for HTML interpolation and t.raw(key) for accessing raw values without coercion.

function getTranslationsReact Deprecated

                  
                    getTranslationsReact<T>(namespace?: string)
                  
                

Same as getTranslations but returns a function with t.rich(key, tags) for React node interpolation.

function getDefaultLocale

                  
                    getDefaultLocale(): string
                  
                

Returns the defaultLocale configured via the integration options or createIntlMiddleware. Defaults to "en" if not set.

function getLocales

                  
                    getLocales(): string[]
                  
                

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

function isValidLocale

                  
                    isValidLocale(locale: string): boolean
                  
                

Checks whether a locale string is in the configured locales list. Returns true for any string if no locales have been configured.

function getMessages

                  
                    getMessages(): Record<string, unknown>
                  
                

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

function createIntlMiddleware

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

Creates an Astro middleware that automatically calls setRequestLocale on every request. Also sets locales, defaultLocale, and routes in the intl store. When routes are provided, the middleware rewrites translated URLs to their canonical filesystem paths. Import from astro-intl/middleware.

function path

                  
                    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. Substitutes [param] placeholders with the provided params. Import from astro-intl/routing.

function switchLocalePath

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

Converts the current URL to its equivalent in another 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.

type RequestConfig

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

type IntlConfig

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

type Primitive

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().

type DotPaths<T>

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

type RoutesMap

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

function createGetTranslations (React)

                  
                    createGetTranslations<UI, DefaultLocale>(ui: UI, defaultLocale: DefaultLocale)
                  
                

Creates a standalone translation function for React without relying on the global store. Returns getTranslations(lang, namespace) with t.rich() support returning ReactNode[]. Import from astro-intl/react.

function createGetTranslations (Svelte)

                  
                    createGetTranslations<UI, DefaultLocale>(ui: UI, defaultLocale: DefaultLocale)
                  
                

Creates a standalone translation function for Svelte without relying on the global store. Returns getTranslations(lang, namespace) with t.rich() support returning RichSegment[]. Import from astro-intl/svelte.

function renderRichText

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

Converts an array of RichSegment[] into an HTML string. Use tags for native HTML elements (e.g. { bold: "strong" }) or components for custom rendering functions. Import from astro-intl/svelte.

function getFallbackRoutes

                  
                    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.

type FallbackRouteInfo

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).

type t.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].

type MessagesDirConfig

Configuration type for the messagesDir option. Set to a directory path (e.g., "./src/i18n/messages") and the integration will automatically load {locale}.json files with the correct import attributes.

function AutoRedirect Component

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

Astro component that detects the user's browser language and redirects to the appropriate localized route. Use this in your root page instead of Astro.redirect() to avoid the blank page issue in static mode. Import from astro-intl/components.

type Auto-detect Locale (Static Mode)

When running in static mode without explicit setRequestLocale() calls, getLocale() automatically detects the locale from window.location.pathname. Falls back to defaultLocale if no valid locale is found. Client-side only.