A platform for community discussion. Free, open, simple.
FEATURE: Allow re-localization twice a day if post version has changed (#34023)
Natalie Tayb7d7f99c046e
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex 84b93a0ae28..7b55ee228d6 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -41,21 +41,32 @@ module Jobslocales.each do |locale|next if LocaleNormalizer.is_same?(locale, detected_locale)+regionless_locale = locale.split("_").first- next if post.post_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists?+ exists = post.post_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists?- begin- DiscourseAi::Translation::PostLocalizer.localize(post, locale)- rescue FinalDestination::SSRFDetector::LookupFailedError- # do nothing, there are too many sporadic lookup failures- rescue => e- DiscourseAi::Translation::VerboseLogger.log(- "Failed to translate post #{post.id} to #{locale}: #{e.message}\n\n#{e.backtrace[0..3].join("\n")}",- )+ if exists && !DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)+ nextend++ localize(post, locale)endMessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)end++ private++ def localize(post, locale)+ begin+ DiscourseAi::Translation::PostLocalizer.localize(post, locale)+ rescue FinalDestination::SSRFDetector::LookupFailedError+ # do nothing, there are too many sporadic lookup failures+ rescue => e+ DiscourseAi::Translation::VerboseLogger.log(+ "Failed to translate post #{post.id} to #{locale}: #{e.message}\n\n#{e.backtrace[0..3].join("\n")}",+ )+ end+ endendenddiff --git a/plugins/discourse-ai/lib/translation/entry_point.rb b/plugins/discourse-ai/lib/translation/entry_point.rbindex 68447209f23..108e6eebaa0 100644--- a/plugins/discourse-ai/lib/translation/entry_point.rb+++ b/plugins/discourse-ai/lib/translation/entry_point.rb@@ -16,9 +16,19 @@ module DiscourseAiendend- plugin.on(:post_edited) do |post, topic_changed|- if DiscourseAi::Translation.enabled? && topic_changed- Jobs.enqueue(:detect_translate_topic, topic_id: post.topic_id)+ plugin.on(:post_edited) do |post, topic_changed, revisor|+ if DiscourseAi::Translation.enabled?+ grace = [SiteSetting.editing_grace_period.seconds, 5.minutes].max++ if topic_changed+ if revisor.topic_title_changed?+ Jobs.enqueue_in(grace, :detect_translate_topic, topic_id: post.topic_id)+ end+ else+ if revisor.should_create_new_version?+ Jobs.enqueue_in(grace, :detect_translate_post, post_id: post.id)+ end+ endendendenddiff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rbindex 0ca76d2bb2d..e3582396c78 100644--- a/plugins/discourse-ai/lib/translation/post_localizer.rb+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb@@ -4,8 +4,8 @@ module DiscourseAimodule Translationclass PostLocalizerdef self.localize(post, target_locale = I18n.locale)- if post.blank? || target_locale.blank? || post.locale == target_locale.to_s ||- post.raw.blank?+ if post.blank? || target_locale.blank? ||+ LocaleNormalizer.is_same?(post.locale, target_locale) || post.raw.blank?returnendreturn if post.raw.length > SiteSetting.ai_translation_max_post_length@@ -23,6 +23,35 @@ module DiscourseAilocalization.save!localizationend++ def self.has_relocalize_quota?(post, locale, skip_incr: false)+ return false if get_relocalize_quota(post, locale).to_i >= 2++ incr_relocalize_quota(post, locale) unless skip_incr+ true+ end++ private++ def self.relocalize_key(post, locale)+ "post_relocalized_#{post.id}_#{locale}"+ end++ def self.get_relocalize_quota(post, locale)+ Discourse.redis.get(relocalize_key(post, locale)).to_i || 0+ end++ def self.incr_relocalize_quota(post, locale)+ key = relocalize_key(post, locale)++ if (count = get_relocalize_quota(post, locale)).zero?+ Discourse.redis.set(key, 1, ex: 1.day.to_i)+ else+ ttl = Discourse.redis.ttl(key)+ incr = count.to_i + 1+ Discourse.redis.set(key, incr, ex: ttl)+ end+ endendendenddiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rbindex ffb08aea9c4..55737a0ebcc 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb@@ -68,22 +68,31 @@ describe Jobs::DetectTranslatePost dojob.execute({ post_id: post.id })end- it "skips translating if the post is already localized" do- post.update(locale: "en")- Fabricate(:post_localization, post:, locale: "ja")+ context "when translation exists and retranslation quota hit" do+ before do+ DiscourseAi::Translation::PostLocalizer+ .expects(:has_relocalize_quota?)+ .with(post, "ja")+ .returns(false)+ end- DiscourseAi::Translation::PostLocalizer.expects(:localize).never+ it "skips translating if the post is already localized" do+ post.update(locale: "en")+ Fabricate(:post_localization, post:, locale: "ja")- job.execute({ post_id: post.id })- end+ DiscourseAi::Translation::PostLocalizer.expects(:localize).never- it "does not translate to language of similar variant" do- post.update(locale: "en_GB")- Fabricate(:post_localization, post: post, locale: "ja_JP")+ job.execute({ post_id: post.id })+ end- DiscourseAi::Translation::PostLocalizer.expects(:localize).never+ it "does not translate to language of similar variant" do+ post.update(locale: "en_GB")+ Fabricate(:post_localization, post: post, locale: "ja_JP")- job.execute({ post_id: post.id })+ DiscourseAi::Translation::PostLocalizer.expects(:localize).never++ job.execute({ post_id: post.id })+ endendit "handles translation errors gracefully" dodiff --git a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rbindex e826b87e667..8a92316b80e 100644--- a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb@@ -51,7 +51,7 @@ describe DiscourseAi::Translation::EntryPoint doendend- describe "upon first post (topic) edited" do+ describe "upon topic edited" dofab!(:post) { Fabricate(:post, post_number: 1) }fab!(:non_first_post) { Fabricate(:post, post_number: 2) }@@ -60,18 +60,34 @@ describe DiscourseAi::Translation::EntryPoint doassign_fake_provider_to(:ai_default_llm_model)end- it "enqueues detect topic locale and translate topic job" do+ it "enqueues in grace period detect translate topic job if title changed" do+ freeze_time++ SiteSetting.editing_grace_period = 10.minutesSiteSetting.ai_translation_enabled = truetopic = post.topicrevisor = PostRevisor.new(post, topic)- revisor.revise!(- post.user,- { title: "A whole new hole" },- { validate_post: false, bypass_bump: false },- )+ revisor.revise!(post.user, { title: "A whole new hole" }, { skip_validations: true })revisor.post_process_post- expect_job_enqueued(job: :detect_translate_topic, args: { topic_id: topic.id })+ expect_job_enqueued(+ job: :detect_translate_topic,+ args: {+ topic_id: topic.id,+ },+ at: 10.minutes.from_now,+ )+ expect(job_enqueued?(job: :detect_translate_post)).to eq false+ end++ it "does not enqueue detect translate topic job if title did not change" do+ new_category = Fabricate(:category)+ SiteSetting.ai_translation_enabled = true+ topic = post.topic+ post.revise(post.user, category_id: new_category.id)++ expect(job_enqueued?(job: :detect_translate_topic, args: { topic_id: topic.id })).to eq false+ expect(job_enqueued?(job: :detect_translate_post)).to eq falseendit "does not enqueue if setting disabled" do@@ -80,6 +96,36 @@ describe DiscourseAi::Translation::EntryPoint doexpect(job_enqueued?(job: :detect_translate_topic, args: { topic_id: post.topic_id }),).to eq false+ expect(job_enqueued?(job: :detect_translate_post)).to eq false+ end+ end++ describe "upon post edited" do+ it "enqueues detect translate post job in grace period" do+ freeze_time++ SiteSetting.editing_grace_period = 10.minutes+ SiteSetting.ai_translation_enabled = true+ post = Fabricate(:post, post_number: 1)+ post.revise(post.user, { raw: "new raw" }, { force_new_version: true })++ expect_job_enqueued(+ job: :detect_translate_post,+ args: {+ post_id: post.id,+ },+ at: 10.minutes.from_now,+ )+ expect(job_enqueued?(job: :detect_translate_topic)).to eq false+ end++ it "does not enqueue detect translate post job if no new version of post" do+ SiteSetting.ai_translation_enabled = true+ post = Fabricate(:post, post_number: 1)+ post.revise(post.user, { raw: post.raw + "A" })++ expect(job_enqueued?(job: :detect_translate_post)).to eq false+ expect(job_enqueued?(job: :detect_translate_topic)).to eq falseendendenddiff --git a/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rbindex 787744811c5..e34496a5686 100644--- a/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb@@ -88,4 +88,35 @@ describe DiscourseAi::Translation::PostLocalizer do}.to_not change { PostLocalization.count }endend++ describe ".has_relocalize_quota?" do+ fab!(:post)++ it "returns false if quota is already 2 or more" do+ Discourse.redis.set(described_class.relocalize_key(post, "en"), 2, ex: 10)+ expect(described_class.has_relocalize_quota?(post, "en")).to eq(false)++ Discourse.redis.set(described_class.relocalize_key(post, "en"), 3, ex: 10)+ expect(described_class.has_relocalize_quota?(post, "en")).to eq(false)+ end++ it "returns true if quota is less than 2 and increments quota" do+ Discourse.redis.set(described_class.relocalize_key(post, "en"), 1, ex: 10)++ expect(described_class.has_relocalize_quota?(post, "en")).to eq(true)+ end++ it "does not increment quota if skip_incr is true" do+ Discourse.redis.set(described_class.relocalize_key(post, "en"), 1, ex: 10)++ described_class.has_relocalize_quota?(post, "en", skip_incr: true)+ expect(Discourse.redis.get(described_class.relocalize_key(post, "en"))).to eq("1")+ end++ it "increments quota if it was not set before" do+ described_class.has_relocalize_quota?(post, "en")++ expect(Discourse.redis.get(described_class.relocalize_key(post, "en"))).to eq("1")+ end+ endend