-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Observations): Add automatic translations
- Loading branch information
1 parent
f390604
commit 5fc0a6f
Showing
12 changed files
with
182 additions
and
2 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 |
---|---|---|
|
@@ -23,3 +23,6 @@ RESPONSIBLE_EMAIL= | |
SEND_EMAILS_IN_DEV=false | ||
|
||
MAPBOX_API_KEY= | ||
|
||
GOOGLE_CLIENT_ID= | ||
GOOGLE_API_KEY= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class TranslationJob < ApplicationJob | ||
class TranslationException < StandardError | ||
end | ||
|
||
queue_as :default | ||
retry_on TranslationException, wait: 5.minutes, attempts: 3 | ||
|
||
# Takes an entity (an ActiveRecord object) and an original_locale, and translates all the fields based on that locale | ||
# The model should have a TRANSLATABLE_FIELDS constant | ||
def perform(entity, original_locale) | ||
return unless entity.class.const_defined?(:AUTOMATICALLY_TRANSLATABLE_FIELDS) | ||
|
||
fields = entity.class.const_get(:AUTOMATICALLY_TRANSLATABLE_FIELDS) | ||
translation_service = TranslationService.new | ||
translated_fields = {} | ||
|
||
fields.each do |field| | ||
translated_fields[field] = {} | ||
I18n.available_locales.each do |locale| | ||
next if locale == original_locale | ||
|
||
I18n.with_locale original_locale do | ||
translated_fields[field][locale] = translation_service.call(entity.send(field), I18n.locale, locale) | ||
end | ||
end | ||
end | ||
|
||
translated_fields.each do |field, translation| | ||
translation.each do |locale, text| | ||
I18n.with_locale locale do | ||
entity.send("#{field}=", text) | ||
entity.translation.send("#{field}_translated_from=", "#{original_locale}") | ||
end | ||
end | ||
end | ||
entity.save | ||
|
||
rescue e | ||
Sentry.capture_exception(e) | ||
raise TranslationException | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
# Service that communicates with the Google Translator API | ||
class TranslationService | ||
def initialize | ||
@translator = Google::Cloud::Translate.translation_v2_service( | ||
key: ENV["GOOGLE_API_KEY"] | ||
) | ||
end | ||
|
||
def call(text, from, to) | ||
translation = @translator.translate text, from: from, to: to | ||
|
||
translation.text | ||
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
7 changes: 7 additions & 0 deletions
7
db/migrate/20240128163512_add_automatic_translation_field_to_observation_translations.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,7 @@ | ||
class AddAutomaticTranslationFieldToObservationTranslations < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :observation_translations, :details_translated_from, :string | ||
add_column :observation_translations, :concern_opinion_translated_from, :string | ||
add_column :observation_translations, :litigation_status_translated_from, :string | ||
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