-
Notifications
You must be signed in to change notification settings - Fork 52
FEATURE: Detect locale and translate posts from core table #287
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a7a61f3
FEATURE: Detect locale and translate posts from core table
nattsw e3fe877
compat
nattsw 6592e21
order
nattsw 0e96217
test order
nattsw 3b054c0
no retry
nattsw 051adea
no translate same locale
nattsw 333e3b5
update job
nattsw 135bb57
context cop
nattsw ce6ca90
loop through locales
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 hidden or 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 hidden or 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,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Jobs | ||
| class DetectPostsLocale < ::Jobs::Base | ||
| cluster_concurrency 1 | ||
| sidekiq_options retry: false | ||
|
|
||
| BATCH_SIZE = 50 | ||
|
|
||
| def execute(args) | ||
| return unless SiteSetting.translator_enabled | ||
| return unless SiteSetting.experimental_content_translation | ||
|
|
||
| posts = | ||
| Post | ||
| .where(locale: nil) | ||
| .where(deleted_at: nil) | ||
| .where("posts.user_id > 0") | ||
| .where.not(raw: [nil, ""]) | ||
| .order(updated_at: :desc) | ||
| .limit(BATCH_SIZE) | ||
| return if posts.empty? | ||
|
|
||
| posts.each do |post| | ||
| begin | ||
| DiscourseTranslator::PostLocaleDetector.detect_locale(post) | ||
| rescue => e | ||
| Rails.logger.error( | ||
| "Discourse Translator: Failed to detect post #{post.id}'s locale: #{e.message}", | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| DiscourseTranslator::VerboseLogger.log("Detected #{posts.size} post locales") | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Jobs | ||
| class TranslatePosts < ::Jobs::Base | ||
| cluster_concurrency 1 | ||
| sidekiq_options retry: false | ||
|
|
||
| BATCH_SIZE = 50 | ||
|
|
||
| def execute(args) | ||
| return unless SiteSetting.translator_enabled | ||
| return unless SiteSetting.experimental_content_translation | ||
|
|
||
| locales = SiteSetting.automatic_translation_target_languages.split("|") | ||
| return if locales.blank? | ||
|
|
||
| locales.each do |locale| | ||
| posts = | ||
| Post | ||
| .joins( | ||
| "LEFT JOIN post_localizations pl ON pl.post_id = posts.id AND pl.locale = #{ActiveRecord::Base.connection.quote(locale)}", | ||
| ) | ||
| .where(deleted_at: nil) | ||
| .where("posts.user_id > 0") | ||
| .where.not(raw: [nil, ""]) | ||
| .where.not(locale: nil) | ||
| .where.not(locale: locale) | ||
| .where("pl.id IS NULL") | ||
| .limit(BATCH_SIZE) | ||
|
|
||
| next if posts.empty? | ||
|
|
||
| posts.each do |post| | ||
| begin | ||
| DiscourseTranslator::PostTranslator.translate(post, locale) | ||
| rescue => e | ||
| Rails.logger.error( | ||
| "Discourse Translator: Failed to translate post #{post.id} to #{locale}: #{e.message}", | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| DiscourseTranslator::VerboseLogger.log("Translated #{posts.size} posts to #{locale}") | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseTranslator | ||
| class PostLocaleDetector | ||
| def self.detect_locale(post) | ||
| return if post.blank? | ||
|
|
||
| translator = DiscourseTranslator::Provider::TranslatorProvider.get | ||
| detected_locale = translator.detect!(post) | ||
| post.update!(locale: detected_locale) | ||
| detected_locale | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseTranslator | ||
| class PostTranslator | ||
| def self.translate(post, target_locale = I18n.locale) | ||
| return if post.blank? || target_locale.blank? || post.locale == target_locale.to_s | ||
|
|
||
| target_locale_sym = target_locale.to_s.sub("-", "_").to_sym | ||
|
|
||
| translator = DiscourseTranslator::Provider::TranslatorProvider.get | ||
| translated_raw = translator.translate_post!(post, target_locale_sym) | ||
|
|
||
| localization = | ||
| PostLocalization.find_or_initialize_by(post_id: post.id, locale: target_locale_sym.to_s) | ||
|
|
||
| localization.raw = translated_raw | ||
| localization.cooked = PrettyText.cook(translated_raw) | ||
| localization.post_version = post.version | ||
| localization.localizer_user_id = Discourse.system_user.id | ||
| localization.save! | ||
| localization | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Jobs::DetectPostsLocale do | ||
| fab!(:post) { Fabricate(:post, locale: nil) } | ||
| subject(:job) { described_class.new } | ||
|
|
||
| before do | ||
| SiteSetting.translator_enabled = true | ||
| SiteSetting.experimental_content_translation = true | ||
| end | ||
|
|
||
| it "does nothing when translator is disabled" do | ||
| SiteSetting.translator_enabled = false | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).never | ||
|
|
||
| job.execute({}) | ||
| end | ||
|
|
||
| it "does nothing when content translation is disabled" do | ||
| SiteSetting.experimental_content_translation = false | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).never | ||
|
|
||
| job.execute({}) | ||
| end | ||
|
|
||
| it "does nothing when there are no posts to detect" do | ||
| Post.update_all(locale: "en") | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).never | ||
|
|
||
| job.execute({}) | ||
| end | ||
|
|
||
| it "detects locale for posts with nil locale" do | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).once | ||
| job.execute({}) | ||
| end | ||
|
|
||
| it "detects most recently updated posts first" do | ||
| post_2 = Fabricate(:post, locale: nil) | ||
| post_3 = Fabricate(:post, locale: nil) | ||
|
|
||
| post.update!(updated_at: 3.days.ago) | ||
| post_2.update!(updated_at: 2.day.ago) | ||
| post_3.update!(updated_at: 4.day.ago) | ||
|
|
||
| original_batch = described_class::BATCH_SIZE | ||
| described_class.const_set(:BATCH_SIZE, 1) | ||
|
|
||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_2).once | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).never | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post_3).never | ||
|
|
||
| job.execute({}) | ||
| ensure | ||
| described_class.const_set(:BATCH_SIZE, original_batch) | ||
| end | ||
|
|
||
| it "skips bot posts" do | ||
| post.update!(user: Discourse.system_user) | ||
| DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).never | ||
|
|
||
| job.execute({}) | ||
| end | ||
|
|
||
| it "handles detection errors gracefully" do | ||
| DiscourseTranslator::PostLocaleDetector | ||
| .expects(:detect_locale) | ||
| .with(post) | ||
| .raises(StandardError.new("jiboomz")) | ||
| .once | ||
|
|
||
| expect { job.execute({}) }.not_to raise_error | ||
| end | ||
|
|
||
| it "logs a summary after running" do | ||
| DiscourseTranslator::PostLocaleDetector.stubs(:detect_locale) | ||
| DiscourseTranslator::VerboseLogger.expects(:log).with(includes("Detected 1 post locales")) | ||
|
|
||
| job.execute({}) | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.