FIX: TL2 promotion message from discobot didn't use user locale

FIX: TL2 promotion message from discobot didn’t use user locale

Includes a small refactor/fix, because return isn’t allowed in blocks.

diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index f66219c..7f3fde2 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -300,15 +300,17 @@ after_initialize do
   )
 
   self.on(:system_message_sent) do |args|
-    if args[:message_type] == 'tl2_promotion_message' && SiteSetting.discourse_narrative_bot_enabled
+    next if !SiteSetting.discourse_narrative_bot_enabled
+    next if args[:message_type] != 'tl2_promotion_message'
 
-      raw = I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template",
-                  discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
-                  reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
+    recipient = args[:post].topic.topic_users.where.not(user_id: args[:post].user_id).last&.user
+    recipient ||= Discourse.site_contact_user if args[:post].user == Discourse.site_contact_user
+    next if recipient.nil?
 
-      recipient = args[:post].topic.topic_users.where.not(user_id: args[:post].user_id).last&.user
-      recipient ||= Discourse.site_contact_user if args[:post].user == Discourse.site_contact_user
-      return if recipient.nil?
+    I18n.with_locale(recipient.effective_locale) do
+      raw = I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template",
+                   discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
+                   reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
 
       PostCreator.create!(
         ::DiscourseNarrativeBot::Base.new.discobot_user,
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
index 4774223..c7fa4b7 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
@@ -757,4 +757,20 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
     expect(Topic.last.title).to eq(I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"))
     expect(Topic.last.topic_users.map(&:user_id).sort).to eq([DiscourseNarrativeBot::Base.new.discobot_user.id, recipient.id])
   end
+
+  it "invites to advanced training using the user's effective locale" do
+    SiteSetting.allow_user_locale = true
+    recipient = Fabricate(:user, locale: "de")
+
+    TranslationOverride.upsert!("de", 'discourse_narrative_bot.tl2_promotion_message.subject_template', 'german title')
+    TranslationOverride.upsert!("de", 'discourse_narrative_bot.tl2_promotion_message.text_body_template', 'german body')
+
+    expect {
+      DiscourseEvent.trigger(:system_message_sent, post: Post.last, message_type: 'tl2_promotion_message')
+    }.to change { Topic.count }
+
+    topic = Topic.last
+    expect(topic.title).to eq("german title")
+    expect(topic.first_post.raw).to eq("german body")
+  end
 end

GitHub sha: ae744693

This commit appears in #13203 which was approved by tgxworld. It was merged by tgxworld.