A platform for community discussion. Free, open, simple.
FIX: Do not send empty content when detecting language (#34825)
Natalie Tay3d47622d0f69
diff --git a/plugins/discourse-ai/lib/translation/language_detector.rb b/plugins/discourse-ai/lib/translation/language_detector.rbindex 1cba37fc47f..2fb2bfc9a5d 100644--- a/plugins/discourse-ai/lib/translation/language_detector.rb+++ b/plugins/discourse-ai/lib/translation/language_detector.rb@@ -13,6 +13,7 @@ module DiscourseAidef detectreturn nil if !SiteSetting.ai_translation_enabled+ return nil if @text.blank?if (ai_persona = AiPersona.find_by(id: SiteSetting.ai_translation_locale_detector_persona)).blank?diff --git a/plugins/discourse-ai/lib/translation/post_locale_detector.rb b/plugins/discourse-ai/lib/translation/post_locale_detector.rbindex 74f625bf3e0..4338ca61d07 100644--- a/plugins/discourse-ai/lib/translation/post_locale_detector.rb+++ b/plugins/discourse-ai/lib/translation/post_locale_detector.rb@@ -7,8 +7,14 @@ module DiscourseAireturn if post.blank?text = PostDetectionText.get_text(post)- detected_locale = LanguageDetector.new(text, post:).detect- locale = LocaleNormalizer.normalize_to_i18n(detected_locale)++ if text.blank?+ locale = SiteSetting.default_locale+ else+ detected_locale = LanguageDetector.new(text, post:).detect+ locale = LocaleNormalizer.normalize_to_i18n(detected_locale)+ end+post.update_column(:locale, locale)localeenddiff --git a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rbindex 885e4b82fc3..dc2341a050a 100644--- a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb@@ -54,5 +54,13 @@ describe DiscourseAi::Translation::LanguageDetector dolocale_detector.detectendend++ it "skips detection when provided blank text" do+ blank_detector = described_class.new(" ")+ allow(AiPersona).to receive(:find_by).and_call_original++ expect(blank_detector.detect).to eq(nil)+ expect(AiPersona).not_to have_received(:find_by)+ endendenddiff --git a/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rbindex cec8cf572b9..d4d2af6ba1b 100644--- a/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb@@ -25,6 +25,13 @@ describe DiscourseAi::Translation::PostLocaleDetector do)end+ it "returns site default locale if post is empty" do+ post.update_column(:cooked, "")+ expect { described_class.detect_locale(post) }.to change { post.reload.locale }.from(nil).to(+ "en",+ )+ end+it "bypasses validations when updating locale" dopost.update_column(:cooked, "A")