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

Commit 35d62a6

Browse files
authored
FIX: Skip edits if localization exists (#1422)
We will fine tune updating an outdated localization in the future. For now we are seeing that quick edits are happening and we need to prevent the job from being too trigger-happy.
1 parent f99309d commit 35d62a6

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

app/jobs/regular/detect_translate_post.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ def execute(args)
2020
end
2121
end
2222

23-
begin
24-
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
25-
rescue FinalDestination::SSRFDetector::LookupFailedError
26-
# this job is non-critical
27-
# the backfill job will handle failures
28-
return
23+
# the user may fill locale in manually
24+
if (detected_locale = post.locale).blank?
25+
begin
26+
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
27+
rescue FinalDestination::SSRFDetector::LookupFailedError
28+
# this job is non-critical
29+
# the backfill job will handle failures
30+
return
31+
end
2932
end
3033

3134
locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
3235
return if locales.blank?
3336

3437
locales.each do |locale|
3538
next if locale == detected_locale
39+
next if post.post_localizations.exists?(locale:)
3640

3741
begin
3842
DiscourseAi::Translation::PostLocalizer.localize(post, locale)

app/jobs/regular/detect_translate_topic.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ def execute(args)
1818
return if topic.category&.read_restricted?
1919
end
2020

21-
begin
22-
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
23-
rescue FinalDestination::SSRFDetector::LookupFailedError
24-
# this job is non-critical
25-
# the backfill job will handle failures
26-
return
21+
if (detected_locale = topic.locale).blank?
22+
begin
23+
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
24+
rescue FinalDestination::SSRFDetector::LookupFailedError
25+
# this job is non-critical
26+
# the backfill job will handle failures
27+
return
28+
end
2729
end
2830

2931
locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
3032
return if locales.blank?
3133

3234
locales.each do |locale|
3335
next if locale == detected_locale
36+
next if topic.topic_localizations.exists?(locale:)
3437

3538
begin
3639
DiscourseAi::Translation::TopicLocalizer.localize(topic, locale)

spec/jobs/regular/detect_translate_post_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
job.execute({ post_id: post.id })
4040
end
4141

42+
it "skips locale detection when post has a locale" do
43+
post.update!(locale: "en")
44+
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
45+
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
46+
47+
job.execute({ post_id: post.id })
48+
end
49+
4250
it "skips bot posts" do
4351
post.update!(user: Discourse.system_user)
4452
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
@@ -56,16 +64,23 @@
5664

5765
it "skips translating to the post's language" do
5866
post.update(locale: "en")
59-
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
6067
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "en").never
6168
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
6269

6370
job.execute({ post_id: post.id })
6471
end
6572

73+
it "skips translating if the post is already localized" do
74+
post.update(locale: "en")
75+
Fabricate(:post_localization, post: post, locale: "ja")
76+
77+
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
78+
79+
job.execute({ post_id: post.id })
80+
end
81+
6682
it "handles translation errors gracefully" do
6783
post.update(locale: "en")
68-
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
6984
DiscourseAi::Translation::PostLocalizer.expects(:localize).raises(
7085
StandardError.new("API error"),
7186
)

spec/jobs/regular/detect_translate_topic_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
job.execute({ topic_id: topic.id })
4040
end
4141

42+
it "skips locale detection when topic has a locale" do
43+
topic.update!(locale: "en")
44+
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).never
45+
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
46+
47+
job.execute({ topic_id: topic.id })
48+
end
49+
4250
it "skips bot topics" do
4351
topic.update!(user: Discourse.system_user)
4452
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
@@ -56,16 +64,22 @@
5664

5765
it "skips translating to the topic's language" do
5866
topic.update(locale: "en")
59-
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
6067
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "en").never
6168
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
6269

6370
job.execute({ topic_id: topic.id })
6471
end
6572

73+
it "skips translating if the topic is already localized" do
74+
topic.update(locale: "en")
75+
Fabricate(:topic_localization, topic:, locale: "ja")
76+
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
77+
78+
job.execute({ topic_id: topic.id })
79+
end
80+
6681
it "handles translation errors gracefully" do
6782
topic.update(locale: "en")
68-
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
6983
DiscourseAi::Translation::TopicLocalizer.expects(:localize).raises(
7084
StandardError.new("API error"),
7185
)

0 commit comments

Comments
 (0)