最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to force translate i18n in some specific text (variable) in Vuejs - Stack Overflow

programmeradmin0浏览0评论

In normal context, we just attach translation property to a variable like :

this.name = this.$t('language.name');

But I want to specific it in a specific language sometime( ex: in French). Can we do something like this in vue.js ?

this.name = this.$t('language.name', locale: fr);

In normal context, we just attach translation property to a variable like :

this.name = this.$t('language.name');

But I want to specific it in a specific language sometime( ex: in French). Can we do something like this in vue.js ?

this.name = this.$t('language.name', locale: fr);
Share Improve this question edited Feb 3, 2022 at 5:56 Rohìt Jíndal 27.2k15 gold badges77 silver badges132 bronze badges asked Jul 19, 2021 at 3:55 DFX NguyễnDFX Nguyễn 7071 gold badge10 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Using the old package kazupon/vue-i18n, the following should be possible:

$t(key, locale)

When using the successor-package intlify/vue-i18n-next, the answer depends on if you are using Vue I18n's Legacy API or the newer Composition API:


Using Legacy API as described in the normal setup guide the usages of the t() function are stated here.

This means you can still use the following call to translate a key to a specific locale (e.g. 'fr'):

$t(key, locale)

Example:

$t('message.key', 'fr')

Using the Composition API by calling createI18n() with the option legacy: false (as described here), the usages of the t() function are different as stated here. You can no longer pass the locale-string as the second parameter, but the locale can be handed over inside a TranslateOptions object. Unfortunately, there is no t(key,TranslateOptions) variant, but only the following variants:

$t(key, plural, TranslateOptions)
$t(key, defaultMsg, TranslateOptions)
$t(key, interpolations, TranslateOptions)

So the easiest solution would be e.g.:

$t('message.key', 1, { locale: 'fr' })
发布评论

评论列表(0)

  1. 暂无评论