FIX: Always use site default locale when creating notes

FIX: Always use site default locale when creating notes

diff --git a/plugin.rb b/plugin.rb
index d5d3a41..a54aee6 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -229,7 +229,9 @@ after_initialize do
     user = User.find_by_id(self.user_id)
     created_by_user = User.find_by_id(self.created_by_id)
     warning_topic = Topic.find_by_id(self.topic_id)
-    raw_note = I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
+    raw_note = I18n.with_locale(SiteSetting.default_locale) do
+      I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
+    end
     ::DiscourseUserNotes.add_note(
       user,
       raw_note,
@@ -242,7 +244,13 @@ after_initialize do
     return unless self.action == UserHistory.actions[:suspend_user]
     target_user = User.find_by_id(self.target_user_id)
     created_by_user = User.find_by_id(self.acting_user_id)
-    raw_note = I18n.t("user_notes.user_suspended", username: created_by_user.username, suspended_till: I18n.l(target_user.suspended_till, format: :date_only), reason: self.details)
+    raw_note = I18n.with_locale(SiteSetting.default_locale) do
+      I18n.t("user_notes.user_suspended",
+        username: created_by_user.username,
+        suspended_till: I18n.l(target_user.suspended_till, format: :date_only),
+        reason: self.details
+      )
+    end
     ::DiscourseUserNotes.add_note(
       target_user,
       raw_note,
@@ -253,12 +261,14 @@ after_initialize do
   end
 
   on(:user_silenced) do |details|
-    raw_note = I18n.t(
-      "user_notes.user_silenced",
-      username: details[:silenced_by]&.username || '',
-      silenced_till: I18n.l(details[:silenced_till], format: :date_only),
-      reason: details[:reason]
-    )
+    raw_note = I18n.with_locale(SiteSetting.default_locale) do
+      I18n.t(
+        "user_notes.user_silenced",
+        username: details[:silenced_by]&.username || '',
+        silenced_till: I18n.l(details[:silenced_till], format: :date_only),
+        reason: details[:reason]
+      )
+    end
     note_args = {}
     if post = Post.with_deleted.where(id: details[:post_id]).first
       note_args = { post_id: post.id, topic_id: post.topic_id }
diff --git a/spec/user_history_spec.rb b/spec/user_history_spec.rb
index 499f27c..34d4e03 100644
--- a/spec/user_history_spec.rb
+++ b/spec/user_history_spec.rb
@@ -17,6 +17,19 @@ describe UserHistory do
 
         expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
       end
+
+      it "should use system language" do
+        freeze_time
+
+        UserHistory.create!(action: UserHistory.actions[:suspend_user], target_user_id: user.id, acting_user_id: admin.id)
+
+        I18n.with_locale(:fr) do # Simulate request from french user
+          UserHistory.create!(action: UserHistory.actions[:suspend_user], target_user_id: user.id, acting_user_id: admin.id)
+        end
+
+        notes = PluginStore.get('user_notes', "notes:#{user.id}")
+        expect(notes[0]['raw']).to eq(notes[1]['raw'])
+      end
     end
   end
 end
diff --git a/spec/user_warning_spec.rb b/spec/user_warning_spec.rb
index 4d70051..bf23d47 100644
--- a/spec/user_warning_spec.rb
+++ b/spec/user_warning_spec.rb
@@ -18,6 +18,20 @@ describe UserWarning do
 
         expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
       end
+
+      it "should use system language" do
+        freeze_time
+
+        warning = UserWarning.create!(topic_id: topic.id, user_id: user.id, created_by_id: admin.id)
+        warning.destroy!
+        
+        I18n.with_locale(:fr) do # Simulate request from french user
+          UserWarning.create(topic_id: topic.id, user_id: user.id, created_by_id: admin.id)
+        end
+
+        notes = PluginStore.get('user_notes', "notes:#{user.id}")
+        expect(notes[0]['raw']).to eq(notes[1]['raw'])
+      end
     end
   end
 end

GitHub sha: ef36fc39

1 Like