Reputation: 3622
I have as usual my translation files in locales.
Now I have an invitation mail in various languages.
I want the user to select in what language the mail should be sent, because it's not about the language of the operating user, it's about the language the mail receiver should read.
Is there a way to tell rails explicitly what language to pick, like t(:my_string, :en)
?
The goal is, to have it temporary translated for only one single call of t
.
This is what I need, to tell within the t()
command, what language is to be used.
Upvotes: 7
Views: 1601
Reputation: 95
if you want to temporarily change locale:
I18n.with_locale(:*your_locale*)
serves the purpose.
for further readings, https://translation.io/blog/rails-i18n-with-locale
Upvotes: 0
Reputation: 129
To implement this, you need to have locale information for the receiver. Once you have that, you can pass it to I18n.t
method in your rails code.
preferred_locale = `Receiver.preferred_locale_name` || I18n.default_locale
I18n.t('sample-string', locale: preferred_locale)
Upvotes: 0
Reputation: 1182
It's possible to pass :locale
option to t
method:
t :my_string, locale: :de
Upvotes: 8
Reputation: 3584
You can use the I18n
backend which take the locale as first argument:
I18n.backend.translate(:en, :my_string)
Upvotes: 0