-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: Translates every post to automatic_translation_target_languages #207
Merged
+430
−17
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1ec2f58
FEATURE: Translate all new posts automatically
nattsw 6a5581a
Switch conditions
nattsw 3664d20
Add test for translate job
nattsw 54b2699
Update var readable
nattsw f6a46ec
Lint
nattsw 3501883
Updates
nattsw 34d80c8
Simplify tests
nattsw 78b54a2
Deflakify tests
nattsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,25 @@ | ||
# 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| | ||
"DiscourseTranslator::#{SiteSetting.translator}".constantize.translate( | ||
translatable, | ||
target_locale.to_sym, | ||
) | ||
end | ||
|
||
topic_id, post_id = | ||
translatable.is_a?(Post) ? [translatable.topic_id, translatable.id] : [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| | ||
nattsw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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_per_model = [translations_per_run / models_translated, 1].max | ||
topic_ids = fetch_untranslated_model_ids(Topic, translations_per_model) | ||
translations_per_model = translations_per_run if topic_ids.empty? | ||
post_ids = fetch_untranslated_model_ids(Post, translations_per_model) | ||
nattsw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a lock here?