安装国际化语言包,注意本教程适用版本是nuxt2
"@nuxtjs/i18n": "^7.3.1",
在Nuxt.confug中添加配置
modules: [
[
'@nuxtjs/i18n',
{
locales: ['ru', 'en', 'jp'],
defaultLocale: env[process.env.NODE_ENV]?.language || 'ru',
vueI18n: {
fallbackLocale: 'en',
messages: {
en,
jp,
ru
}
}
}
]
],
页面跳转
<nuxt-link
v-for="item in menus"
:key="item.key"
:to="localePath(item.url)"
>
{{ $t(item.name) }}
</nuxt-link>
this.$router.push(this.localePath(item.url))
切换语言
this.switchLocalePath('en')
获取locale信息
this.$i18n
this.$i18n.localeProperties.code