Last active
April 28, 2021 22:15
-
-
Save valscion/0ae4eac7a1f3fd0b6b671a09d5bdccdf to your computer and use it in GitHub Desktop.
Mobility + EasyTranslate. Replace dashes in file names with slashes
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
# frozen_string_literal: true | |
module AutoTranslate | |
module Callbacks | |
class AfterCommit | |
attr_reader :original_attribute_name, :translated_attribute_name | |
def initialize(original_attribute_name, translated_attribute_name) | |
@original_attribute_name = original_attribute_name | |
@translated_attribute_name = translated_attribute_name | |
end | |
def after_commit(model) | |
return unless model.previous_changes.include?(original_attribute_name) | |
::AutoTranslate::Workers::TranslateAttribute.perform_async( | |
model.to_gid, | |
original_attribute_name, | |
translated_attribute_name | |
) | |
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
# frozen_string_literal: true | |
module AutoTranslate | |
module Model | |
extend ActiveSupport::Concern | |
included do | |
def self.auto_translates(original_attribute_name, translated_attribute_name) | |
callback = ::AutoTranslate::Callbacks::AfterCommit.new( | |
original_attribute_name, | |
translated_attribute_name | |
) | |
after_commit callback | |
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
# frozen_string_literal: true | |
module AutoTranslate | |
module Providers | |
class Fake | |
def translate(text, to:, from: nil) | |
[ | |
prelude(to, from), | |
scramble(text) | |
].join(" ") | |
end | |
private | |
def prelude(to, from) | |
lang_identifier = [from, to].compact.join(' -> ') | |
"FAKE_TRANSLATION[#{lang_identifier}]" | |
end | |
def scramble(text) | |
fragment = Nokogiri::HTML.fragment(text) | |
fragment.search('.//text()').each do |text_node| | |
scrambled = rot13(text_node.content) | |
text_node.replace(scrambled) | |
end | |
fragment.to_html | |
end | |
def rot13(text) | |
text.tr('A-Za-z', 'N-ZA-Mn-za-m') | |
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
# frozen_string_literal: true | |
module AutoTranslate | |
module Providers | |
class Google | |
def initialize(api_key:) | |
@api_key = api_key | |
end | |
def translate(text, to:, from: nil) | |
::EasyTranslate.translate(text, to: to, from: from, api_key: @api_key) | |
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
# frozen_string_literal: true | |
module AutoTranslate | |
module Workers | |
class TranslateAttribute | |
include Sidekiq::Worker | |
def perform(model_gid, original_attribute_name, translated_attribute_name) | |
model = GlobalID::Locator.locate(model_gid) | |
original_value = model.read_attribute(original_attribute_name) | |
translation_backend = model.mobility_backend_for(translated_attribute_name) | |
target_locales(model).each do |locale| | |
translation = provider.translate(original_value, to: locale) | |
translation_backend.write(locale, translation) | |
end | |
model.save! | |
end | |
private | |
def target_locales(model) | |
I18n.available_locales.map(&:to_sym) - [model.locale.to_sym] | |
end | |
def provider | |
Rails.application.config.auto_translate_provider | |
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
class Venue < ApplicationRecord | |
include Mobility | |
translates :translated_description_html, type: :text | |
include AutoTranslate::Model | |
auto_translates :description_html, :translated_description_html | |
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
# frozen_string_literal: true | |
Rails.application.configure do | |
google_api_key = ENV['GOOGLE_TRANSLATE_API_KEY'] | |
if google_api_key.present? | |
config.auto_translate_provider = ::AutoTranslate::Providers::Google.new(api_key: google_api_key) | |
else | |
config.auto_translate_provider = ::AutoTranslate::Providers::Fake.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment