我正在使用插件vue-i18n在 Nuxt.js 驱动的 SPA 中进行翻译。这允许轻松访问components中的消息,如下所示:
$t('footer.press')
但是如何在组件之外获得翻译?在我的具体情况下,我需要在商店操作中使用它们:
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 插件的方式:
包.json
…
"dependencies": {
…
"vue-i18n": "^8.18.2",
…
},
…
nuxt.config.js
…
plugins: [
…
'~/plugins/i18n',
…
],
…