博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用vuex+vue-i18n方式国际化
阅读量:4706 次
发布时间:2019-06-10

本文共 999 字,大约阅读时间需要 3 分钟。

在static/lang下新建文件:

index.js、en.js、zh.js...

// en.jsmodule.exports = {  route: {    title: 'chengdu',  }    }// zh.jsmodule.exports = {  route: {    title: '成都',  }    }// index.jsimport path from 'path'import fs from 'fs'let files = fs.readdirSync(path.join('./', 'static', 'lang'))let langPack = {}for (let file of files) {  if (file !== 'index.js') {    langPack[file.replace(/.js/, '')] = require(`./${file}`)  }}export deafult langPack

然后在src/store/plugins下的i18n.js中使用插件方式引入vue-i18n

import * as types from '../types'import VueI18n from 'vue-i18n'import langPack from '@/../static/lang'import Vue from 'vue'Vue.use(VueI18n)export const i18n = new VueI18n({  locale: 'zh',  messages: langPack,})function i18nPlugin (store) {  store.subscribe((mutation, state) => {    if (mutation.type === types.SET_LANGUAGE) {      i18n.locale = mutation.payload.toString()    }  })}export default i18nPlugin

记得要在store/index.js中引入插件以及使用mutation的方式改变state。

转载于:https://www.cnblogs.com/mesopotamiazZ/p/8025814.html

你可能感兴趣的文章
乔布斯的魔力演讲
查看>>
JavaScript的Event Loop(浏览器)
查看>>
Customized version of Stack C++
查看>>
BackgroundWroker使用方法备忘
查看>>
从无到有实现主从复制
查看>>
【Android】如何创建自己的证书文件,如何为apk以及zip文件签名 openssl
查看>>
DIV+CSS兼容解决DIV最大宽度和最小宽度问题
查看>>
IIC总线的FPGA实现
查看>>
Standard C++ Episode 1
查看>>
记一次Linux服务器上查杀木马经历
查看>>
Winform Timer用法,Invoke在Timer的事件中更新控件状态
查看>>
多线程同步技术(一)
查看>>
HDU 1829 并查集up
查看>>
rabbitmq3.7集群搭建实战
查看>>
深入理解Asp.net MVC路由
查看>>
正则化方法:L1和L2 regularization、数据集扩增、dropout
查看>>
java是编译型语言还是解释型语言?
查看>>
day4 小结
查看>>
语系/地区码
查看>>
【非原创】LightOJ - 1284 Lights inside 3D Grid【概率期望】
查看>>