Vue-i18n change locale not updating everything

pkdkk picture pkdkk · Aug 29, 2018 · Viewed 7.7k times · Source

i'm new to vue-i18n, seams great, but have some challenge getting it to work probably.

All template translations are updated as expected when changing locale, but when

script

data() {
  return {
    locales: {
      en: this.$i18n.t('topnav.lang.english'),
      da: this.$i18n.t('topnav.lang.danish'),
      sw: this.$i18n.t('topnav.lang.swedish'),
      no: this.$i18n.t('topnav.lang.norwegian'),
    }
  }
},

template

WORKING

{{$t('topnav.lang.english')}}

NOT WORKING

<a class="dropdown-item">{{locales.en}}</a>

NOT WORKING

<a class="dropdown-item" @click="changeLocale(key)" v-for="(value, key) in locales">{{value}}</a>

i have tried a lot of things, eg. lazyload the languages files and so on, but with no luck.

Answer

Zain Wania picture Zain Wania · Sep 27, 2018

change from data to computed, data is not inherently reactive but luckily computed is!

the alternative is to directly put your translation in the template if you do not want to use computed