【问题标题】:Vue I18n Doesn't Rerender when Message Change消息更改时 Vue I18n 不重新呈现
【发布时间】:2018-03-22 04:09:58
【问题描述】:

您好,看看这段代码,如果我在解析新数据时异步设置消息,它不会重新呈现翻译。

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-i18n/dist/vue-i18n.js"></script>

<div id="app">
  <p>{{ $t("home") }}</p>
</div>

const locale = {
    id: {
    home: 'Beranda'
  },
  en: {
    home: 'Home'
  }
}

const i18n = new VueI18n({
  locale: 'id'
})

new Vue({
  el: '#app',
  i18n,
  created () {
    setTimeout(() => {
    this.$i18n.setLocaleMessage(locale)
  }, 100)
 }
})

更新

我当前的解决方法是定义一个返回 Promise 的方法和保存文本的变量。承诺解决后,我设置翻译。

const locale = {
	id: {
  	home: 'Beranda'
  },
  en: {
  	home: 'Home'
  }
}

const i18n = new VueI18n({
  locale: 'id'
})

new Vue({
	el: '#app',
	i18n,
  data: {
  	text: null
  },
  methods: {
  	getData () {
    	return new Promise(resolve => {
      	setTimeout(() => {
          this.$i18n.setLocaleMessage('id', locale.id)
          this.$i18n.setLocaleMessage('en', locale.en)
          resolve()
        }, 1000)
      })
    }
  },
  created () {
  	this.getData().then(() => {
    	this.text = this.$t('home')
    })
  }
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-i18n/dist/vue-i18n.js"></script>

<div id="app">
  <p>{{ text }}</p>
</div>

【问题讨论】:

    标签: vue.js vuejs2 vue-i18n


    【解决方案1】:

    看文档,需要设置keypath。

    定义:

    const messages = { // keypath
        id: {
        home: 'Beranda'
      },
      en: {
        home: 'Home'
      }
    }
    

    并使用messages 设置默认语言:

    const i18n = new VueI18n({
      locale: 'id', // default locale
      messages // keypath is set
    })
    

    如果您使用消息以外的键路径名称:

    const translations = { // different keypath constant
        id: {
        home: 'Beranda'
      },
      en: {
        home: 'Home'
      }
    }
    
    
    
    const i18n = new VueI18n({
      locale: 'id',
      messages: translations // messages now use translations keypath
    })
    
    
    new Vue({
        el: '#app',
        i18n,
      /* created () { // you don't need to set locale here
        setTimeout(() => {
          this.$i18n.setLocaleMessage(locale)
        }, 100)
      } */
    })
    

    这是working demo


    更新

    根据您的评论,异步设置语言环境 - 您可以这样使用:

    const i18n = new VueI18n({
      messages
    })
    
    new Vue({
        el: '#app',
        i18n,
      created () {
        setTimeout(() => {
          this.$i18n.locale  = 'id'
        }, 100)
      }
    })
    

    demo

    【讨论】:

    • 您好,我已经尝试了您的建议。在控制台上更改为i18n 返回(index):68 Uncaught TypeError: Cannot read property 'setLocaleMessage' of undefined。看看这个复制链接jsfiddle.net/jefrydco/q5d3cp9a/13,
    • 您好我使用 setTimeout 设置 localeMessage 是模拟网络请求。我想从API动态获取消息,所以必须是异步执行。
    • 不是当前的语言环境对象,而是消息对象本身。让我们澄清一下,这里我想动态添加从网络请求中获取的消息对象。翻译数据源来自 API。本地未定义。在您的回答中,您在实例化 VueI18n 构造函数时定义了消息对象。看看我上面更新的当前解决方法。
    • 您应该使用const i18n = new VueI18n({ messages: locale }) 并将语言环境设置为“id”或“en”作为我最后更新的答案。
    • @VishalGulati 我实现的解决方法是在上面这篇文章的更新部分
    猜你喜欢
    • 2019-01-09
    • 2021-01-26
    • 1970-01-01
    • 2021-07-10
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多