1 min. read

Temporarily Change Locale

Michaël Hoste

Michaël Hoste

Founder & CEO

Who is Michaël?

Michaël is the founder of Translation.io. He is always interested in hearing your feedback on the product.

Feel free to get in touch: michael@translation.io

Illustrative image for the blog post Temporarily Change Locale

Sometimes it can be very useful to temporarily change the current locale to perform an action and then directly switch back to the original locale. For example you need to deliver an email to a user in its own preferred locale.

Rails I18n ships with the I18n.with_locale method and Translation.io is of course fully compatible with it.

Here is a basic example of usage :

I18n.locale #=> :en

I18n.with_locale(:fr) do
  I18n.locale #=> :fr

I18n.locale #=> :en

And a real world example in a mailer :

class UserMailer < ActionMailer::Base
  default from: 'noreply@translation.io'

  def invitation(user)
    I18n.with_locale(user.locale) do
        :subject => _("You have been invited"),
        :to      => user.email
Published on August 07, 2014
Ready to give it a try? Start your free 7-days trial