[Feature request][Action Mailer] Add locale support

Instead of wrapping the call to mail in I18n.with_locale inside my mailer methods like this:

I18n.with_locale(@user.locale)
  mail to: @user.email
end

I would like to avoid the wrapping and simply do just this:

mail to: @user.email, locale: @user.locale

or even specify the locale using default like this:

default to: -> { @user.email },  locale: -> { @user.locale }

If you agree this is a good idea, I would be happy to submit a PR.

2 Likes

Currently you can simply do the following.

  before_action do
    if params
      @user = params[:user]
    end
  end

  around_action :with_locale_from_user

  def with_locale_from_user(&block)
    I18n.with_locale(current_locale, &block)
  end

And then to use UserMailer.with(user: user).welcome.deliver_later?

But I agree that it would be nice to reduce this boilerplate.

2 Likes

I have created a PR here: https://github.com/rails/rails/pull/39687

1 Like

I am new to Rails development. Please let me know if I need to do anything else to get this patch accepted. Thanks :slight_smile:

1 Like