add user locale

This commit is contained in:
scossar 2016-02-09 15:54:13 -08:00
parent 7ddb7ff429
commit a20a52b25f
3 changed files with 67 additions and 4 deletions

View file

@ -11,37 +11,43 @@ class UserNotifications < ActionMailer::Base
def signup(user, opts={}) def signup(user, opts={})
build_email(user.email, build_email(user.email,
template: "user_notifications.signup", template: "user_notifications.signup",
locale: user_locale(user),
email_token: opts[:email_token]) email_token: opts[:email_token])
end end
def signup_after_approval(user, opts={}) def signup_after_approval(user, opts={})
build_email(user.email, build_email(user.email,
template: 'user_notifications.signup_after_approval', template: 'user_notifications.signup_after_approval',
locale: user_locale(user),
email_token: opts[:email_token], email_token: opts[:email_token],
new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url)) new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url, locale: locale))
end end
def authorize_email(user, opts={}) def authorize_email(user, opts={})
build_email(user.email, build_email(user.email,
template: "user_notifications.authorize_email", template: "user_notifications.authorize_email",
locale: user_locale(user),
email_token: opts[:email_token]) email_token: opts[:email_token])
end end
def forgot_password(user, opts={}) def forgot_password(user, opts={})
build_email(user.email, build_email(user.email,
template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password", template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password",
locale: user_locale(user),
email_token: opts[:email_token]) email_token: opts[:email_token])
end end
def admin_login(user, opts={}) def admin_login(user, opts={})
build_email(user.email, build_email(user.email,
template: "user_notifications.admin_login", template: "user_notifications.admin_login",
locale: user_locale(user),
email_token: opts[:email_token]) email_token: opts[:email_token])
end end
def account_created(user, opts={}) def account_created(user, opts={})
build_email(user.email, build_email(user.email,
template: "user_notifications.account_created", template: "user_notifications.account_created",
locale: user_locale(user),
email_token: opts[:email_token]) email_token: opts[:email_token])
end end
@ -181,6 +187,10 @@ class UserNotifications < ActionMailer::Base
protected protected
def user_locale(user)
user.respond_to?(:locale) ? user.locale : nil
end
def email_post_markdown(post) def email_post_markdown(post)
result = "[email-indent]\n" result = "[email-indent]\n"
result << "#{post.raw}\n\n" result << "#{post.raw}\n\n"
@ -264,6 +274,7 @@ class UserNotifications < ActionMailer::Base
from_alias = opts[:from_alias] from_alias = opts[:from_alias]
notification_type = opts[:notification_type] notification_type = opts[:notification_type]
user = opts[:user] user = opts[:user]
locale = user_locale(user)
# category name # category name
category = Topic.find_by(id: post.topic_id).category category = Topic.find_by(id: post.topic_id).category
@ -338,6 +349,7 @@ class UserNotifications < ActionMailer::Base
site_description: SiteSetting.site_description, site_description: SiteSetting.site_description,
site_title: SiteSetting.title, site_title: SiteSetting.title,
style: :notification, style: :notification,
locale: locale
} }
# If we have a display name, change the from address # If we have a display name, change the from address

View file

@ -28,7 +28,7 @@ module Email
}.merge!(@opts) }.merge!(@opts)
if @template_args[:url].present? if @template_args[:url].present?
@template_args[:header_instructions] = I18n.t('user_notifications.header_instructions') @template_args[:header_instructions] = I18n.t('user_notifications.header_instructions', locale: @opts[:locale])
if @opts[:include_respond_instructions] == false if @opts[:include_respond_instructions] == false
@template_args[:respond_instructions] = '' @template_args[:respond_instructions] = ''
@ -67,7 +67,7 @@ module Email
html_override.gsub!("%{unsubscribe_link}", unsubscribe_link) html_override.gsub!("%{unsubscribe_link}", unsubscribe_link)
if SiteSetting.unsubscribe_via_email_footer && @opts[:add_unsubscribe_via_email_link] if SiteSetting.unsubscribe_via_email_footer && @opts[:add_unsubscribe_via_email_link]
unsubscribe_via_email_link = PrettyText.cook(I18n.t('unsubscribe_via_email_link', hostname: Discourse.current_hostname), sanitize: false).html_safe unsubscribe_via_email_link = PrettyText.cook(I18n.t('unsubscribe_via_email_link', hostname: Discourse.current_hostname, locale: @opts[:locale]), sanitize: false).html_safe
html_override.gsub!("%{unsubscribe_via_email_link}", unsubscribe_via_email_link) html_override.gsub!("%{unsubscribe_via_email_link}", unsubscribe_via_email_link)
else else
html_override.gsub!("%{unsubscribe_via_email_link}", "") html_override.gsub!("%{unsubscribe_via_email_link}", "")
@ -114,7 +114,7 @@ module Email
body << "\n" body << "\n"
body << I18n.t('unsubscribe_link', template_args) body << I18n.t('unsubscribe_link', template_args)
if SiteSetting.unsubscribe_via_email_footer && @opts[:add_unsubscribe_via_email_link] if SiteSetting.unsubscribe_via_email_footer && @opts[:add_unsubscribe_via_email_link]
body << I18n.t('unsubscribe_via_email_link', hostname: Discourse.current_hostname) body << I18n.t('unsubscribe_via_email_link', hostname: Discourse.current_hostname, locale: @opts[:locale])
end end
end end

View file

@ -268,6 +268,15 @@ describe UserNotifications do
end end
end end
# The parts of emails that are derived from templates are translated
shared_examples "sets user locale" do
context "set locale for translating templates" do
it "sets the locale" do
expects_build_with(has_key(:locale))
end
end
end
shared_examples "notification email building" do shared_examples "notification email building" do
let(:post) { Fabricate(:post, user: user) } let(:post) { Fabricate(:post, user: user) }
let(:mail_type) { "user_#{notification_type}"} let(:mail_type) { "user_#{notification_type}"}
@ -341,6 +350,7 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :mentioned } let(:notification_type) { :mentioned }
include_examples "supports reply by email" include_examples "supports reply by email"
include_examples "sets user locale"
end end
end end
@ -348,6 +358,7 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :replied } let(:notification_type) { :replied }
include_examples "supports reply by email" include_examples "supports reply by email"
include_examples "sets user locale"
end end
end end
@ -355,6 +366,7 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :quoted } let(:notification_type) { :quoted }
include_examples "supports reply by email" include_examples "supports reply by email"
include_examples "sets user locale"
end end
end end
@ -362,6 +374,7 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :posted } let(:notification_type) { :posted }
include_examples "supports reply by email" include_examples "supports reply by email"
include_examples "sets user locale"
end end
end end
@ -369,6 +382,7 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :invited_to_private_message } let(:notification_type) { :invited_to_private_message }
include_examples "no reply by email" include_examples "no reply by email"
include_examples "sets user locale"
end end
end end
@ -376,7 +390,44 @@ describe UserNotifications do
include_examples "notification email building" do include_examples "notification email building" do
let(:notification_type) { :invited_to_topic } let(:notification_type) { :invited_to_topic }
include_examples "no reply by email" include_examples "no reply by email"
include_examples "sets user locale"
end end
end end
# notification emails derived from templates are translated into the user's locale
shared_examples "notification derived from template" do
let(:user) { Fabricate(:user, locale: locale) }
let(:mail_type) { mail_type }
let(:notification) { Fabricate(:notification, user: user) }
end
describe "notifications from template" do
context "user locale has been set" do
%w(signup signup_after_approval authorize_email forgot_password admin_login account_created).each do |mail_type|
include_examples "notification derived from template" do
SiteSetting.default_locale = "en"
let(:locale) { "fr" }
let(:mail_type) { mail_type }
it "sets the locale" do
expects_build_with(has_entry(:locale, "fr"))
end
end
end
end
context "user locale has not been set" do
%w(signup signup_after_approval authorize_email forgot_password admin_login account_created).each do |mail_type|
include_examples "notification derived from template" do
SiteSetting.default_locale = "en"
let(:locale) { nil }
let(:mail_type) { mail_type }
it "sets the locale" do
expects_build_with(has_entry(:locale, nil))
end
end
end
end
end
end end