API Reference
setRequestLocale
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.
runWithLocale
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.
getLocale
getLocale(): string
Returns the current locale string set by setRequestLocale. Throws if called before setting the locale.
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 parser-sanitized t.markup(key, tags | {curlyOpen} values?, tags {curlyClose}) for HTML interpolation and t.raw(key) for accessing raw values without coercion.
getDefaultLocale
getDefaultLocale(): string
Returns the defaultLocale configured via the integration options or createIntlMiddleware. Defaults to "en" if not set.
getLocales
getLocales(): string[]
Returns the array of locales configured via the integration options or createIntlMiddleware. Returns an empty array if not set.
isValidLocale
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.
getMessages
getMessages(): Record<string, unknown>
Returns the full messages object for the current request. Throws if called before setRequestLocale.
createIntlMiddleware
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.
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, rejects invalid locale segments, and substitutes [param] placeholders with the provided params. Import from astro-intl/routing.
switchLocalePath
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.
RequestConfigType returned by your getRequestConfig function. Contains locale: string and messages: Record.
IntlConfigConfiguration type for the integration options. Contains defaultLocale: string, locales: string[], routes?: RoutesMap, and fallbackRoutes?: FallbackRouteInfo[].
PrimitiveUnion type for interpolation values: string | number | boolean | null | undefined. Used as the value type in the values object passed to t() and t.markup().
DotPaths<T>Utility type that generates all valid dot-notation paths for a given messages object. Used internally to provide autocomplete for translation keys.
RoutesMapType for the routes configuration object. Maps route keys to an object of locale → URL template pairs. Example: { about: { en: "/about", es: "/sobre-nosotros" } }.
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.
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.
renderRichText
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.
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.
FallbackRouteInfoType 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).
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].
MessagesDirConfigConfiguration 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.
AutoRedirect Component
<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.
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.