question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to pass default value using $t()

See original GitHub issue

Reporting 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kkyrylcommented, Aug 29, 2021

@kazupon thank you so much for clarifying. I do hope you can update the documentation because this was not an easy discovery 😃

0reactions
kazuponcommented, Aug 28, 2021

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.

I think I understand it better now, looks like it’s legacy: true by default and $t is only available in legacy mode ($t is undefined if I set legacy: false).

In Composition API (legacy: false), You can use $t. so you need to set up globalInjection: true in creaetI18n. https://vue-i18n.intlify.dev/guide/advanced/composition.html#implicit-with-injected-properties-and-functions

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript : Can't set default parameter value as false
You just need to set the default value of freehand no need for ? it's ... the parameters as object is you can...
Read more >
Unable to specify a default value for a generic parameter #3737
I think the problem here is that in general, e.g. if there are other parameters also using _T in their type, the default...
Read more >
Specify default column values | BigQuery - Google Cloud
This page describes how to set a default value for a column in a BigQuery table. When you add a row to a...
Read more >
Be careful when using || to set default values in JavaScript
When no value is passed into a function the value of the parameter will be undefined . Using OR however means that the...
Read more >
Lombok Builder with Default Value - Baeldung
In this quick tutorial, we'll investigate how can we provide default values for attributes when using the builder pattern with Lombok.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found