A platform for community discussion. Free, open, simple.
FIX: Retranslate topic excerpts and avoid using post versions to indicate change (#36382)
Natalie Taya4440912931f
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex baab2a98454..fd7cda2b046 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -54,9 +54,8 @@ module Jobsnext if LocaleNormalizer.is_same?(locale, detected_locale)exists = post.localizations.matching_locale(locale).exists?- if exists && !DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)- next- end+ has_quota = DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)+ next if !force && exists && !has_quotalocalize(post, locale)enddiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rbindex 1940f11f8ad..85eeb9019bb 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb@@ -50,7 +50,10 @@ module Jobslocales.each do |locale|next if LocaleNormalizer.is_same?(locale, detected_locale)- next if topic.localizations.matching_locale(locale).exists?+ exists = topic.localizations.matching_locale(locale).exists?++ has_quota = DiscourseAi::Translation::TopicLocalizer.has_relocalize_quota?(topic, locale)+ next if !force && exists && !has_quotabeginDiscourseAi::Translation::TopicLocalizer.localize(topic, locale)diff --git a/plugins/discourse-ai/lib/translation/entry_point.rb b/plugins/discourse-ai/lib/translation/entry_point.rbindex 108e6eebaa0..d61b12f739b 100644--- a/plugins/discourse-ai/lib/translation/entry_point.rb+++ b/plugins/discourse-ai/lib/translation/entry_point.rb@@ -16,19 +16,19 @@ module DiscourseAiendend- plugin.on(:post_edited) do |post, topic_changed, revisor|+ plugin.on(:post_edited) do |post, _, 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+ title_changed = revisor.topic_title_changed?+ raw_changed = revisor.raw_changed?+ excerpt_changed = post.is_first_post? && raw_changed++ if title_changed || excerpt_changed+ Jobs.enqueue_in(grace, :detect_translate_topic, topic_id: post.topic_id)end++ Jobs.enqueue_in(grace, :detect_translate_post, post_id: post.id) if raw_changedendendenddiff --git a/plugins/discourse-ai/lib/translation/localizable_quota.rb b/plugins/discourse-ai/lib/translation/localizable_quota.rbnew file mode 100644index 00000000000..0eb44aee561--- /dev/null+++ b/plugins/discourse-ai/lib/translation/localizable_quota.rb@@ -0,0 +1,42 @@+# frozen_string_literal: true++module DiscourseAi+ module Translation+ module LocalizableQuota+ extend ActiveSupport::Concern++ MAX_QUOTA_PER_DAY = 2++ class_methods do+ def has_relocalize_quota?(model, locale, skip_incr: false)+ return false if get_relocalize_quota(model, locale).to_i >= MAX_QUOTA_PER_DAY++ incr_relocalize_quota(model, locale) unless skip_incr+ true+ end++ def relocalize_key(model, locale)+ "#{model_name}_relocalized_#{model.id}_#{locale}"+ end++ private++ def get_relocalize_quota(model, locale)+ Discourse.redis.get(relocalize_key(model, locale)).to_i || 0+ end++ def incr_relocalize_quota(model, locale)+ key = relocalize_key(model, locale)++ if (count = get_relocalize_quota(model, 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+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rbindex 4a166a1f2a5..aa97f425992 100644--- a/plugins/discourse-ai/lib/translation/post_localizer.rb+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb@@ -3,7 +3,7 @@module DiscourseAimodule Translationclass PostLocalizer- MAX_QUOTA_PER_DAY = 2+ include LocalizableQuotadef self.localize(post, target_locale = I18n.locale, llm_model: nil)if post.blank? || target_locale.blank? ||@@ -40,33 +40,8 @@ module DiscourseAilocalizationend- def self.has_relocalize_quota?(post, locale, skip_incr: false)- return false if get_relocalize_quota(post, locale).to_i >= MAX_QUOTA_PER_DAY-- 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+ def self.model_name+ "post"endendenddiff --git a/plugins/discourse-ai/lib/translation/topic_localizer.rb b/plugins/discourse-ai/lib/translation/topic_localizer.rbindex c6b1b3bec16..b5b609a3ea1 100644--- a/plugins/discourse-ai/lib/translation/topic_localizer.rb+++ b/plugins/discourse-ai/lib/translation/topic_localizer.rb@@ -3,6 +3,8 @@module DiscourseAimodule Translationclass TopicLocalizer+ include LocalizableQuota+def self.localize(topic,target_locale = I18n.locale,@@ -38,6 +40,10 @@ module DiscourseAilocalization.save!localizationend++ def self.model_name+ "topic"+ 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 6bfa56894ed..69505813050 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@@ -108,6 +108,15 @@ describe Jobs::DetectTranslatePost dojob.execute({ post_id: post.id })end++ it "translates if force is true" do+ post.update(locale: "en")+ Fabricate(:post_localization, post:, locale: "ja")++ DiscourseAi::Translation::PostLocalizer.expects(:localize).with(post, "ja").once++ job.execute({ post_id: post.id, force: true })+ endendit "handles translation errors gracefully" dodiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rbindex cd1d387516f..54fd2745204 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb@@ -78,21 +78,40 @@ describe Jobs::DetectTranslateTopic dojob.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+ context "when translation exists and retranslation quota hit" do+ before do+ DiscourseAi::Translation::TopicLocalizer+ .expects(:has_relocalize_quota?)+ .with(topic, "ja")+ .returns(false)+ end- 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")- it "does not translate to language of similar variant" do- topic.update(locale: "en_GB")- Fabricate(:topic_localization, topic:, locale: "ja_JP")+ DiscourseAi::Translation::TopicLocalizer.expects(:localize).never- DiscourseAi::Translation::PostLocalizer.expects(:localize).never+ job.execute({ topic_id: topic.id })+ end- job.execute({ topic_id: topic.id })+ it "does not translate to language of similar variant" do+ topic.update(locale: "en_GB")+ Fabricate(:topic_localization, topic:, locale: "ja_JP")++ DiscourseAi::Translation::TopicLocalizer.expects(:localize).never++ job.execute({ topic_id: topic.id })+ end++ it "translates when force is true" do+ topic.update(locale: "en")+ Fabricate(:topic_localization, topic:, locale: "ja")++ DiscourseAi::Translation::TopicLocalizer.expects(:localize).with(topic, "ja").once++ job.execute({ topic_id: topic.id, force: true })+ 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 8a92316b80e..a8b318a7a0b 100644--- a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb@@ -80,6 +80,30 @@ describe DiscourseAi::Translation::EntryPoint doexpect(job_enqueued?(job: :detect_translate_post)).to eq falseend+ it "enqueues in grace period detect translate topic job if first post (excerpt) changed" do+ freeze_time++ SiteSetting.editing_grace_period = 10.minutes+ SiteSetting.ai_translation_enabled = true+ topic = post.topic+ revisor = PostRevisor.new(post, topic)+ revisor.revise!(+ post.user,+ { raw: post.raw + " Additional content." },+ { skip_validations: true },+ )+ revisor.post_process_post++ 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 true+ end+it "does not enqueue detect translate topic job if title did not change" donew_category = Fabricate(:category)SiteSetting.ai_translation_enabled = true@@ -106,8 +130,8 @@ describe DiscourseAi::Translation::EntryPoint doSiteSetting.editing_grace_period = 10.minutesSiteSetting.ai_translation_enabled = true- post = Fabricate(:post, post_number: 1)- post.revise(post.user, { raw: "new raw" }, { force_new_version: true })+ post = Fabricate(:post, post_number: 2)+ post.revise(post.user, { raw: "new raw" })expect_job_enqueued(job: :detect_translate_post,@@ -118,14 +142,5 @@ describe DiscourseAi::Translation::EntryPoint do)expect(job_enqueued?(job: :detect_translate_topic)).to eq falseend-- 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 false- endendenddiff --git a/plugins/discourse-ai/spec/lib/translation/localizable_quota_spec.rb b/plugins/discourse-ai/spec/lib/translation/localizable_quota_spec.rbnew file mode 100644index 00000000000..8b125c35314--- /dev/null+++ b/plugins/discourse-ai/spec/lib/translation/localizable_quota_spec.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++describe DiscourseAi::Translation::LocalizableQuota do+ let(:test_class) do+ Class.new do+ include DiscourseAi::Translation::LocalizableQuota++ def self.model_name+ "post"+ end+ end+ end++ let(:model) { OpenStruct.new(id: 123) }+ let(:locale) { "en" }++ after { Discourse.redis.del(test_class.relocalize_key(model, locale)) }++ describe ".has_relocalize_quota?" do+ it "returns false if quota is at MAX_QUOTA_PER_DAY" do+ Discourse.redis.set(test_class.relocalize_key(model, locale), 2, ex: 10)+ expect(test_class.has_relocalize_quota?(model, locale)).to eq(false)+ end++ it "returns false if quota is above MAX_QUOTA_PER_DAY" do+ Discourse.redis.set(test_class.relocalize_key(model, locale), 3, ex: 10)+ expect(test_class.has_relocalize_quota?(model, locale)).to eq(false)+ end++ it "returns true if quota is below MAX_QUOTA_PER_DAY and increments quota" do+ Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 10)++ expect(test_class.has_relocalize_quota?(model, locale)).to eq(true)+ expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("2")+ end++ it "does not increment quota if skip_incr is true" do+ Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 10)++ test_class.has_relocalize_quota?(model, locale, skip_incr: true)+ expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("1")+ end++ it "initializes quota to 1 if not set before" do+ test_class.has_relocalize_quota?(model, locale)++ expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("1")+ end++ it "sets expiry to 1 day when initializing quota" do+ test_class.has_relocalize_quota?(model, locale)++ ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))+ expect(ttl).to be_within(5).of(1.day.to_i)+ end++ it "preserves TTL when incrementing existing quota" do+ Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 3600)+ original_ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))++ test_class.has_relocalize_quota?(model, locale)++ new_ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))+ expect(new_ttl).to be_within(2).of(original_ttl)+ end+ end+end