Nuxt.js - How do I use vue-i18n outside components?

4.8k views Asked by At

I'm using the plugin vue-i18n for translations in a Nuxt.js-powered SPA. This allows easy access to messages within components, like this:

$t('footer.press')

But how do I get translations outside components? In my specific case, I need them in a store action:

export const actions = {

  async myAction({ commit, state, rootState, rootGetters }, options) {

    (...)

    const message = $t("example.message.key")             // doesn't work, undefined
    const message1 = this.$i18n.t("example.message.key")  // doesn't work, undefined

    (...)

    })

  }


This is how I include the vue-i18n plugin in the project:

package.json

…
 "dependencies": {
        …
        "vue-i18n": "^8.18.2",
        …
    },
…

nuxt.config.js

…
plugins: [
        …
        '~/plugins/i18n',
        …
    ],
…
2

There are 2 answers

4
gru On BEST ANSWER

After some research, I found a working solution on the Vue Forum here:

const message = this.app.i18n.t("example.message.key")

Works for me like a charm!

1
Jiro Matchonson On
this.$t('logInWongCredentials')

(nuxt)