Hi take a look this code, if I set the message asynchronously whenever new data resolved, it doesn't re render the translation.
<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)
}
})
Updated
My current workaround is define a method that return Promise and the variable that will hold the text. When the promise is resolved, then I set the translation.
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>