如何在 Vuejs 中的某些特定文本(变量)中强制翻译 i18n
How to force translate i18n in some specific text (variable) in Vuejs
在正常情况下,我们只是将翻译 属性 附加到变量,例如 :
this.name = this.$t('language.name');
但我想在某个时候用特定的语言(例如:法语)来具体说明。
我们可以在 vue.js
中做这样的事情吗?
this.name = this.$t('language.name', locale: fr);
使用旧包kazupon/vue-i18n
,应该可以做到:
$t(key, locale)
当使用后继包 intlify/vue-i18n-next
时,答案取决于您使用的是 Vue I18n 的 Legacy API 还是更新的 作文API:
使用 旧版 API,如正常 setup guide the usages of the t()
function are stated here 中所述。
这意味着您仍然可以使用以下调用将密钥转换为特定语言环境(例如 'fr'):
$t(key, locale)
示例:
$t('message.key', 'fr')
通过调用带有选项 legacy: false
的 createI18n()
来使用 组合 API(如 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 所述)。不幸的是,有没有 t(key,TranslateOptions
) 变体,只有以下变体:
$t(key, plural, TranslateOptions)
$t(key, defaultMsg, TranslateOptions)
$t(key, interpolations, TranslateOptions)
所以最简单的解决方案是例如:
$t('message.key', 1, { locale: 'fr' })
在正常情况下,我们只是将翻译 属性 附加到变量,例如 :
this.name = this.$t('language.name');
但我想在某个时候用特定的语言(例如:法语)来具体说明。
我们可以在 vue.js
中做这样的事情吗?
this.name = this.$t('language.name', locale: fr);
使用旧包kazupon/vue-i18n
,应该可以做到:
$t(key, locale)
当使用后继包 intlify/vue-i18n-next
时,答案取决于您使用的是 Vue I18n 的 Legacy API 还是更新的 作文API:
使用 旧版 API,如正常 setup guide the usages of the t()
function are stated here 中所述。
这意味着您仍然可以使用以下调用将密钥转换为特定语言环境(例如 'fr'):
$t(key, locale)
示例:
$t('message.key', 'fr')
通过调用带有选项 legacy: false
的 createI18n()
来使用 组合 API(如 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 所述)。不幸的是,有没有 t(key,TranslateOptions
) 变体,只有以下变体:
$t(key, plural, TranslateOptions)
$t(key, defaultMsg, TranslateOptions)
$t(key, interpolations, TranslateOptions)
所以最简单的解决方案是例如:
$t('message.key', 1, { locale: 'fr' })