-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Translate all new posts automatically
Adds a new site setting 'translate_posts_to_languages'
- Loading branch information
Showing
15 changed files
with
339 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
class TranslateTranslatable < ::Jobs::Base | ||
def execute(args) | ||
return unless SiteSetting.translator_enabled | ||
return if SiteSetting.automatic_translation_target_languages.blank? | ||
|
||
translatable = args[:type].constantize.find_by(id: args[:translatable_id]) | ||
return if translatable.blank? | ||
|
||
target_locales = SiteSetting.automatic_translation_target_languages.split("|") | ||
target_locales.each do |target_locale| | ||
# 1. no special retry, job will be automatically retried with backoff | ||
# 2. translate function will handle cases where translation is not needed or not possible | ||
"DiscourseTranslator::#{SiteSetting.translator}".constantize.translate( | ||
translatable, | ||
target_locale.to_sym, | ||
) | ||
end | ||
|
||
topic_id = translatable.is_a?(Post) ? translatable.topic.id : translatable.id | ||
post_id = translatable.is_a?(Post) ? translatable.id : 1 | ||
MessageBus.publish("/topic/#{topic_id}", type: :revised, id: post_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
class AutomaticTranslationBackfill < ::Jobs::Scheduled | ||
every 5.minutes | ||
|
||
BACKFILL_LOCK_KEY = "discourse_translator_backfill_lock" | ||
|
||
def execute(args = nil) | ||
return unless SiteSetting.translator_enabled | ||
return unless should_backfill? | ||
return unless secure_backfill_lock | ||
|
||
begin | ||
process_batch | ||
ensure | ||
Discourse.redis.del(BACKFILL_LOCK_KEY) | ||
end | ||
end | ||
|
||
def fetch_untranslated_model_ids(model = Post, limit = 100, target_locales = backfill_locales) | ||
m = model.name.downcase | ||
DB.query_single(<<~SQL, target_locales: target_locales, limit: limit) | ||
SELECT m.id | ||
FROM #{m}s m | ||
LEFT JOIN discourse_translator_#{m}_locales dl ON dl.#{m}_id = m.id | ||
LEFT JOIN LATERAL ( | ||
SELECT array_agg(DISTINCT locale)::text[] as locales | ||
FROM discourse_translator_#{m}_translations dt | ||
WHERE dt.#{m}_id = m.id | ||
) translations ON true | ||
WHERE NOT ( | ||
ARRAY[:target_locales]::text[] <@ | ||
(COALESCE( | ||
array_cat( | ||
ARRAY[COALESCE(dl.detected_locale, '')]::text[], | ||
COALESCE(translations.locales, ARRAY[]::text[]) | ||
), | ||
ARRAY[]::text[] | ||
)) | ||
) | ||
ORDER BY m.id DESC | ||
LIMIT :limit | ||
SQL | ||
end | ||
|
||
private | ||
|
||
def should_backfill? | ||
return false if SiteSetting.automatic_translation_target_languages.blank? | ||
return false if SiteSetting.automatic_translation_backfill_maximum_translations_per_hour == 0 | ||
true | ||
end | ||
|
||
def secure_backfill_lock | ||
Discourse.redis.set(BACKFILL_LOCK_KEY, "1", ex: 5.minutes.to_i, nx: true) | ||
end | ||
|
||
def translations_per_run | ||
[ | ||
(SiteSetting.automatic_translation_backfill_maximum_translations_per_hour / 12) / | ||
backfill_locales.size, | ||
1, | ||
].max | ||
end | ||
|
||
def backfill_locales | ||
@backfill_locales ||= SiteSetting.automatic_translation_target_languages.split("|") | ||
end | ||
|
||
def translator | ||
@translator_klass ||= "DiscourseTranslator::#{SiteSetting.translator}".constantize | ||
end | ||
|
||
def translate_records(type, record_ids) | ||
record_ids.each do |id| | ||
record = type.find(id) | ||
backfill_locales.each do |target_locale| | ||
begin | ||
translator.translate(record, target_locale.to_sym) | ||
rescue => e | ||
# continue with other locales even if one fails | ||
Rails.logger.warn( | ||
"Failed to machine-translate #{type.name}##{id} to #{target_locale}: #{e.message}\n#{e.backtrace.join("\n")}", | ||
) | ||
next | ||
end | ||
end | ||
end | ||
end | ||
|
||
def process_batch | ||
models_translated = [Post, Topic].size | ||
translations_to_run = [translations_per_run / models_translated, 1].max | ||
topic_ids = fetch_untranslated_model_ids(Topic, translations_to_run) | ||
translations_to_run = translations_per_run if topic_ids.empty? | ||
post_ids = fetch_untranslated_model_ids(Post, translations_to_run) | ||
return if topic_ids.empty? && post_ids.empty? | ||
|
||
translate_records(Topic, topic_ids) | ||
translate_records(Post, post_ids) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
lib/discourse_translator/translatable_languages_setting.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseTranslator | ||
class TranslatableLanguagesSetting < LocaleSiteSetting | ||
def self.printable_values | ||
values.map { |v| v[:value] } | ||
end | ||
|
||
@lock = Mutex.new | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.