Vue implements internationalization of web page language switching

Vue implements internationalization of web page language switching

1. Basic steps

1: Install yarn add vue-i18n

Create a new .js file in this path: src/lang/index.js :

2: Import

import VueI18n from 'vue-i18n'

3: Register

import Vue from 'vue'

Vue.use(VueI18n)

4: Instantiation

const i18n = new VueI18n({
	locale:'The current language identifier', // en: English zh: Chinese messages:{
		// Language pack en:{
		home:'home'
		},
		zh:{
		home:'Home'
		}
	}
})

5: Exposed

export default i18n

6: Mount to main.js

import i18n from '@/lang'
new Vue({
i18n
})

Use: <div>{ {$t('home')}}</home>

2. Import element-ui international language configuration in main.js

import i18n from './lang/index'
import ElementUI from 'element-ui'
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
})

3. Create a new .vue file to switch languages ​​through buttons

Notice:

this.$i18n.locale can get and set the current language and use internationalization in js files

4. To achieve the internationalization of the website menu, you need to introduce it in the routing

After import i18n from '@/lang' i18n.t===this.$t

This concludes this article about Vue's implementation of internationalization of web page languages. I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • The whole process of realizing website internationalization using Vite2 and Vue3
  • Detailed explanation of how Vue uses i18n to achieve internationalization
  • Use vue internationalization i18n to implement multiple language switching functions
  • The international development and implementation process of vue-i18n and element-ui in the vue project
  • Vue uses vue-i18n to implement internationalization code

<<:  Introduction to the Enctype attribute of the Form tag and its application examples

>>:  Detailed explanation of the process of deploying the distributed configuration center Apollo with one click using docker compose

Recommend

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

How to use nginx to access local static resources on Linux server

1. Check whether port 80 is occupied. Generally, ...

Workerman writes the example code of mysql connection pool

First of all, you need to understand why you use ...

In-depth analysis of the Linux kernel macro container_of

1. As mentioned above I saw this macro when I was...

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Win10 installation Linux system tutorial diagram

To install a virtual machine on a Windows system,...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

Detailed process of installing logstash in Docker

Edit docker-compose.yml and add the following con...

Vue + element dynamic multiple headers and dynamic slots

Table of contents 1. Demand 2. Effect 3. All code...