This time I will show you how to switch the national language in Vue, and what are the precautions for switching the national language in Vue. The following is a practical case, let's take a look.
In the development process based on vue-cli project, the multi-language switching function can use the vue-i18 plug-in. The specific implementation method is as follows:
step1: Install vue-i18 in the project Plug-in
cnpm install vue-i18n --save-dev
step2: Introduce the vue-i18n plug-in into the project’s entry file main.js
import Vue from 'vue' import router from './router' import VueI18n from 'vue-i18n' Vue.use(VueI18n) const i18n = new VueI18n({ locale: 'zh', // 语言标识 messages: { 'zh': require('./assets/lang/zh'), 'en': require('./assets/lang/en') } }) // vue实例中引入 /* eslint-disable no-new */ new Vue({ el: '#app', i18n, router, template: '<Layout/>', components: { Layout }, })
step3: Use ## in the page
#When used in templates, there are probably the following three situations. If there are any omissions, please correct mezh.jsmodule.exports = { menu : { home:"首页" }, content:{ main:"这里是内容" } }
module.exports = { menu : { home:"home" }, content:{ main:"this is content" } }
<p class="title">{{$t('menu.home')}}</p>
<input :placeholder="$t('content.main')" type="text">
console.log(this.$t('content.main'));
tabEn: function () { this.$i18n.locale = 'en' }, tabCn: function () { this.$i18n.locale = 'zh' }
How to get the absolute position of the DOM element in the front-end interface
The above is the detailed content of How to switch national language in Vue. For more information, please follow other related articles on the PHP Chinese website!