Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions app/jobs/regular/detect_translate_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ def execute(args)
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("|")
return if locales.blank?

locales.each do |locale|
next if locale == detected_locale
next if post.post_localizations.exists?(locale:)

begin
DiscourseAi::Translation::PostLocalizer.localize(post, locale)
Expand Down
15 changes: 9 additions & 6 deletions app/jobs/regular/detect_translate_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ def execute(args)
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("|")
return if locales.blank?

locales.each do |locale|
next if locale == detected_locale
next if topic.topic_localizations.exists?(locale:)

begin
DiscourseAi::Translation::TopicLocalizer.localize(topic, locale)
Expand Down
19 changes: 17 additions & 2 deletions spec/jobs/regular/detect_translate_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
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
Expand All @@ -56,16 +64,23 @@

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"),
)
Expand Down
18 changes: 16 additions & 2 deletions spec/jobs/regular/detect_translate_topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
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
Expand All @@ -56,16 +64,22 @@

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"),
)
Expand Down
Loading