Skip to content

Commit 21a3e2b

Browse files
committed
FIX: Revert translating raw for translator providers as they strip newlines
1 parent 04e9ac5 commit 21a3e2b

File tree

7 files changed

+62
-33
lines changed

7 files changed

+62
-33
lines changed

app/services/discourse_translator/base.rb

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def self.translate(translatable, target_locale_sym = I18n.locale)
3333
detected_lang = detect(translatable)
3434

3535
if translatable.locale_matches?(target_locale_sym)
36-
return detected_lang, get_untranslated_cooked(translatable)
36+
return detected_lang, get_untranslated(translatable)
3737
end
3838

3939
translation = translatable.translation_for(target_locale_sym)
@@ -50,9 +50,7 @@ def self.translate(translatable, target_locale_sym = I18n.locale)
5050
end
5151

5252
translated = translate!(translatable, target_locale_sym)
53-
save_translation(translatable, target_locale_sym) do
54-
TranslatedContentNormalizer.normalize(translatable, translated)
55-
end
53+
save_translation(translatable, target_locale_sym) { translated }
5654
[detected_lang, translated]
5755
end
5856

@@ -125,27 +123,18 @@ def self.translate_supported?(detected_lang, target_lang)
125123
private
126124

127125
def self.text_for_detection(translatable)
128-
get_untranslated_raw(translatable).truncate(DETECTION_CHAR_LIMIT, omission: nil)
126+
get_untranslated(translatable, raw: true).truncate(DETECTION_CHAR_LIMIT, omission: nil)
129127
end
130128

131-
def self.text_for_translation(translatable)
129+
def self.text_for_translation(translatable, raw: false)
132130
max_char = SiteSetting.max_characters_per_translation
133-
get_untranslated_raw(translatable).truncate(max_char, omission: nil)
134-
end
135-
136-
def self.get_untranslated_raw(translatable)
137-
case translatable.class.name
138-
when "Post"
139-
translatable.raw
140-
when "Topic"
141-
translatable.title
142-
end
131+
get_untranslated(translatable, raw:).truncate(max_char, omission: nil)
143132
end
144133

145-
def self.get_untranslated_cooked(translatable)
134+
def self.get_untranslated(translatable, raw: false)
146135
case translatable.class.name
147136
when "Post"
148-
translatable.cooked
137+
raw ? translatable.raw : translatable.cooked
149138
when "Topic"
150139
translatable.title
151140
end

app/services/discourse_translator/discourse_ai.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ def self.translate!(translatable, target_locale_sym = I18n.locale)
3333
),
3434
)
3535
end
36-
::DiscourseAi::Translator.new(text_for_translation(translatable), target_locale_sym).translate
36+
translated =
37+
::DiscourseAi::Translator.new(
38+
text_for_translation(translatable, raw: true),
39+
target_locale_sym,
40+
).translate
41+
DiscourseTranslator::TranslatedContentNormalizer.normalize(translatable, translated)
3742
end
3843

3944
private

spec/services/amazon_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
describe ".detect" do
2020
let(:post) { Fabricate(:post) }
2121
let!(:client) { Aws::Translate::Client.new(stub_responses: true) }
22-
let(:text) { described_class.truncate(post.raw) }
22+
let(:text) { described_class.truncate(post.cooked) }
2323
let(:detected_lang) { "en" }
2424

2525
before do
@@ -40,8 +40,8 @@
4040
expect(post.detected_locale).to eq(detected_lang)
4141
end
4242

43-
it "should fail graciously when the raw translated text is blank" do
44-
post.raw = ""
43+
it "should fail graciously when the cooked translated text is blank" do
44+
post.cooked = ""
4545
expect(described_class.detect(post)).to be_nil
4646
end
4747
end

spec/services/base_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ class EmptyTranslator < DiscourseTranslator::Base
5252
fab!(:post)
5353

5454
it "truncates to max_characters_per_translation" do
55-
post.raw = "a" * (SiteSetting.max_characters_per_translation + 1)
55+
post.cooked = "a" * (SiteSetting.max_characters_per_translation + 1)
5656
expect(DiscourseTranslator::Base.text_for_translation(post).length).to eq(
5757
SiteSetting.max_characters_per_translation,
5858
)
5959
end
60+
61+
it "uses raw if required" do
62+
post.raw = "a" * (SiteSetting.max_characters_per_translation + 1)
63+
expect(DiscourseTranslator::Base.text_for_translation(post, raw: true).length).to eq(
64+
SiteSetting.max_characters_per_translation,
65+
)
66+
end
6067
end
6168

6269
describe ".detect" do
@@ -93,15 +100,15 @@ class EmptyTranslator < DiscourseTranslator::Base
93100
fab!(:post)
94101

95102
it "returns nil when text is blank" do
96-
post.raw = ""
103+
post.cooked = ""
97104
expect(TestTranslator.translate(post)).to be_nil
98105
end
99106

100107
it "returns original text when detected language matches current locale" do
101108
TestTranslator.save_detected_locale(post) { I18n.locale.to_s }
102-
post.update(raw: "hello")
109+
post.cooked = "hello"
103110

104-
expect(TestTranslator.translate(post)).to eq(%w[en <p>hello</p>])
111+
expect(TestTranslator.translate(post)).to eq(%w[en hello])
105112
end
106113

107114
it "returns cached translation if available" do

spec/services/google_spec.rb

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040

4141
it "should truncate string to 1000 characters" do
4242
length = 2000
43-
post.raw = rand(36**length).to_s(36)
43+
post.cooked = rand(36**length).to_s(36)
4444
detected_lang = "en"
4545

4646
request_url = "#{DiscourseTranslator::Google::DETECT_URI}"
4747
body = {
48-
q: post.raw.truncate(DiscourseTranslator::Google::DETECTION_CHAR_LIMIT, omission: nil),
48+
q: post.cooked.truncate(DiscourseTranslator::Google::DETECTION_CHAR_LIMIT, omission: nil),
4949
key: api_key,
5050
}
5151

@@ -69,6 +69,34 @@
6969

7070
expect(described_class.detect(post)).to eq(detected_lang)
7171
end
72+
73+
it "strips img tags from detection text" do
74+
post.cooked = "there are some words <img src='http://example.com/image.jpg'> to be said"
75+
detected_lang = "en"
76+
77+
request_url = "#{DiscourseTranslator::Google::DETECT_URI}"
78+
body = { q: "there are some words to be said", key: api_key }
79+
80+
Excon
81+
.expects(:post)
82+
.with(
83+
request_url,
84+
body: URI.encode_www_form(body),
85+
headers: {
86+
"Content-Type" => "application/x-www-form-urlencoded",
87+
"Referer" => "http://test.localhost",
88+
},
89+
)
90+
.returns(
91+
mock_response.new(
92+
200,
93+
%{ { "data": { "detections": [ [ { "language": "#{detected_lang}", "isReliable": false, "confidence": 0.18397073 } ] ] } } },
94+
),
95+
)
96+
.once
97+
98+
expect(described_class.detect(post)).to eq(detected_lang)
99+
end
72100
end
73101

74102
describe ".translate_supported?" do
@@ -149,10 +177,10 @@
149177

150178
it "truncates text for translation to max_characters_per_translation setting" do
151179
SiteSetting.max_characters_per_translation = 50
152-
post.raw = "a" * 100
180+
post.cooked = "a" * 100
153181
post.set_detected_locale("de")
154182
body = {
155-
q: post.raw.truncate(SiteSetting.max_characters_per_translation, omission: nil),
183+
q: post.cooked.truncate(SiteSetting.max_characters_per_translation, omission: nil),
156184
source: "de",
157185
target: "en",
158186
key: api_key,

spec/services/libre_translate_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
it "truncates text for translation to max_characters_per_translation setting" do
4545
SiteSetting.max_characters_per_translation = 50
4646
post.set_detected_locale("de")
47-
body = { q: post.raw, source: "de", target: "en", format: "html", api_key: api_key }
47+
body = { q: post.cooked, source: "de", target: "en", format: "html", api_key: api_key }
4848

4949
translated_text = "hur dur hur dur"
5050
# https://publicapi.dev/libre-translate-api

spec/services/microsoft_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def translate_endpoint
176176
it "raises an error if the post is too long to be translated" do
177177
I18n.locale = "ja"
178178
SiteSetting.max_characters_per_translation = 100_000
179-
post.update_columns(raw: "*" * (DiscourseTranslator::Microsoft::LENGTH_LIMIT + 1))
179+
post.update_columns(cooked: "*" * (DiscourseTranslator::Microsoft::LENGTH_LIMIT + 1))
180180

181181
expect { described_class.translate(post) }.to raise_error(
182182
DiscourseTranslator::TranslatorError,
@@ -190,7 +190,7 @@ def translate_endpoint
190190
:post,
191191
"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&textType=html&to=ja",
192192
).with(
193-
body: "[{\"Text\":\"Hello world\"}]",
193+
body: "[{\"Text\":\"\\u003cp\\u003eHello world\\u003c/p\\u003e\"}]",
194194
headers: {
195195
"Ocp-Apim-Subscription-Key" => SiteSetting.translator_azure_subscription_key,
196196
"Content-Type" => "application/json",

0 commit comments

Comments
 (0)