discourse/plugins/discourse-ai/lib/translation/localizable_quota.rb
Natalie Tay a444091293
FIX: Retranslate topic excerpts and avoid using post versions to indicate change (#36382)
There are three bugs fixed here:

## bug 1
`revisor.should_create_new_version?` does not trigger when an upload is
added. This means localizations will not be updated. This is now fixed
by not using `should_create_new_version` but `raw_changed` instead.

m/t/389685/15

## bug 2
When the topic's first post is updated, it does not update the
topic_localization excerpt. This is now fixed.

t/169370/3

## bug 3
When the topic title is edited (entry_point.rb), the
`detect_translate_topic` job is enqueued, however, localization does not
happen because it already exists. The job now is consistent and
relocalizes based on a daily quota.

t/169370/3
2025-12-02 22:45:11 +08:00

42 lines
1.1 KiB
Ruby

# 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
end