首页 > 解决方案 > vue-i18n:有没有办法在 $t 方法中手动设置语言环境?

问题描述

我正在使用 Firestore 和 vue-18n 开发一个项目。我想要的是将翻译后的消息保存到 Firestore 中,并且我想在$t方法中手动设置语言环境。我的代码是这样的。

methods: {
    doSomething() {
        ...
        db.collection('messages').add({
            body: this.$t('componentName.hello'), // I want to set a locale here
        })
    }
}

有没有办法在$t方法内部设置语言环境?

标签: vue.jsvue-i18n

解决方案


...
 body: this.$t('componentName.hello', locale), //for example "en"
...

来源:https ://github.com/kazupon/vue-i18n/blob/5.x/gitbook/api.md#tkeypath-lang-arguments


推荐阅读