Skip to content

Commit 5c4de36

Browse files
authored
FIX: Include some of the post content when detecting the title language (#277)
This commit appends some of the first post's text to the topic title when detecting the language. This modification does not affect the text that is sent for actual translation.
1 parent 7ebee97 commit 5c4de36

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

app/services/discourse_translator/base.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,16 @@ def self.translate_supported?(detected_lang, target_lang)
123123
private
124124

125125
def self.text_for_detection(translatable)
126-
get_untranslated(translatable, raw: true).truncate(DETECTION_CHAR_LIMIT, omission: nil)
126+
text = get_untranslated(translatable, raw: true)
127+
128+
if translatable.class.name == "Topic"
129+
# due to topics having short titles,
130+
# we need to add the first post to the detection text
131+
first_post = get_untranslated(translatable.first_post, raw: true)
132+
text = text + " " + first_post if first_post
133+
end
134+
135+
text.truncate(DETECTION_CHAR_LIMIT, omission: nil)
127136
end
128137

129138
def self.text_for_translation(translatable, raw: false)

spec/services/base_spec.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class EmptyTranslator < DiscourseTranslator::Base
3434
end
3535

3636
describe ".text_for_detection" do
37-
fab!(:post)
37+
fab!(:topic) { Fabricate(:topic, title: "it is a fine day") }
38+
fab!(:post) { Fabricate(:post, topic:) }
3839

3940
it "truncates to DETECTION_CHAR_LIMIT of 1000" do
4041
post.raw = "a" * 1001
@@ -46,6 +47,12 @@ class EmptyTranslator < DiscourseTranslator::Base
4647
post.raw = text
4748
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq(text)
4849
end
50+
51+
it "appends some text from the first post for topics" do
52+
topic.first_post.raw = "a" * 999
53+
expected = (topic.title + " " + topic.first_post.raw).truncate(1000)
54+
expect(DiscourseTranslator::Base.text_for_detection(topic)).to eq(expected)
55+
end
4956
end
5057

5158
describe ".text_for_translation" do

0 commit comments

Comments
 (0)