A platform for community discussion. Free, open, simple.
FIX: create system message in user selected locale
Arpit Jalan23fdaf07f24f
diff --git a/lib/system_message.rb b/lib/system_message.rbindex 14b70fcfb9e..f6186d1871d 100644--- a/lib/system_message.rb+++ b/lib/system_message.rb@@ -12,7 +12,8 @@ class SystemMessageenddef self.create_from_system_user(recipient, type, params = {})- self.new(recipient).create_from_system_user(type, params)+ params = params.merge(from_system: true)+ self.new(recipient).create(type, params)enddef initialize(recipient)@@ -21,50 +22,47 @@ class SystemMessagedef create(type, params = {})params = defaults.merge(params)+ from_system = params[:from_system] || false++ title = I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.subject_template", params) }+ raw = I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.text_body_template", params) }- title = I18n.t("system_messages.#{type}.subject_template", params)- raw = I18n.t("system_messages.#{type}.text_body_template", params)+ if from_system+ user = Discourse.system_user+ target_group_names = nil+ else+ user = Discourse.site_contact_user+ target_group_names = Group.exists?(name: SiteSetting.site_contact_group_name) ? SiteSetting.site_contact_group_name : nil+ end- creator = PostCreator.new(Discourse.site_contact_user,+ creator = PostCreator.new(user,title: title,raw: raw,archetype: Archetype.private_message,target_usernames: @recipient.username,- target_group_names: Group.exists?(name: SiteSetting.site_contact_group_name) ? SiteSetting.site_contact_group_name : nil,+ target_group_names: target_group_names,subtype: TopicSubtype.system_message,skip_validations: true)- post = creator.create+ post = I18n.with_locale(@recipient.effective_locale) { creator.create }+if creator.errors.present?raise StandardError, creator.errors.full_messages.join(" ")end- UserArchivedMessage.create!(user: Discourse.site_contact_user, topic: post.topic)+ unless from_system+ UserArchivedMessage.create!(user: Discourse.site_contact_user, topic: post.topic)+ endpostend- def create_from_system_user(type, params = {})- params = defaults.merge(params)-- title = I18n.t("system_messages.#{type}.subject_template", params)- raw = I18n.t("system_messages.#{type}.text_body_template", params)-- PostCreator.create!(Discourse.system_user,- title: title,- raw: raw,- archetype: Archetype.private_message,- target_usernames: @recipient.username,- subtype: TopicSubtype.system_message,- skip_validations: true)- end-def defaults{site_name: SiteSetting.title,username: @recipient.username,user_preferences_url: "#{Discourse.base_url}/u/#{@recipient.username_lower}/preferences",- new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url),+ new_user_tips: I18n.with_locale(@recipient.effective_locale) { I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url) },site_password: "",base_url: Discourse.base_url,}diff --git a/spec/components/system_message_spec.rb b/spec/components/system_message_spec.rbindex b501fc5ebb1..b0c1845e343 100644--- a/spec/components/system_message_spec.rb+++ b/spec/components/system_message_spec.rb@@ -30,6 +30,24 @@ describe SystemMessage doexpect(UserArchivedMessage.where(user_id: admin.id, topic_id: topic.id).length).to eq(1)end+ it 'can create a post from system user in user selected locale' do+ SiteSetting.allow_user_locale = true+ user_de = Fabricate(:user, locale: 'de')+ system_user = Discourse.system_user++ post = SystemMessage.create_from_system_user(user_de, :welcome_invite)+ topic = post.topic++ expect(post.valid?).to eq(true)+ expect(topic).to be_private_message+ expect(topic).to be_valid+ expect(topic.title).to eq(I18n.with_locale(:de) { I18n.t("system_messages.welcome_invite.subject_template", site_name: SiteSetting.title) })+ expect(topic.subtype).to eq(TopicSubtype.system_message)+ expect(topic.allowed_users.include?(user_de)).to eq(true)+ expect(topic.allowed_users.include?(system_user)).to eq(true)+ expect(UserArchivedMessage.where(user_id: system_user.id, topic_id: topic.id).length).to eq(0)+ end+it 'should allow site_contact_group_name' dogroup = Fabricate(:group)SiteSetting.site_contact_group_name = group.name