ActionMailer deliver_later and i18n in 4.2

Hi all,

previously if we needed in sending a mail in different locale we used I18n.with_locale. I didn’t find any about that in sources and docs/guides. How it’ll work with deliver_later and ActiveJob?

Best Regards,

Igas

There shouldn’t be any problem since deliver_later will simply invoke your deliver_now at some point, and that should run as you expect.

You should probably be passing in the locale you want as an argument to your mailers, and set the locale there before actually creating the mail object/rendering the view.

Ah yes, Carlos is correct. I always explicitly pass the locale to with_locale, so I assumed you are doing the same.

Yeah, I understand how to do it now, but don’t understand how it’ll work in 4.2. Let’s look at example. I send confirmation instructions UserMailer.confirmation(@user).deliver_later and I have 2 views app/views/user_mailer/confirmation.en.html.erb & app/views/user_mailer/confirmation.ru.html.erb. And deliver_later just add the job rails/delivery_job.rb at 04b40b3debebc24e11a1d9c81ea313125500185b · rails/rails · GitHub but this job wont know nothing about user locale.

I’m not asking for help how to solve this, I just worried that this scenario currently not mentioned or solved as I see.

Rather than doing something like:

I18n.with_locale(:en) { Mailer.zomg(foo, bar).deliver_later }

You can do

Mailer.zomg(:en foo, bar).deliver_later

And within the mailer

def zomg(locale, foo, bar)

I18n.with_locale(locale) do

end

end

Right now that’s the simplest thing that I can think of, and it’s so simple that I’m not even sure Rails should have any special handling for that - but maybe it should.

Hi!

I had the same problem and what I’ve managed to come up with is hackish, but transparent solution.

This way you can use ActiveMailer’s #deliver_later and it’s hashbang friend without worrying about passing locale yourself.

So much better to leave grizzly implementation details out of the sight :wink:

You can find it as a gist here: Make Rails' 4.2 ActionMailer locale aware or i18n for ya emailz · GitHub

Best regards,

Sija

PS. If someone would come up with similar solution, but applied instead on the level of ActiveJob I’d be happy to hear about it :slight_smile: