来自 api 调用的 VueI18n 动态标签

问题描述

我想从 api 中检索标签,它们有 2 种语言,并在用户单击更改语言时切换它们。现在认加载一种语言,但它不是响应式的。如何让它对用户更改语言做出反应?我正在考虑以某种方式在其他一切之前调用函数并将标签存储在 localStorage

代码:(lang.js 文件

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { HTTP } from './../config/http-request.js'

Vue.use(VueI18n)

const locale = 'da'
var langName;

const i18n = new VueI18n({
  locale,// language identifier
  messages: {}
})
 
function getLang(lang){
    console.log(lang) 
    if(lang == "da"){
        langName = "Danish"
    }else{
        langName = "English"
    }
    HTTP.get("1.0/language/label?language_name="+ langName + "&compact=True")
    .then((response) => {
        var msg = response.data.compact
        i18n.setLocaleMessage(lang,msg)
        i18n.locale = lang
        return lang
    })
    .catch(function(error) {
        console.log(error)
    })
}


 // This method is called when the manual switch languages,to be loaded when the page is initialized with a default language
// Vue.prototype.$loadLanguageAsync = loadLanguageAsync

getLang(locale)


export default i18n

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)