安装插件
npm install vue-i18n@^8.18.2
i18njs帮助类
import Vue from 'vue'
import VueI18n from 'vue-i18n'
//this.$i18n.locale // 通过切换locale的值来实现语言切换
//window.vm.$i18n.t('lang.我的网站名') 主要为了在index页面使用多语言
//html {{$t('lang.test')}}
//js调用this.$t('lang.test')
//自己的框架里面所用的多语言
export function initI18n() {
Vue.use(VueI18n)
let i18nOptions = {
locale: 'CN', // 语言标识
fallbackLocale: 'CN',//没有英文的时候默认中文语言
silentFallbackWarn: true,//抑制警告
messages: {
'CN': require('./locale/zh.js'), // 中文语言包
'US': require('./locale/en.js') // 英文语言包
}
}
return new VueI18n(i18nOptions)
}
语言文件
export const lang = {
test: 'English',
}
export const lang = {
test: '中文',
}
mainjs
//多语言
import {initI18n} from '@/lang/i18n.js'
const i18n =initI18n();
/* window.vm.$i18n.t('lang.我的网站名') 主要为了在index页面使用多语言 */
window.vm = new Vue({
el: '#app',
router,store,i18n,
components: { App },
template: '<App/>'
})