I18n Other APIs related to Nuxt.
The following APIs are exposed both on NuxtApp
.
VueI18n
| Composer
See also NuxtApp
$i18n
is the global Composer
or global VueI18n
instance of Vue I18n. See about details here
If you set i18n.vueI18n.legacy
option to false
in your @nuxtjs/i18n
configuration, $i18n
is a global Composer
instance. Otherwise, it is a global VueI18n
instance.
Example use:
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.$i18n.onBeforeLanguageSwitch = (oldLocale, newLocale, isInitialSetup, nuxtApp) => {
console.log('onBeforeLanguageSwitch', oldLocale, newLocale, isInitialSetup)
}
})
See more info about those in Extension of Vue section.
i18n:registerModule
Hook({ langDir: string, locales: LocaleObject[] }) => void
)import { createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
async setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
nuxt.hook('i18n:registerModule', register => {
register({
// langDir path needs to be resolved
langDir: resolve('./lang'),
locales: [
{
code: 'en',
file: 'en.json',
},
{
code: 'fr',
file: 'fr.json',
},
]
})
})
}
})
See also Extending messages hook