Class: Decidim::MachineTranslationSaveJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/decidim/machine_translation_save_job.rb

Overview

This job is used by machine translation services to store the result of a field translation. This way services don’t need to care about how to save it and also enables storing translations asynchronously when the translation service returns the translated text in a webhook.

Instance Method Summary collapse

Instance Method Details

#perform(resource, field_name, target_locale, translated_text) ⇒ Object

Performs the job. It won’t perform anything if the ‘Decidim.machine_translation_service` config is not set.

resource - Any kind of ‘Decidim::TranslatableResource` model instance field_name - A Symbol representing the name of the field being translated target_locale - A Symbol representing the target locale for the translation translated_text - A String with the value of the field_name, translated

into the target_locale


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/jobs/decidim/machine_translation_save_job.rb', line 19

def perform(resource, field_name, target_locale, translated_text)
  resource.with_lock do
    if resource[field_name]["machine_translations"].present?
      resource[field_name]["machine_translations"] = resource[field_name]["machine_translations"].merge(target_locale => translated_text)
    else
      resource[field_name] = resource[field_name].merge("machine_translations" => { target_locale => translated_text })
    end

    # rubocop:disable Rails/SkipsModelValidations
    resource.update_column field_name.to_sym, resource[field_name]
    # rubocop:enable Rails/SkipsModelValidations
  end

  send_translated_report_notifications(resource) if reported_resource_in_organization_language?(resource, target_locale)
end