Nuxt i18n 模块 提供了运行时钩子,以根据应用程序的语言执行特定任务。
'i18n:beforeLocaleSwitch'
在应用程序的语言环境切换之前调用。可以通过返回新的语言环境代码来覆盖新的语言环境。
参数:
oldLocale
string
newLocale
string
initialSetup
string
true
。这是一个特殊情况,因为语言环境在技术上尚未设置,因此我们是从没有语言环境切换到语言环境。context
NuxtApp
返回: string | null
'i18n:localeSwitched'
在应用程序的语言环境切换后立即调用。
参数:
oldLocale
string
newLocale
string
典型的用法是在插件中定义这些回调,在这里您可以访问应用程序的上下文(如果您需要在语言更改时更改 Axios 的配置,例如,这非常有用)。
export default defineNuxtPlugin(nuxtApp => {
// 在设置新语言环境之前调用
nuxtApp.hook('i18n:beforeLocaleSwitch', ({ oldLocale, newLocale, initialSetup, context }) => {
console.log('onBeforeLanguageSwitch', oldLocale, newLocale, initialSetup)
})
// 在设置新的语言环境之后调用
nuxtApp.hook('i18n:localeSwitched', ({ oldLocale, newLocale }) => {
console.log('onLanguageSwitched', oldLocale, newLocale)
})
})