Make all mailers use appropriate locale when sending emails

This commit is contained in:
luisramos0
2018-12-06 11:36:21 +00:00
parent 3a3c9f3847
commit feea499298
6 changed files with 83 additions and 46 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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