3

I have a typescript Vue.js app using vue-i18n and I want to port it to Nuxt.js. In the Vue.js app, I initialized i18n in a file by calling

// Create VueI18n instance with options
const i18n = new VueI18n({
  locale: 'ja', // set locale
  messages, // set locale messages
})

To access the i18n instance from code locations (Vue actions etc.) where there is no access to {{ $t('my.translation.key' }} as in templates, I exported the i18n object from the file where I instantiated it and then accessed it by calling i18n.t('my.translation.key') to retrieve translations and other properties like the current active language.

Now, with nuxt.js and nuxt-i18n, there is no such explicit initialization and everything happens "magically" by configuring nuxt-i18n in the Nuxt config.

How can I then access the i18n instance in actions/other code locations outside of templates? I tried to use just this.$i18n.t(...) but this did not work. Also, if I import Vue from 'vue' and try to call it via Vue.$i18n.t(...) it did not work. How can I get access to the i18n instance?

sceee
  • 1,681
  • 19
  • 34

0 Answers0