首页 > 解决方案 > VueJS 翻译插件

问题描述

我正在使用一个名为 VueTranslated 的插件,由 javisperez 制作,我在获取翻译文本时遇到了一些困难。

我正在使用全局用法。我有一个包含所有单词及其翻译的文件,例如:

import Vue from 'vue'
import VueTranslate from 'vue-translate-plugin'
Vue.use(VueTranslate)

Vue.locales({
  'pt_BR': {
    'hello-world': 'Olá mundo'
  },
  'en_US': {
    'hello-world': 'Hello world'
  }
})

导出默认值()

因此,当我在任何组件中时,我可以使用以下内容来获取单词:

<template>
<div>
    <p> {{t('hello-world)}} </p>
</div>
</template>

然后,p标签显示字符串Hello worldOlá mundo取决于我的语言集。但是如果我在这个组件中有类似的东西

<template>
<div>
    <p> {{message}}
</div>
</template>

<script>
    export default {
        data: () => ({
            message: this.$translate.text('hello-world)
        })
    }
</script>

它只是出现hello-world在我的p标签中。当它来自我的组件的脚本标签时,如何获得翻译的字符串?或者,除了这个插件,我还有什么可以尝试的吗?谢谢

标签: javascriptvue.jsvuejs2translate

解决方案


我只需要this.t('message')像这样使用:

<script>

export default {
    data {
        return {
            message: this.t('message')
        }
    }
}

</script>

推荐阅读