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 Nginx Redis session sharing process diagram

1. Preparation Middleware: Tomcat, Redis, Nginx J...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Solution to nginx hiding version number and WEB server information

Nginx can not only hide version information, but ...

What are the image file formats and how to choose

1. Which three formats? They are: gif, jpg, and pn...

Implementation of deploying Apollo configuration center using docker in CentOS7

Apollo open source address: https://github.com/ct...

Comprehensive explanation of CocosCreator hot update

Table of contents Preface What is Hot Change Coco...

Example of configuring multiple SSL certificates for a single Nginx IP address

By default, Nginx supports only one SSL certifica...

Detailed explanation of MySQL transactions and MySQL logs

Transactional Characteristics 1. Atomicity: After...

Mybatis fuzzy query implementation method

Mybatis fuzzy query implementation method The rev...

jQuery canvas draws picture verification code example

This article example shares the specific code of ...

Postman automated interface testing practice

Table of contents Background Description Creating...

How to implement variable expression selector in Vue

Table of contents Defining the HTML structure Inp...