From 2c5d9269a0e9e9db3f2429d9e2e3f6cea6d03e51 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Wed, 5 Sep 2018 11:44:26 +0200 Subject: [PATCH] FIX: Notifications shouldn't use user locale unless allow_user_locale is enabled --- app/mailers/user_notifications.rb | 11 ++++++--- spec/mailers/user_notifications_spec.rb | 33 ++++++++++--------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index cde42b3ee6d..76be39f1dee 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -20,10 +20,15 @@ class UserNotifications < ActionMailer::Base end def signup_after_approval(user, opts = {}) + locale = user_locale(user) + tips = I18n.t('system_messages.usage_tips.text_body_template', + base_url: Discourse.base_url, + locale: locale) + build_email(user.email, template: 'user_notifications.signup_after_approval', - locale: user_locale(user), - new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url, locale: locale)) + locale: locale, + new_user_tips: tips) end def notify_old_email(user, opts = {}) @@ -320,7 +325,7 @@ class UserNotifications < ActionMailer::Base protected def user_locale(user) - (user.locale.present? && I18n.available_locales.include?(user.locale.to_sym)) ? user.locale : nil + user.effective_locale end def email_post_markdown(post, add_posted_by = false) diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index a0400e5d331..bd56dbd542e 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -802,12 +802,15 @@ describe UserNotifications do describe "notifications from template" do - context "user locale has been set" do + context "user locale is allowed" do + before do + SiteSetting.default_locale = "en" + SiteSetting.allow_user_locale = true + end %w(signup signup_after_approval confirm_old_email notify_old_email confirm_new_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 @@ -817,29 +820,19 @@ describe UserNotifications do end end - context "user locale has not been set" do + context "user locale is not allowed" do + before do + SiteSetting.default_locale = "en" + SiteSetting.allow_user_locale = false + end + %w(signup signup_after_approval notify_old_email confirm_old_email confirm_new_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(:locale) { "fr" } let(:mail_type) { mail_type } it "sets the locale" do - expects_build_with(has_entry(:locale, nil)) - end - end - end - end - - context "user locale is an empty string" do - %w(signup signup_after_approval notify_old_email confirm_new_email confirm_old_email - forgot_password admin_login account_created).each do |mail_type| - include_examples "notification derived from template" do - SiteSetting.default_locale = "en" - let(:locale) { "" } - let(:mail_type) { mail_type } - it "sets the locale" do - expects_build_with(has_entry(:locale, nil)) + expects_build_with(has_entry(:locale, "en")) end end end