getLocale
Server-side async function that detects and returns the current locale for the active request. Uses a multi-step detection strategy to determine the best locale.
Import
import { getLocale } from 'catmint/i18n'
Signature
function getLocale(): Promise<string>
Parameters
This function takes no parameters. It reads the locale from the current request context automatically.
Return Value
Returns a Promise<string> resolving to the detected locale (e.g. 'en', 'fr').
Detection priority
The locale is determined by checking the following sources in order:
- URL locale prefix -- e.g.
/fr/aboutresolves to'fr' - Cookie -- the
catmint-localecookie value - Accept-Language header -- parsed and matched against configured locales
- Default locale -- the
defaultLocalefrom your i18n config
If no request context is available (e.g. during static generation), the default locale from your config is returned, falling back to 'en'.
Examples
Server component
import { getLocale } from 'catmint/i18n'
export default async function Page() {
const locale = await getLocale()
return <h1>{locale === 'fr' ? 'Bienvenue' : 'Welcome'}</h1>
}
In a server function
import { getLocale } from 'catmint/i18n'
import { createServerFn } from 'catmint'
const getGreeting = createServerFn(async () => {
const locale = await getLocale()
return locale === 'es' ? 'Hola' : 'Hello'
})
In middleware
import { getLocale } from 'catmint/i18n'
export async function middleware(request: Request) {
const locale = await getLocale()
// Use locale for routing decisions
}
