A platform for community discussion. Free, open, simple.
FIX: Strip trailing backslash from detected locale (#42887)
Natalie Taydf2563f91b59
diff --git a/plugins/discourse-ai/lib/translation/language_detector.rb b/plugins/discourse-ai/lib/translation/language_detector.rbindex 517f42581e7..a1cd100d58d 100644--- a/plugins/discourse-ai/lib/translation/language_detector.rb+++ b/plugins/discourse-ai/lib/translation/language_detector.rb@@ -53,6 +53,7 @@ module DiscourseAiresult << partialend+ result = result.strip.delete_suffix("\\")result.match?(LANGUAGE_TAG_REGEXP) ? result : nilendenddiff --git a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rbindex 435b1952df8..98807bf93e9 100644--- a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb@@ -62,6 +62,22 @@ describe DiscourseAi::Translation::LanguageDetector doendend+ it "returns the language when the streamed response has a trailing backslash" do+ bot = instance_double(DiscourseAi::Agents::Bot)+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(bot)+ allow(bot).to receive(:reply) { |_, &blk| blk.call("nl\\") }++ expect(locale_detector.detect).to eq("nl")+ end++ it "returns the language when the streamed response has surrounding whitespace" do+ bot = instance_double(DiscourseAi::Agents::Bot)+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(bot)+ allow(bot).to receive(:reply) { |_, &blk| blk.call(" nl\n") }++ expect(locale_detector.detect).to eq("nl")+ end+it "skips detection when provided blank text" doblank_detector = described_class.new(" ")allow(AiAgent).to receive(:find_by).and_call_originaldiff --git a/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rbindex 37ac2f7643a..4aaea8facaa 100644--- a/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb@@ -34,6 +34,18 @@ describe DiscourseAi::Translation::TopicLocaleDetector do).to("zh_CN")end+ it "updates the topic locale when the streamed detector response has a trailing backslash" do+ assign_fake_provider_to(:ai_default_llm_model)+ SiteSetting.ai_translation_enabled = true+ bot = instance_double(DiscourseAi::Agents::Bot)+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(bot)+ allow(bot).to receive(:reply) { |_, &blk| blk.call("nl\\") }++ expect { described_class.detect_locale(topic) }.to change { topic.reload.locale }.from(+ nil,+ ).to("nl")+ end+it "bypasses validations when updating locale" dotopic.update_column(:title, "A")SiteSetting.min_topic_title_length = 15