Unable to pass default value using $t()
See original GitHub issueReporting a bug?
There’s a very high probability of me simply misunderstanding how it’s supposed to work but after some debugging I was not able to find a way to do that.
Based on the documentation we should be able to call:
$t("translation.key", "default value")
However the “default value” is always interpreted as a locale key. Next I tried:
$t("translation.key", null, {default: "default value")})
But options object is never taken into consideration.
After some debugging I was able to find what (I believe) is the culprit:
in t(...args) {
:
else if (isPlainObject(arg2)) {
named = arg2;
}
if (isArray(arg3)) {
list = arg3;
}
else if (isPlainObject(arg3)) {
named = arg3;
}
Shouldn’t it be:
else if (isPlainObject(arg3)) {
assign(options, arg3);
}
Since named interpolation object is expected as arg2?
This is sort of further confirmed by looking at $rt which passes options object as the third argument further down and after calling $rt with the options object as the third argument i can confirm that options are passed down the line.
Expected behavior
I expect to be able to pass options object or default value when using $t function
Reproduction
To reproduce try passing default value to the $t function
System Info
Using vue-i18n v9.1.6 with vue 3 in the browser context
Screenshot
No response
Additional context
No response
Validations
- Read the Contributing Guidelines
- Read the Documentation
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
@kazupon thank you so much for clarifying. I do hope you can update the documentation because this was not an easy discovery 😃
Unfortunately,
$t
default value passing doesn’t support in Legacy API (legacy: true
). for this reason, it’s kept compatible for vue-i18n@v8.x API.In Composition API (
legacy: false
), You can use$t
. so you need to set upglobalInjection: true
increaetI18n
. https://vue-i18n.intlify.dev/guide/advanced/composition.html#implicit-with-injected-properties-and-functions