-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for locale (and timezone) #70
Comments
Hmm interesting. I admittedly haven't had the need to send localized emails myself, but if others have this issue it's certainly a concern. That said, I don't love the solution of changing the global state to achieve it, and changing the method signature is undesirable, potentially breaking for users with mail in-queue... So that would have to be managed. Otoh to your point, it stinks if users have to create a locale field to reference for each mailer method that cares of course. This is also Rails-specific and not all users of RM are using Rails, so that's something to consider. Happy to discuss further. Thanks for pointing this out. |
You can automatically pass and restore the current locale https://gist.github.com/glebm/5725347 # pop and set locale from the args before running
module PerformWithLocale
def perform(*args)
I18n.with_locale(args.pop) do
super(*args)
end
end
end
module DeliverWithLocale
def deliver
# @args are not used when rendered inline, only when enqueueing
@args << I18n.locale.to_s
super
end
end
#= resque_mailer locale support
if defined?(Resque::Mailer)
module Resque::Mailer
# push locale onto the args when queueing
MessageDecoy.send :prepend, DeliverWithLocale
# pop when dequeueing
ClassMethods.send :prepend, PerformWithLocale
end
end |
I've used your gem in an existing project which send localized emails and on first deploy I got some issues due to missing locale support. Here's my use case, I have a mailer which uses layouts and those layouts shows localized links something like
I was sending messages directly in the controller and links and other translated stuff is localized correctly since in controller
I18n.locale
is set according to user locale. When I moved to resque_mailer obviously that code has stopped to work.In my project I was able to retrieve user locale from the mailer and I've solved with this code in my mailer, but this is not always possible.
The same issue can appear if mail content is timezone dependent (wasn't my case) so this should be handled by gem.
This could be solved with something like
This however would change main methods signatures, so it would need some specs.
What do you think? Are you interested in this kind of PR? Otherwise you should mention this issue in readme.
The text was updated successfully, but these errors were encountered: