Extension of Nuxt runtime app context

The following APIs are exposed both on NuxtApp.

$i18n

  • Type: 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)  }})

$getRouteBaseName()

$switchLocalePath()

$localePath()

$localeRoute()

$localeHead()

See more info about those in Extension of Vue section.

Extension of NuxtHooks

i18n:extend-messages Hook

  • Arguments:
    • additionalMessages (type: LocaleMessages<DefineLocaleMessage>[])
    • localeCodes (type: string[]) - locale codes, which is resolved with locales option

The additionalMessages array can be pushed locale messages paired with the locale.

Example:

import { defineNuxtModule } from '@nuxt/kit'export default defineNuxtModule({  async setup(options, nuxt) {    nuxt.hook('i18n:extend-messages', (additionalMessages, localeCodes) => {      additionalMessages.push({        en: {          'my-module-exemple': {            hello: 'Hello from external module'          }        },        fr: {          'my-module-exemple': {            hello: 'Bonjour depuis le module externe'          }        }      })    })  }}

See also Extending messages hook