Composables
defineI18nLocale
The defineI18nLocale() composable is used to define a function to dynamically load locale messages used for lazy-loading translations.
The defineI18nLocale()
composable is used to define a function to dynamically load locale messages used for lazy-loading translations.
The loader function needs to return a Promise
that resolves a messages object.
Type
declare function defineI18nLocale<Messages = LocaleMessages<DefineLocaleMessage>, Locales = Locale>(
loader: (locale: Locales) => Messages | Promise<Messages>
): (locale: Locales) => Messages | Promise<Messages>
Parameters
loader
A function that is the dynamic locale messages loading, that has the following parameters:
locale
Type:Locale
A target locale that is passed from nuxt i18n module. That is passed when the locale is switched in the following cases:- when you switch the locale with
setLocale()
. - when the locale is switched with
<NuxtLink>
. for example, the route path resolved byuseSwitchLocalePath()
or$switchLocalePath()
.
- when you switch the locale with
Usage
An example of a loader function using a fetch request to load locale messages:
export default defineI18nLocale(locale => {
return $fetch(`https://your-company-product/api/${locale}`)
})
defineI18nConfig
The defineI18nConfig() composables is used to define a function which returns the vue-i18n configuration which is passed to the createI18n() options on the Nuxt I18n module.
defineI18nLocaleDetector
The defineI18nLocaleDetector() is composable used to define a function which detects the locale on the server-side, it's called per request on the server.