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.
This commit is contained in:
Natalie Tay 2025-06-11 11:00:22 +08:00 committed by GitHub
parent f99309d1e1
commit 35d62a659b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 16 deletions

View File

@ -20,12 +20,15 @@ module Jobs
end
end
begin
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
rescue FinalDestination::SSRFDetector::LookupFailedError
# this job is non-critical
# the backfill job will handle failures
return
# the user may fill locale in manually
if (detected_locale = post.locale).blank?
begin
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
rescue FinalDestination::SSRFDetector::LookupFailedError
# this job is non-critical
# the backfill job will handle failures
return
end
end
locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
@ -33,6 +36,7 @@ module Jobs
locales.each do |locale|
next if locale == detected_locale
next if post.post_localizations.exists?(locale:)
begin
DiscourseAi::Translation::PostLocalizer.localize(post, locale)

View File

@ -18,12 +18,14 @@ module Jobs
return if topic.category&.read_restricted?
end
begin
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
rescue FinalDestination::SSRFDetector::LookupFailedError
# this job is non-critical
# the backfill job will handle failures
return
if (detected_locale = topic.locale).blank?
begin
detected_locale = DiscourseAi::Translation::TopicLocaleDetector.detect_locale(topic)
rescue FinalDestination::SSRFDetector::LookupFailedError
# this job is non-critical
# the backfill job will handle failures
return
end
end
locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
@ -31,6 +33,7 @@ module Jobs
locales.each do |locale|
next if locale == detected_locale
next if topic.topic_localizations.exists?(locale:)
begin
DiscourseAi::Translation::TopicLocalizer.localize(topic, locale)

View File

@ -39,6 +39,14 @@ describe Jobs::DetectTranslatePost do
job.execute({ post_id: post.id })
end
it "skips locale detection when post has a locale" do
post.update!(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
job.execute({ post_id: post.id })
end
it "skips bot posts" do
post.update!(user: Discourse.system_user)
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
@ -56,16 +64,23 @@ describe Jobs::DetectTranslatePost do
it "skips translating to the post's language" do
post.update(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "en").never
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once
job.execute({ post_id: post.id })
end
it "skips translating if the post is already localized" do
post.update(locale: "en")
Fabricate(:post_localization, post: post, locale: "ja")
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
job.execute({ post_id: post.id })
end
it "handles translation errors gracefully" do
post.update(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).returns("en")
DiscourseAi::Translation::PostLocalizer.expects(:localize).raises(
StandardError.new("API error"),
)

View File

@ -39,6 +39,14 @@ describe Jobs::DetectTranslateTopic do
job.execute({ topic_id: topic.id })
end
it "skips locale detection when topic has a locale" do
topic.update!(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).never
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
job.execute({ topic_id: topic.id })
end
it "skips bot topics" do
topic.update!(user: Discourse.system_user)
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
@ -56,16 +64,22 @@ describe Jobs::DetectTranslateTopic do
it "skips translating to the topic's language" do
topic.update(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "en").never
DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once
job.execute({ topic_id: topic.id })
end
it "skips translating if the topic is already localized" do
topic.update(locale: "en")
Fabricate(:topic_localization, topic:, locale: "ja")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
job.execute({ topic_id: topic.id })
end
it "handles translation errors gracefully" do
topic.update(locale: "en")
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en")
DiscourseAi::Translation::TopicLocalizer.expects(:localize).raises(
StandardError.new("API error"),
)