Updated solution for Rails 6 (and probably 5).
First, make sure you followed Rails guides on localization, so it's all set up
Then, add this to your ApplicationMailer
:
def default_url_options(options = {})
ActionMailer::Base.default_url_options
.merge(locale: I18n.locale)
.merge(options)
end
This will use your selected locale for links generation, so you don't have to pass locale: @locale
every time.
And then, set current locale in your mailers with the following block.
I18n.with_locale(@locale) do
mail(to: @email,
subject: I18n.t("mailers.my_mailer.subject"))
end
The last piece of advice - don't forget to fallback with your @locale
var, so it's smth along these lines: @locale = @user.locale || I18n.default_locale
Update:
Or you can simply monkeypatch mail
method, but make sure you know what you're doing.
def mail(**)
I18n.with_locale(@locale || I18n.default_locale) { super }
end