Nuxt.js - 如何在组件外部使用 vue-i18n?

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

我在 Nuxt.js 支持的 SPA 中使用插件 vue-i18n 进行翻译。这允许在组件 中轻松访问消息 ,如下所示:

$t('footer.press')

但是如何获得翻译外部组件?在我的具体情况下,我需要在 store 操作中使用它们:

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

    (...)

    })

  }


这是我在项目中包含 vue-i18n 插件的方式:

package.json

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

nuxt.config.js

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

经过一些研究,我在 Vue 论坛上找到了一个可行的解决方案 here:

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

对我来说很有魅力!

this.$t('logInWongCredentials')

(nuxt)