ruby-on-rails – 在轨道上使用i18n延迟工作3
发布时间:2020-12-17 03:15:39 所属栏目:百科 来源:网络整理
导读:我有使用delayed_job的这个任务: def any_method UserMailer.delay(queue: "Email",priority: 3).to_user_when_his_account_is_suspended(user,locale)end 如果我以rails模式发送电子邮件: def any_method locale = params[:locale] UserMailer.to_user_wh
我有使用delayed_job的这个任务:
def any_method UserMailer.delay(queue: "Email",priority: 3).to_user_when_his_account_is_suspended(user,locale) end 如果我以rails模式发送电子邮件: def any_method locale = params[:locale] UserMailer.to_user_when_his_account_is_suspended(order,locale).deliver #more code end 电子邮件以适当的语言环境/语言发送. 但是delayed_job无法识别正确的区域设置/语言.在这种情况下,我得到locale = params [:locale]的语言环境,你可以看到下一个例子: locale = params[:locale] UserMailer.delay(queue: "Email",locale) 邮寄编码: 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 我该如何解决这个问题? 解决方法
好的,首先,为什么我认为你的’现有’邮件代码在不在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 这将暂时改变邮件程序中线程的语言,然后,就像一个好公民一样,它会在请求结束时将其设置回旧值. 显然,如果你在整个地方都这样做,你想把它提取到一个产生控制的辅助方法中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |