Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifing a specific locale in $t #1184

Closed
ibrahimBeladi opened this issue Apr 15, 2021 · 4 comments
Closed

Specifing a specific locale in $t #1184

ibrahimBeladi opened this issue Apr 15, 2021 · 4 comments

Comments

@ibrahimBeladi
Copy link

I would like to ask if there is a way to specify the language used in $t function regardless of the current locale/language.
In other words, I want to show messages in language x without hard-coding the messages.
For example, $t('some.message', { locale: 'en' }) would result in the English translation regardless of the current locale.

@ibrahimBeladi ibrahimBeladi changed the title Specifing a specific language in $t Specifing a specific locale in $t Apr 15, 2021
@kazupon
Copy link
Owner

kazupon commented Apr 17, 2021

you can use locale property of $i18n
please check detail docs:
https://kazupon.github.io/vue-i18n/guide/locale.html

thanks

@kazupon kazupon closed this as completed Apr 17, 2021
@ibrahimBeladi
Copy link
Author

What I am asking is that I have the app with locale = a
I want to show one message with locale: b and another message with locale: c.
So, the messages are shown in locale b and c respectively, while the whole app is displayed as locale = a

@kazupon
Copy link
Owner

kazupon commented Apr 17, 2021

This is not the place to ask questions, Here is the bug & issue-reporting place.
You can ask about the question in the Vue.js Discord.
Please understand.

Thanks

@codeflorist
Copy link

codeflorist commented Dec 2, 2021

For anyone else coming accross this question:

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' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants