Home  >  Article  >  Web Front-end  >  How vue and vue-i18n implement multi-language switching of background data

How vue and vue-i18n implement multi-language switching of background data

php中世界最好的语言
php中世界最好的语言Original
2018-03-28 14:55:215071browse

This time I will show you how vue and vue-i18n realize the multi-language switching of background data. How vue and vue-i18n realize the multi-language switching of background data.Notes What are they? Here are actual cases. Let’s take a look.

In the XXX.js fileDefine the function:

getUser(context,info){
  context.$http.get(SERVER_URL+'/users',info).then(function(data){
   let err =data.body.error;
   if(err===0){
    let dataObj = data.body.userLists;  //获取后台返回的数据
    this.users = dataObj.items.map(function (e,i) {  //遍历获取的数据,用this.$t()将每项数据与翻译资源对应
     e.gender=context.$t(e.gender);            //context 是this, gender 与 diabetes_type 为每个items里的key;gender里的value有三种:'GDRNF'、‘GDRF'、‘GDRM'
     e.diabetes_type = context.$t(e.diabetes_type);
     return e;
    });
    this.listLoading = false;
    // console.log(dataObj);
   }
  })
 },
Then you can call the function in the vue component: XXX .getUser(this,info); Perform corresponding operations on the data obtained in the background and put it into the users

array;

The above method is to traverse the data obtained through the map function, using this.$ t() corresponds the value of items to the value in the translation resource, thereby realizing multi-language switching of background data;

Part of the data in the en.json translation resource:

{ 
  "GDRNF":"Not Fill", 
  "GDRF":"Female", 
  "GDRM":"Male", 
 }
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 update the array with $set in vue.js

How to move the array in vue.js Position and update the view

The above is the detailed content of How vue and vue-i18n implement multi-language switching of background data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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