当前位置: 首页 > Web前端 > vue.js

前端通用国际化DI18n_0

时间:2023-03-31 17:54:07 vue.js

1.开发环境vue2.电脑系统windows10专业版3、在开发过程中,我们可能需要开发不同的语言。这时候就需要用到国际化了。先分享一下通用技术DI18n.4。安装npmidi18n-translate--save5。在src目录下新建d18n,新建文件夹langs,目录结构如下:6.en.js代码如下:module.exports={'hello':'hello,world',}7.zh.js代码如下:module.exports={'hello':'Hello,world',}8.在main.js中添加如下代码:importDI18nfrom'di18n-translate';constdi18n=newDI18n({locale:"en",//localeisReplace:false,//是否开始运行(适用于不使用任何构建工具的开发过程)messages:{//语言映射表en:require("@/d18n/langs/en.js"),zh:require('@/d18n/langs/zh.js')}})Vue.prototype.$d18n=di18n;9.在vue模板中使用

{{$d18n.$t("hello")}}

10。语言切换//在相应的方法中添加this.$d18n.locale="en";11、方案2(目录结构为)//在src目录下新建d18n文件夹,在d18n文件夹下新建langs文件夹,在langs文件夹下新建en.js、zh.js;在d18n文件夹下新建index.js12.index.js代码如下:importDI18nfrom'di18n-translate';constdi18n=newDI18n({locale:"zh",//localeisReplace:false,//是否进行替换(不使用构建工具时适用工具开发流程)messages:{//语言映射表en:require("@/d18n/langs/en.js"),zh:require('@/d18n/langs/zh.js')}})exportdefaultdi18n;13.en.js代码如下:module.exports={'hello':'hello,world',}14.zh.js代码如下:module.exports={'hello':'Hello,world',}15.main.js代码如下:importdi18nfrom'@/d18n/index.js';Vue.prototype.$d18n=di18n;vue模板的用法不变16.本期的分享到此结束,希望对大家有所帮助,让我们一起努力,勇攀高峰。