Home > Web Front-end > JS Tutorial > body text

Use vue-i18 plug-in in Vue to achieve multi-language switching

php中世界最好的语言
Release: 2018-06-02 10:26:31
Original
2897 people have browsed it

This time I will bring you how to use the vue-i18 plug-in in Vue to achieve multi-language switching, and how to use the vue-i18 plug-in in Vue to achieve multi-language switching. NotesThere are Which ones, the following are practical cases, let’s take a look.

#step1: Install the vue-i18 plug-in in the project

cnpm install vue-i18n --save-dev
Copy after login

step2:Introduce it into the project’s entry filemain.js vue-i18n plug-in

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 
 }, 
})
Copy after login

#step3: When using

in the page and in the template, there are probably the following three situations. If there are any omissions, please let us know. Correction

zh.js

module.exports = { 
 menu : { 
   home:"首页" 
 }, 
 content:{ 
   main:"这里是内容" 
 } 
}
Copy after login

en.js

module.exports = { 
 menu : { 
   home:"home" 
 }, 
 content:{ 
   main:"this is content" 
 } 
}
Copy after login

1) Use it as the body content in the tag

<p class="title">{{$t('menu.home')}}</p>
Copy after login

2) Use it as the tag attribute

<input :placeholder="$t(&#39;content.main&#39;)" type="text">
Copy after login

3) When used as js Chinese text

console.log(this.$t('content.main'));
Copy after login

4) To be added...

step4: Switch between Chinese and English on the page, and bind the button for switching between Chinese and English Define the event as follows:

tabEn: function () { 
 this.$i18n.locale = 'en' 
}, 
tabCn: function () { 
 this.$i18n.locale = 'zh' 
}
Copy after login

At this point, the use of the vue-i18n plug-in is completed.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use filter in vue

##How to use vue to determine the class of dom

The above is the detailed content of Use vue-i18 plug-in in Vue to achieve multi-language switching. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template