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

后端 未结 7 1807
说谎
说谎 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:14

    This answer was a dirty hack that ignored I18n's with_locale method, which is in another answer. The original answer (which works but you shouldn't use it) is below.

    Quick and dirty:

    class SystemMailer < ActionMailer::Base
      def new_follower(user, follower)
        @follower = follower
        @user = user
        using_locale(@user.profile.locale){mail(:to=>@user.email)}
      end
    
      protected
      def using_locale(locale, &block)
        original_locale = I18n.locale
        I18n.locale = locale
        return_value = yield
        I18n.locale = original_locale
        return_value
      end
    end
    

提交回复
热议问题