Module: ActiveTranslation

Defined in:
app/lib/active_translation/google_translate.rb,
lib/active_translation.rb,
lib/active_translation/engine.rb,
lib/active_translation/version.rb,
app/models/active_translation/cache.rb,
lib/active_translation/translatable.rb,
lib/active_translation/configuration.rb,
lib/active_translation/translation_job.rb,
app/models/active_translation/translation.rb,
app/jobs/active_translation/application_job.rb,
app/models/active_translation/application_record.rb,
app/helpers/active_translation/application_helper.rb,
app/mailers/active_translation/application_mailer.rb,
lib/generators/active_translation/install_generator.rb,
app/controllers/active_translation/application_controller.rb

Overview

USAGE: GoogleTranslate.translate(target_language_code: “es-MX”, text: “Hello world!”) USAGE: GoogleTranslate.translate(target_language_code: “es-MX”, text: Job.posted_status.last.title, obj: Job.posted_status.last)

Defined Under Namespace

Modules: ApplicationHelper, Generators, Translatable Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Cache, Configuration, Engine, GoogleTranslate, Translation, TranslationJob

Constant Summary collapse

VERSION =
"0.7.10"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



13
14
15
# File 'lib/active_translation.rb', line 13

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/active_translation.rb', line 17

def configure
  yield(configuration)
end

.translate(text:, locale:, cache: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_translation.rb', line 21

def translate(text:, locale:, cache: true)
  text = text.to_s
  locale = locale.to_s

  if cache
    cached_translation = Cache.find_by(locale:, checksum: Digest::MD5.hexdigest(text))
    return cached_translation.translated_text if cached_translation
  end

  translated_text = GoogleTranslate.translate(target_language_code: locale, text:)

  Cache.add!(locale:, original_text: text, translated_text:) if cache

  translated_text
end