ActionMailer deliver_later and i18n in 4.2

128 views
Skip to first unread message

Igor Kapkov

unread,
Oct 22, 2014, 8:31:54 AM10/22/14
to rubyonra...@googlegroups.com
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

Mohamed Wael Khobalatte

unread,
Oct 22, 2014, 11:07:36 AM10/22/14
to rubyonra...@googlegroups.com
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Mohamed Wael Khobalatte

Carlos Antonio da Silva

unread,
Oct 22, 2014, 11:12:24 AM10/22/14
to rubyonra...@googlegroups.com
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.
At.
Carlos Antonio

Mohamed Wael Khobalatte

unread,
Oct 22, 2014, 11:36:20 AM10/22/14
to rubyonra...@googlegroups.com
Ah yes, Carlos is correct. I always explicitly pass the locale to with_locale, so I assumed you are doing the same. 

Igor Kapkov

unread,
Oct 23, 2014, 2:57:23 AM10/23/14
to rubyonra...@googlegroups.com
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 https://github.com/rails/rails/blob/04b40b3debebc24e11a1d9c81ea313125500185b/actionmailer/lib/action_mailer/delivery_job.rb 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.

среда, 22 октября 2014 г., 23:12:24 UTC+8 пользователь Carlos Antonio da Silva написал:

Carlos Antonio da Silva

unread,
Oct 23, 2014, 5:29:34 AM10/23/14
to rubyonra...@googlegroups.com
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.

sija...@gmail.com

unread,
Jul 1, 2015, 9:59:15 AM7/1/15
to rubyonra...@googlegroups.com
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 ;)

You can find it as a gist here: http://git.io/vt9pt

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 :)
Reply all
Reply to author
Forward
0 new messages