Sha256: 142a66692971dae3d8f67594f73a3c805c460bc6bc38f85c2c6c52d525d074db

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Decidim
  # 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.
  class MachineTranslationSaveJob < ApplicationJob
    queue_as :default

    # 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
    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
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.5 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.4 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.3 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.2 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.1 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.1.rc1 app/jobs/decidim/machine_translation_save_job.rb
decidim-core-0.23.0 app/jobs/decidim/machine_translation_save_job.rb