current position:Home>The latest vue-i18n international plug-in realizes language switching (with source code)
The latest vue-i18n international plug-in realizes language switching (with source code)
2022-04-29 07:25:51【MFG_ six hundred and sixty-six】
Catalog
1. install i18n Plug ins and elementUI plug-in unit
npm install vue-i18n --save
npm i element-ui -S
2.main.js Global mount i18n and elementUI mount
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
// introduce vuei18n
import VueI18n from 'vue-i18n'
// In the form of plug-ins
Vue.use(VueI18n)
// establish VueI18n example
const i18n = new VueI18n({
locale: localStorage.getItem('language') || 'zh', // Language mark
messages: {
'zh': require('../src/assets/lang/zh'),
'en': require('../src/assets/lang/en')
},
silentTranslationWarn: true, // Remove internationalization warnings
})
new Vue({
router,
store,
i18n, // Note that you should also add , export i18n!!!!
render: h => h(App)
}).$mount('#app')
Location of the local language resource pack , Here's the picture
3. Language file
en.js, The code is as follows
module.exports = {
i18n: {
breadcrumb: 'International projects',
tips: 'Change the language of the current content by switching the language button.',
btn: 'switch language',
title1: 'News sentence',
p1: "1. Open news web pages and contact tools every day to get together with the world. Some people refuse to alienate themselves from the world. But some people are alienated from themselves when they are close to the world, because your meaning does not occupy a position in it.",
p2: "2. News is like an onion, because as long as you peel off a layer of skin called truth, you can see another truth. Just like peeling an onion, the true facts can be obtained only after continuous doubt and evidence collection.",
p3: "3. In the face of absolute power, freedom of the press and innocence of speech are floating clouds."
}
}
zh.js, The code is as follows
module.exports = {
i18n: {
breadcrumb: ' International projects ',
tips: ' By switching the language button , To change the language of the current content .',
btn: ' switch language ',
title1: ' News sentences ',
p1: '1、 Open news pages and contact tools every day , Be united with the world . Some people refuse to alienate , Alienation from the world . But some people are close to the world , But alienated from itself , Because your meaning doesn't occupy a place in it . ',
p2: '2、 News is like onions , Because just peel off a layer of skin called truth , You can see another truth . It's like peeling an onion , Through constant doubt and evidence , To get the real facts . ',
p3: '3、 In the face of absolute power , What freedom of the press 、 Innocent speech , Are floating clouds . ',
}
}
4. Use in components
The source code of the first method is as follows :
<template>
<div>
<el-card>
<span>{
{ $t("i18n.tips") }}</span>
<el-button type="primary" @click="$i18n.locale = $i18n.locale === 'zh' ? 'en' : 'zh'">{
{ $t("i18n.btn") }}
</el-button>
<div class="list">
<h2>{
{ $t("i18n.title1") }}</h2>
<p>{
{ $t("i18n.p1") }}</p>
<p>{
{ $t("i18n.p2") }}</p>
<p>{
{ $t("i18n.p3") }}</p>
</div>
</el-card>
</div>
</template>
<script> export default {
} </script>
<style> </style>
The effect is as follows
The source code of the second method is as follows :
<template>
<div>
<span> switch language :</span>
<select v-model="selLocale" placeholder=" switch language ">
<option value="zh"> Simplified Chinese </option>
<option value="en">English</option>
</select>
<h2>{
{ $t("i18n.title1") }}</h2>
<p>{
{ $t("i18n.p1") }}</p>
<p>{
{ $t("i18n.p2") }}</p>
<p>{
{ $t("i18n.p3") }}</p>
</div>
</template>
<script> export default {
name: 'Home', data () {
return {
selLocale: this.$i18n.locale } }, watch: {
// Switch between multiple languages selLocale (newValue) {
this.$i18n.locale = newValue; localStorage.setItem("language", newValue); console.log(this.$t("message.hello")); console.log(this.$i18n.t("message.hello")); } }, } </script>
The effect is as follows
The source code of the third writing method is as follows :
<template>
<div>
<!-- switch language -->
<el-dropdown trigger="click" @command="handleSetLanguage">
<div>
<span class="el-dropdown-link">
Click to switch <i class="el-icon-arrow-down el-icon--right"></i>
</span>
</div>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item :disabled="language === 'zh'" command="zh">
chinese
</el-dropdown-item>
<el-dropdown-item :disabled="language === 'en'" command="en">
English
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<h2>{
{ $t("i18n.title1") }}</h2>
<p>{
{ $t("i18n.p1") }}</p>
<p>{
{ $t("i18n.p2") }}</p>
<p>{
{ $t("i18n.p3") }}</p>
</div>
</template>
<script> export default {
name: 'Home', data () {
return {
selLocale: this.$i18n.locale } }, methods: {
handleSetLanguage (lang) {
this.$i18n.locale = lang; this.language = lang; localStorage.setItem("lang", lang); }, // Switch between Chinese and English changeLang (val) {
let locale = this.$i18n.locale; if (val == this.$t("English")) {
locale === "zh" ? (this.$i18n.locale = "en") : (this.$i18n.locale = "zh"); localStorage.lang = this.$i18n.locale; this.language = this.$t("English"); document.title = 456; } else if (val == this.$t(" chinese ")) {
locale === "zh" ? (this.$i18n.locale = "en") : (this.$i18n.locale = "zh"); localStorage.lang = this.$i18n.locale; this.language = this.$t(" chinese "); } }, // Verify that the form is updated in a timely manner reload () {
this.isActiveRoute = false; this.$nextTick(() => {
this.isActiveRoute = true; }); }, }, created () {
this.account = window.localStorage.getItem("account"); this.activePath = window.sessionStorage.getItem("activePath"); if (this.$i18n.locale == "en") {
this.language = this.$t("English"); } else {
this.language = this.$t(" chinese "); } }, watch: {
"$i18n.locale" (newVal, oldVal) {
if (newVal != oldVal) {
this.reload(); } }, }, } </script>
The effect is as follows
5. Calling method
label Inside
call
<div>{ { $t("i18n.title1") }}</div>
label On
call
<el-input :placeholder="$t('lang.input.PleaseEnterTheRoleName')"></el-button>
js Calling method
data () {
return {
message:this.$t('i18n.title1')
}
}
If you feel the article is good, remember to pay attention and collect , If there is something wrong, please correct it , If you need to reprint , Please indicate the source , Thank you very much !!!
copyright notice
author[MFG_ six hundred and sixty-six],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290458556079.html
The sidebar is recommended
- How to realize Ethernet wireless communication between 200smartplc?
- Still working on nginx configuration? Try this visual configuration tool. It's really powerful!
- Simple sorting of JavaScript deep copy and shallow copy
- React realizes the communication of brother components by means of message subscription and publication
- React limits props
- Java lesson 30
- Java lesson 29 136 The number 1189 appears only once Maximum number of balloons
- Simple sorting of JavaScript deep copy and shallow copy
- "Geely" new SUV is equipped with frameless doors, and the interior is more beautiful than Mercedes Benz. Can 19 universal accept it?
- Configuration of using less in react project
guess what you like
Expression used in react render
React configuration agent
JQuery sends a post request (a method of connecting PHP variables to strings)
Solve the problem that Django cannot access the local server( http://127.0.0.1:8000/ )Or the problem that the command line execution (Python 3 manage.py runserver 0.0.0.0:8000) does not respond
Summary of the writing method of react router V6
Bipeng ditch, punch in and net red, open-air boundless swimming pool, Ramada hot spring, encounter the beauty of the four seasons
JavaScript gets and modifies elements. What's wrong with not extracting the classlist attribute
Mixed mixin of Vue
Vue + element implements table filtering
Vue router2. 0
Random recommended
- Which one charges less for opening a securities account and how to open the account
- Spring MVC notes 02 domain object sharing data, view, restful, httpmessageconverter, file upload and download
- Httpclient setting timeout
- Command line / Python uses pdf2htmlex to convert PDF to HTML
- [front end three swordsmen III] analysis of JavaScript scalpel Part II
- How to choose the front-end learning path
- Finite element parametric element, calculation, theoretical problems
- Handwritten CSS modules to understand its principle
- Front end browser debugging tips
- Performance problem analysis guide for enterprise JavaScript applications running in production systems
- CSS aspect-ratio All In One
- Actual combat of vue3 project --- Zhihu daily --- details page
- Actual combat of vue3 project --- Zhihu daily --- home page function
- Great Wall Motors is falling endlessly! The boss has lost 150 billion yuan in half a year, and the performance of the new energy sector has improved
- Nginx tips batch shutdown process
- Openresty introduces nginx_ upstream_ check_ Module module
- Nginx multiple servers_ How does name match
- Why does the front end still prompt cannot post, and the error reported by the back end still prompt null pointer?
- HTML Li set margin: 50%, but the width of the outermost div is the standard
- Are there any specific steps to create a prototype, such as JavaScript?
- How does HTML5 display the value obtained from the database in the specified area
- Problems related to nginx rewriting interface
- What happens when you open a CSS file in eclipse without responding
- React download local template can be downloaded locally, but the deployment online download is blank
- @Babel / traverse official example running error: typeerror: traverse is not a function
- The click event of jQuery has no effect
- How to learn from non professional background?
- Usage of API documented as @since 1.8+ less... (Ctrl+F1) Inspection info: This inspection finds all
- Configuration Vue of webpack
- Introduction to nginx (learning notes)
- Is the financial management class of qiniu school true? Can you learn how to manage money in free lessons
- How does Java randomly get elements from a list
- Use of Vue on in Vue
- Use of Vue router
- Vue basic syntax
- Use of webpack
- Vue diff algorithm
- CSS -- Text gradient from top to bottom
- Routing: Vue router
- Leetcode 658. Find K closest elements