How can I send emails in Rails 3 using the recipient's locale?

后端 未结 7 1788
说谎
说谎 2021-02-03 23:44

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

7条回答
  •  醉梦人生
    2021-02-04 00:10

    I believe the best way to do this is with the great method I18n.with_locale, it allows you to temporarily change the I18n.locale inside a block, you can use it like this:

    def new_follower(user, follower)
      @follower = follower
      @user = user
      I18n.with_locale(@user.profile.locale) do
        mail to: @user.email
      end
    end
    

    And it'll change the locale just to send the email, immediately changing back after the block ends.

    Source: http://www.rubydoc.info/docs/rails/2.3.8/I18n.with_locale

提交回复
热议问题