Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit eaab58e

Browse files
committed
add ai_translation_max_post_length
1 parent dc9286d commit eaab58e

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

config/locales/server.en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ en:
118118
ai_discord_allowed_guilds: "Discord guilds (servers) where the bot is allowed to search"
119119
ai_bot_enable_dedicated_ux: "Allow for full screen bot interface, instead of a PM"
120120

121+
ai_translation_enabled: "Enables the AI translation feature"
122+
ai_translation_model: "The model to use for translation. This model must support translation. Personas can override this setting."
123+
ai_translation_backfill_limit_to_public_content: "When enabled, only content in public categories will be translated. When disabled, content in group PMs and private categories will also be sent for translation."
124+
ai_translation_max_post_length: "The maximum length of a post to be translated. Posts longer than this will not be translated."
125+
ai_translation_backfill_max_age_days: "The maximum age of a post and topic to be translated. Posts and topics older than this will not be translated."
126+
121127
reviewables:
122128
reasons:
123129
flagged_by_toxicity: The AI plugin flagged this after classifying it as toxic.

config/settings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ discourse_ai:
491491
default: true
492492
client: false
493493
area: "ai-features/translation"
494+
ai_translation_max_post_length:
495+
default: 1000
496+
client: false
497+
area: "ai-features/translation"
494498
ai_translation_backfill_max_age_days:
495499
default: 5
496500
client: false

lib/translation/post_localizer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module DiscourseAi
44
module Translation
55
class PostLocalizer
66
def self.localize(post, target_locale = I18n.locale)
7-
return if post.blank? || target_locale.blank? || post.locale == target_locale.to_s
7+
if post.blank? || target_locale.blank? || post.locale == target_locale.to_s ||
8+
post.raw.blank?
9+
return
10+
end
11+
return if post.raw.length > SiteSetting.ai_translation_max_post_length
812
target_locale = target_locale.to_s.sub("-", "_")
913

1014
translated_raw =

spec/lib/translation/post_localizer_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def post_raw_translator_stub(opts)
1818
allow(mock).to receive(:translate).and_return(opts[:translated])
1919
end
2020

21-
it "returns nil if post is blank" do
21+
it "returns nil if post does not exist" do
2222
expect(described_class.localize(nil, "ja")).to eq(nil)
2323
end
2424

@@ -33,6 +33,19 @@ def post_raw_translator_stub(opts)
3333
expect(described_class.localize(post, "en")).to eq(nil)
3434
end
3535

36+
it "returns nil if post raw is blank" do
37+
post.raw = ""
38+
39+
expect(described_class.localize(post, "ja")).to eq(nil)
40+
end
41+
42+
it "returns nil if post raw is too long" do
43+
SiteSetting.ai_translation_max_post_length = 10
44+
post.raw = "This is a very long post that exceeds the limit."
45+
46+
expect(described_class.localize(post, "ja")).to eq(nil)
47+
end
48+
3649
it "translates with post and locale" do
3750
post_raw_translator_stub({ text: post.raw, target_locale: "ja", translated: translated_raw })
3851

0 commit comments

Comments
 (0)