From feea499298c68cde233da372d0350db664172430 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 6 Dec 2018 11:36:21 +0000 Subject: [PATCH] Make all mailers use appropriate locale when sending emails --- app/helpers/i18n_helper.rb | 16 ++++++- app/mailers/enterprise_mailer.rb | 26 ++++++----- app/mailers/producer_mailer.rb | 7 +-- app/mailers/spree/order_mailer_decorator.rb | 49 ++++++++++++--------- app/mailers/spree/user_mailer_decorator.rb | 18 +++++--- app/mailers/subscription_mailer.rb | 13 +++--- 6 files changed, 83 insertions(+), 46 deletions(-) diff --git a/app/helpers/i18n_helper.rb b/app/helpers/i18n_helper.rb index 4c93bdc0bc..6ed767d675 100644 --- a/app/helpers/i18n_helper.rb +++ b/app/helpers/i18n_helper.rb @@ -1,7 +1,7 @@ module I18nHelper def set_locale # Save a given locale - if params[:locale] && Rails.application.config.i18n.available_locales.include?(params[:locale]) + if params[:locale] && available_locale?(params[:locale]) spree_current_user.update_attributes!(locale: params[:locale]) if spree_current_user cookies[:locale] = params[:locale] end @@ -13,4 +13,18 @@ module I18nHelper I18n.locale = spree_current_user.andand.locale || cookies[:locale] || I18n.default_locale end + + def valid_locale(locale) + if available_locale?(locale) + locale + else + I18n.default_locale + end + end + + private + + def available_locale?(locale) + Rails.application.config.i18n.available_locales.include?(locale) + end end diff --git a/app/mailers/enterprise_mailer.rb b/app/mailers/enterprise_mailer.rb index 03d9279c93..f8ddbe970e 100644 --- a/app/mailers/enterprise_mailer.rb +++ b/app/mailers/enterprise_mailer.rb @@ -1,15 +1,18 @@ require 'devise/mailers/helpers' class EnterpriseMailer < Spree::BaseMailer include Devise::Mailers::Helpers + include I18nHelper def welcome(enterprise) @enterprise = enterprise - subject = t('enterprise_mailer.welcome.subject', - enterprise: @enterprise.name, - sitename: Spree::Config[:site_name]) - mail(:to => enterprise.contact.email, - :from => from_address, - :subject => subject) + I18n.with_locale valid_locale(@enterprise.owner.locale) do + subject = t('enterprise_mailer.welcome.subject', + enterprise: @enterprise.name, + sitename: Spree::Config[:site_name]) + mail(:to => enterprise.contact.email, + :from => from_address, + :subject => subject) + end end def manager_invitation(enterprise, user) @@ -17,11 +20,12 @@ class EnterpriseMailer < Spree::BaseMailer @instance = Spree::Config[:site_name] @instance_email = from_address - subject = t('enterprise_mailer.invite_manager.subject', enterprise: @enterprise.name) - - mail(to: user.email, - from: from_address, - subject: subject) + I18n.with_locale valid_locale(@enterprise.owner.locale) do + subject = t('enterprise_mailer.invite_manager.subject', enterprise: @enterprise.name) + mail(to: user.email, + from: from_address, + subject: subject) + end end private diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index ad7569dad2..32338c1379 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -1,4 +1,5 @@ class ProducerMailer < Spree::BaseMailer + include I18nHelper def order_cycle_report(producer, order_cycle) @producer = producer @@ -10,9 +11,10 @@ class ProducerMailer < Spree::BaseMailer @total = total_from_line_items(line_items) @tax_total = tax_total_from_line_items(line_items) - subject = "[#{Spree::Config.site_name}] #{I18n.t('producer_mailer.order_cycle.subject', producer: producer.name)}" + I18n.with_locale valid_locale(@producer.owner.locale) do + subject = "[#{Spree::Config.site_name}] #{I18n.t('producer_mailer.order_cycle.subject', producer: producer.name)}" - if has_orders?(order_cycle, producer) + return unless has_orders?(order_cycle, producer) mail( to: @producer.contact.email, from: from_address, @@ -23,7 +25,6 @@ class ProducerMailer < Spree::BaseMailer end end - private def has_orders?(order_cycle, producer) diff --git a/app/mailers/spree/order_mailer_decorator.rb b/app/mailers/spree/order_mailer_decorator.rb index a4b2db8b70..25ea4a51da 100644 --- a/app/mailers/spree/order_mailer_decorator.rb +++ b/app/mailers/spree/order_mailer_decorator.rb @@ -2,41 +2,50 @@ Spree::OrderMailer.class_eval do helper HtmlHelper helper CheckoutHelper helper SpreeCurrencyHelper + include I18nHelper def cancel_email(order, resend = false) @order = find_order(order) - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.cancel_email.subject')} ##{order.number}" - mail(to: order.email, from: from_address, subject: subject) + I18n.with_locale valid_locale(@order.user.locale) do + subject = (resend ? "[#{t(:resend).upcase}] " : '') + subject += "#{Spree::Config[:site_name]} #{t('order_mailer.cancel_email.subject')} ##{order.number}" + mail(to: order.email, from: from_address, subject: subject) + end end def confirm_email_for_customer(order, resend = false) find_order(order) # Finds an order instance from an id - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" - mail(:to => @order.email, - :from => from_address, - :subject => subject, - :reply_to => @order.distributor.contact.email) + I18n.with_locale valid_locale(@order.user.locale) do + subject = (resend ? "[#{t(:resend).upcase}] " : '') + subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" + mail(:to => @order.email, + :from => from_address, + :subject => subject, + :reply_to => @order.distributor.contact.email) + end end def confirm_email_for_shop(order, resend = false) find_order(order) # Finds an order instance from an id - subject = (resend ? "[#{t(:resend).upcase}] " : '') - subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" - mail(:to => @order.distributor.contact.email, - :from => from_address, - :subject => subject) + I18n.with_locale valid_locale(@order.user.locale) do + subject = (resend ? "[#{t(:resend).upcase}] " : '') + subject += "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{@order.number}" + mail(:to => @order.distributor.contact.email, + :from => from_address, + :subject => subject) + end end def invoice_email(order, pdf) find_order(order) # Finds an order instance from an id - attachments["invoice-#{@order.number}.pdf"] = pdf if pdf.present? - subject = "#{Spree::Config[:site_name]} #{t(:invoice)} ##{@order.number}" - mail(:to => @order.email, - :from => from_address, - :subject => subject, - :reply_to => @order.distributor.contact.email) + I18n.with_locale valid_locale(@order.user.locale) do + attachments["invoice-#{@order.number}.pdf"] = pdf if pdf.present? + subject = "#{Spree::Config[:site_name]} #{t(:invoice)} ##{@order.number}" + mail(:to => @order.email, + :from => from_address, + :subject => subject, + :reply_to => @order.distributor.contact.email) + end end def find_order(order) diff --git a/app/mailers/spree/user_mailer_decorator.rb b/app/mailers/spree/user_mailer_decorator.rb index dced31e7fd..7b7cb38122 100644 --- a/app/mailers/spree/user_mailer_decorator.rb +++ b/app/mailers/spree/user_mailer_decorator.rb @@ -1,8 +1,12 @@ Spree::UserMailer.class_eval do + include I18nHelper + def signup_confirmation(user) @user = user - mail(:to => user.email, :from => from_address, - :subject => t(:welcome_to) + Spree::Config[:site_name]) + I18n.with_locale valid_locale(@user.locale) do + mail(:to => user.email, :from => from_address, + :subject => t(:welcome_to) + Spree::Config[:site_name]) + end end # Overriding `Spree::UserMailer.confirmation_instructions` which is @@ -12,10 +16,12 @@ Spree::UserMailer.class_eval do @instance = Spree::Config[:site_name] @contact = ContentConfig.footer_email - subject = t('spree.user_mailer.confirmation_instructions.subject') - mail(to: confirmation_email_address, - from: from_address, - subject: subject) + I18n.with_locale valid_locale(@user.locale) do + subject = t('spree.user_mailer.confirmation_instructions.subject') + mail(to: confirmation_email_address, + from: from_address, + subject: subject) + end end private diff --git a/app/mailers/subscription_mailer.rb b/app/mailers/subscription_mailer.rb index 426d94f847..448783ceda 100644 --- a/app/mailers/subscription_mailer.rb +++ b/app/mailers/subscription_mailer.rb @@ -1,6 +1,7 @@ class SubscriptionMailer < Spree::BaseMailer helper CheckoutHelper helper ShopMailHelper + include I18nHelper def confirmation_email(order) @type = 'confirmation' @@ -46,10 +47,12 @@ class SubscriptionMailer < Spree::BaseMailer private def send_mail(order) - subject = "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{order.number}" - mail(to: order.email, - from: from_address, - subject: subject, - reply_to: order.distributor.contact.email) + I18n.with_locale valid_locale(order.user.locale) do + subject = "#{Spree::Config[:site_name]} #{t('order_mailer.confirm_email.subject')} ##{order.number}" + mail(to: order.email, + from: from_address, + subject: subject, + reply_to: order.distributor.contact.email) + end end end