Delayed job with i18n on rails 3

I have this task with delayed_job:

def any_method
 UserMailer.delay(queue: "Email", priority: 3).to_user_when_his_account_is_suspended(user, locale)
end

If I send an email in the form of rails:

def any_method
 locale = params[:locale]
 UserMailer.to_user_when_his_account_is_suspended(order, locale).deliver
 #more code
end

The letter is sent in the correct language / language.

However, delayed_job does not recognize the correct language / s. In this case, I get the locale with locale = params[:locale], you can see the following example:

locale = params[:locale]
UserMailer.delay(queue: "Email", priority: 3).to_user_when_his_account_is_suspended(user, locale)

Postal code:

 def to_user_when_his_account_is_suspended(user, locale)
  @user = user
  @locale = locale
  mail(:to => @user.email, :subject => t('.user_account_has_been_suspended'))
 end

How can I fix this problem?

+5
source share
2 answers

Well, firstly, why do I think your “existing” postal code works when it's not in DJ.

I18n.locale. , ... , I18n.locale , Rails .

DJ ... ! , .

:

def to_user_when_his_account_is_suspended(user, locale)
  @user = user
  old_locale = I18n.locale
  I18n.locale = locale
  mail(:to => @user.email, :subject => t('.user_account_has_been_suspended'))
  I18n.locale = old_locale
end

, , , .

, , , .

+1

, , -:

 def to_user_when_his_account_is_suspended(user, locale)
   @user = user
   I18n.with_locale(locale) do
     mail(:to => @user.email, :subject => t('.user_account_has_been_suspended'))
   end
 end
+4

All Articles