From a20a52b25f3671ba2553b72c4f528eb3e3f24aa8 Mon Sep 17 00:00:00 2001 From: scossar Date: Tue, 9 Feb 2016 15:54:13 -0800 Subject: [PATCH] add user locale --- app/mailers/user_notifications.rb | 14 ++++++- lib/email/message_builder.rb | 6 +-- spec/mailers/user_notifications_spec.rb | 51 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 9aca0902eba..81ea701312f 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -11,37 +11,43 @@ class UserNotifications < ActionMailer::Base def signup(user, opts={}) build_email(user.email, template: "user_notifications.signup", + locale: user_locale(user), email_token: opts[:email_token]) end def signup_after_approval(user, opts={}) build_email(user.email, template: 'user_notifications.signup_after_approval', + locale: user_locale(user), 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 def authorize_email(user, opts={}) build_email(user.email, template: "user_notifications.authorize_email", + locale: user_locale(user), email_token: opts[:email_token]) end def forgot_password(user, opts={}) build_email(user.email, template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password", + locale: user_locale(user), email_token: opts[:email_token]) end def admin_login(user, opts={}) build_email(user.email, template: "user_notifications.admin_login", + locale: user_locale(user), email_token: opts[:email_token]) end def account_created(user, opts={}) build_email(user.email, template: "user_notifications.account_created", + locale: user_locale(user), email_token: opts[:email_token]) end @@ -181,6 +187,10 @@ class UserNotifications < ActionMailer::Base protected + def user_locale(user) + user.respond_to?(:locale) ? user.locale : nil + end + def email_post_markdown(post) result = "[email-indent]\n" result << "#{post.raw}\n\n" @@ -264,6 +274,7 @@ class UserNotifications < ActionMailer::Base from_alias = opts[:from_alias] notification_type = opts[:notification_type] user = opts[:user] + locale = user_locale(user) # category name category = Topic.find_by(id: post.topic_id).category @@ -338,6 +349,7 @@ class UserNotifications < ActionMailer::Base site_description: SiteSetting.site_description, site_title: SiteSetting.title, style: :notification, + locale: locale } # If we have a display name, change the from address diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index efa0eeab407..991620e3ad8 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -28,7 +28,7 @@ module Email }.merge!(@opts) 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 @template_args[:respond_instructions] = '' @@ -67,7 +67,7 @@ module Email html_override.gsub!("%{unsubscribe_link}", unsubscribe_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) else html_override.gsub!("%{unsubscribe_via_email_link}", "") @@ -114,7 +114,7 @@ module Email body << "\n" body << I18n.t('unsubscribe_link', template_args) 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 diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 4123b40da14..29466c08c02 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -268,6 +268,15 @@ describe UserNotifications do 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 let(:post) { Fabricate(:post, user: user) } let(:mail_type) { "user_#{notification_type}"} @@ -341,6 +350,7 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :mentioned } include_examples "supports reply by email" + include_examples "sets user locale" end end @@ -348,6 +358,7 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :replied } include_examples "supports reply by email" + include_examples "sets user locale" end end @@ -355,6 +366,7 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :quoted } include_examples "supports reply by email" + include_examples "sets user locale" end end @@ -362,6 +374,7 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :posted } include_examples "supports reply by email" + include_examples "sets user locale" end end @@ -369,6 +382,7 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :invited_to_private_message } include_examples "no reply by email" + include_examples "sets user locale" end end @@ -376,7 +390,44 @@ describe UserNotifications do include_examples "notification email building" do let(:notification_type) { :invited_to_topic } include_examples "no reply by email" + include_examples "sets user locale" 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