这篇文章主要介绍了使用vue 国际化i18n 多实现语言切换功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
安装
npm install vue-i18n
新建一个文件夹 i18n ,内新建 en.js zh.js index.js 三个文件
准备翻译信息
en.js
export default { home: { helloworld: "hello workd !" } };
zh.js
export default { home: { helloworld: "你好<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码网^</strong>世界" } };
index.js
创建Vue-i18n实例
import Vue from "vue"; import VueI18n from "vue-i18n"; import enLocale from "./en"; import zhLocale from "./zh"; Vue.use(VueI18n); const i18n = new VueI18n({ locale: localStorage.lang || "zh", messages: { en: { ...enLocale }, zh: { ...zhLocale } } }); export default i18n;
i18n 挂载到 vue 根实例
main.js
import Vue from "vue"; import App from "./App.vue"; import router from "./router"; import store from "./store"; import i18n from "./assets/i18n/index"; Vue.config.productionTip = false; Vue.prototype.$i18n = i18n; new Vue({ router, store, i18n, render: h => h(App) }).$mount("#app");
简单的使用
about.vue
<div class="about"> <h1>{{ $t("home.helloworld") }}</h1><button>切换英文</button><p>{{hi}}</p></div>
注意:
比如说上面的hi 你要通过这种形式来写的时候,不能放在data 里面,因为当语言切换的时候 他是不会变的 ,要写在computed内
总结
以上所述是小编给大家介绍的使用vue 国际化i18n 多实现语言切换功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!
以上就是使用vue 国际化i18n 实现多实现语言切换功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!