views:

128

answers:

2

How can I send mails in a mailer using the recipient's locale. I have the preferred locale for each user in the database. Notice this is different from the current locale (I18n.locale), as long as the current user doesn't have to be the recipient. So the difficult thing is to use the mailer in a different locale without changing I18n.locale:

def new_follower(user, follower)
  @follower = follower
  @user = user
  mail :to=>@user.email
end

Using I18n.locale = @user.profile.locale before mail :to=>... would solve the mailer issue, but would change the behaviour in the rest of the thread.

+1  A: 

This simple plugin was developed for rails 2 but seems to work in rails 3 too.

http://github.com/Bertg/i18n_action_mailer

With it you can do the following:

def new_follower(user, follower)
  @follower = follower
  @user = user
  set_locale user.locale
  mail :to => @user.email, :subject => t(:new_follower_subject)
end

The subject and mail templates are then translated using the user's locale.

Kusti
Thanks! But which plugin do you mean? :)
Jose
Oops, forgot the link! :D Now it's there, it's i18n_action_mailer plugin.
Kusti
Unfortunately, it doesn't fill my purpose, as I have different views for each locale: welcome.es.erb, welcome.en.erb, etc. The plugin only overloads t and l methods.
Jose
+1  A: 

Here's an updated version that also supports the '.key' short-hand notation, so you don't have to spell out each key in its entirety.

http://github.com/larspind/i18n_action_mailer

Lars Pind
Thanks for the plugin. However, it still fails to select the appropriate view according to the locale under Rails 3
Jose