module Decidim::TranslatableResource
Public Class Methods
translatable_fields(*list)
click to toggle source
Calls superclass method
# File lib/decidim/translatable_resource.rb, line 15 def self.translatable_fields(*list) @translatable_fields = list @translatable_fields.each do |field| method_name = "#{field}=" # We're overriding the attribute setter method so that we can reuse the # machine translations. This is to fix a bug encoutered when updating # the resource from a FormObject. The FormObject on the `#create` # action in the controller doesn't have the machine translations loaded, # so they're effectively lost whenever a resource is updated. # # This overriding allows us to keep the old machine translations, so # that we skip some translations requests. define_method(method_name) do |new_value| return super(new_value) if attributes[field.to_s].nil? return super(new_value) unless [new_value, attributes[field.to_s]].all?(Hash) return super(new_value) if new_value.has_key?("machine_translations") original_value = attributes[field.to_s] new_value = new_value.merge("machine_translations" => original_value["machine_translations"]) if original_value.has_key?("machine_translations") super(new_value) end end end
translatable_fields_list()
click to toggle source
# File lib/decidim/translatable_resource.rb, line 43 def self.translatable_fields_list @translatable_fields end
Public Instance Methods
content_original_language()
click to toggle source
Public: Returns the original language in which the resource was created or
the organization's default locale.
# File lib/decidim/translatable_resource.rb, line 79 def content_original_language field_name = self.class.translatable_fields_list.first field_value = self[field_name] return field_value.except("machine_translations").keys.first if field_value && field_value.except("machine_translations").keys.count == 1 organization.default_locale end
machine_translation()
click to toggle source
Fires a job to start the machine translation process, only if the service is properly configured and the organization has machine translations enabled.
# File lib/decidim/translatable_resource.rb, line 50 def machine_translation return unless Decidim.machine_translation_service_klass organization = try(:organization) return if organization && !organization.enable_machine_translations return if try(:enable_machine_translations) == false Decidim::MachineTranslationResourceJob.perform_later( self, translatable_previous_changes, I18n.locale.to_s ) end
translatable_fields_are_hashes()
click to toggle source
# File lib/decidim/translatable_resource.rb, line 68 def translatable_fields_are_hashes self.class.translatable_fields_list.each do |field| value = send(field).presence next if value.nil? || value.is_a?(Hash) errors.add(field, :invalid) end end
translatable_previous_changes()
click to toggle source
# File lib/decidim/translatable_resource.rb, line 64 def translatable_previous_changes previous_changes.slice(*self.class.translatable_fields_list) end