defineI18nConfig
The defineI18nConfig() composable is used to define a function which returns the vue-i18n configuration which is passed to the createI18n() options on the Nuxt I18n module.
The loader function needs to return a vue-i18n options object, or a Promise that resolves to one.
For more details on configuring vue-i18n, see the Vue I18n documentation.
Type
export function defineI18nConfig<Config extends I18nOptions>(
loader: () => Config | Promise<Config>
): () => Config | Promise<Config>
Parameters
loader
A function that is the vue-i18n options loading.
Usage
An example defining a simple vue-i18n options object:
export default defineI18nConfig(() => ({
legacy: false,
locale: 'en',
messages: {
en: {
welcome: 'Welcome'
},
fr: {
welcome: 'Bienvenue'
}
}
}))
Where the config runs
The config is loaded on the server as well as in the browser. The server loads it outside the Nuxt app, so it can only use APIs that exist in both places, such as $fetch() and useRuntimeConfig().
Nuxt app composables (useRequestURL(), useNuxtApp(), useCookie(), ...) are the exception: a config calling one is loaded inside the Nuxt app instead, on both server and client. That is decided at build time, by reading the calls the config makes itself. A composable reached through an imported helper is not visible, and fails when the server loads the config - call it in the config file to make it detectable.
export default defineI18nConfig(() => ({
legacy: false,
messages: {
en: {
// loaded in the Nuxt app, where the request is reachable
host: useRequestURL().host
}
}
}))
$t() used with a locale detector, and its messages are left out of the types generated by experimental.typedOptionsAndMessages. Keep messages needed there in a config that does not use these composables.