Skip to content

A platform for community discussion. Free, open, simple.

DEV: Also pass configured supported locales into the prompt for language detection (#40029)

Natalie Tay7684b418ae29
diff --git a/plugins/discourse-ai/lib/agents/locale_detector.rb b/plugins/discourse-ai/lib/agents/locale_detector.rb
index 387a3440768..69b2a6e2385 100644
--- a/plugins/discourse-ai/lib/agents/locale_detector.rb
+++ b/plugins/discourse-ai/lib/agents/locale_detector.rb
@@ -7,6 +7,8 @@ module DiscourseAi
false
end
+ STATIC_LANGUAGE_CODES = %w[en es fr de it pt-BR ru zh-CN ja ko].freeze
+
def system_prompt
<<~PROMPT.strip
You will be given a piece of text, and your task is to detect the locale (language) of the text and return it in a specific JSON format.
@@ -29,7 +31,7 @@ module DiscourseAi
- Simplified Chinese: zh-CN
- Japanese: ja
- Korean: ko
-
+ #{configured_locale_lines}
If the language is not in this list, use the appropriate IETF language tag code.
5. Avoid using `und` and prefer `en` over `en-US` or `en-GB` unless the text specifically indicates a regional variant.
@@ -53,6 +55,29 @@ module DiscourseAi
def temperature
0
end
+
+ private
+
+ def configured_locale_lines
+ settings =
+ SiteSetting.content_localization_supported_locales.to_s.split("|").reject(&:blank?)
+ return "" if settings.empty?
+
+ configured =
+ settings.filter_map do |locale|
+ lang =
+ LocaleSiteSetting.language_names[locale] ||
+ LocaleSiteSetting.language_names[locale.split("_").first]
+ next if lang.nil? || lang["name"].blank?
+
+ hyphenated = locale.tr("_", "-")
+ next if STATIC_LANGUAGE_CODES.include?(hyphenated)
+
+ "- #{lang["name"]}: #{hyphenated}"
+ end
+
+ configured.empty? ? "" : "#{configured.join("\n")}\n"
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/agents/locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/agents/locale_detector_spec.rb
new file mode 100644
index 00000000000..7e565431f0e
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/agents/locale_detector_spec.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+describe DiscourseAi::Agents::LocaleDetector do
+ describe "#system_prompt" do
+ let(:prompt) { described_class.new.system_prompt }
+
+ context "when content_localization_supported_locales is blank" do
+ before { SiteSetting.content_localization_supported_locales = "" }
+
+ it "leaves the static block intact with no extra entries" do
+ expect(prompt).to include(
+ "- Korean: ko\n\nIf the language is not in this list, use the appropriate IETF language tag code.",
+ )
+ end
+ end
+
+ context "with obscure codes" do
+ before { SiteSetting.content_localization_supported_locales = "nb_NO|zh_TW" }
+
+ it "appends every recognisable code in hyphenated form" do
+ expect(prompt).to include(
+ "- Korean: ko\n- Norwegian Bokmål: nb-NO\n- Chinese: zh-TW\n\nIf the language is not in this list, use the appropriate IETF language tag code.",
+ )
+ expect(prompt).to include("- Norwegian Bokmål: nb-NO")
+ expect(prompt).to include("- Chinese: zh-TW")
+ end
+ end
+
+ context "when supported locales overlap with the static list" do
+ before { SiteSetting.content_localization_supported_locales = "ja" }
+
+ it "does not duplicate codes already listed" do
+ expect(prompt.scan(/^\s*- Japanese: ja\s*$/).size).to eq(1)
+ end
+ end
+ end
+end