Joern Akkermann
Joern Akkermann

Reputation: 3622

explicitly tell into what language to translate

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

Answers (5)

googlesnet
googlesnet

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

Ayush Billore
Ayush Billore

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

Anton Styagun
Anton Styagun

Reputation: 1182

It's possible to pass :locale option to t method:

  t :my_string, locale: :de

Upvotes: 8

Nicolas Buduroi
Nicolas Buduroi

Reputation: 3584

You can use the I18n backend which take the locale as first argument:

I18n.backend.translate(:en, :my_string)

Upvotes: 0

bor1s
bor1s

Reputation: 4113

I think you will explicitly need to set proper language to I18n.
You can use: I18n.locale = :en to set language you need.
Also look here: I18n

Upvotes: 4

Related Questions