Lzh on GitHub

Nuxt

其他与 Nuxt 相关的 i18n API。

Nuxt 运行时应用上下文的扩展

以下 API 均在 NuxtApp 上公开。

$i18n

  • 类型VueI18n | Composer

另请参阅 NuxtApp

$i18n 是 Vue I18n 的全局 Composer 或全局 VueI18n 实例。详情请参阅 此处

如果您在 @nuxtjs/i18n 配置中将 i18n.vueI18n.legacy 选项设置为 false,则 $i18n 是一个全局 Composer 实例。否则,它是一个全局 VueI18n 实例。

使用示例:

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.$i18n.onBeforeLanguageSwitch = (oldLocale, newLocale, isInitialSetup, nuxtApp) => {
    console.log('onBeforeLanguageSwitch', oldLocale, newLocale, isInitialSetup)
  }
})

$routeBaseName()

$switchLocalePath()

$localePath()

$localeRoute()

$localeHead()

有关这些内容的更多信息,请参阅 Vue 的扩展 部分。


NuxtHooks 的扩展

'i18n:registerModule' 钩子

  • 参数
    • registerModule(类型:({ langDir: string, locales: LocaleObject[] }) => void
my-module-example/module.ts
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 路径需要解析
        langDir: resolve('./lang'),
        locales: [
          {
            code: 'en',
            file: 'en.json',
          },
          {
            code: 'fr',
            file: 'fr.json',
          },
        ]
      })
    })
  }
})

另请参阅 扩展消息钩子