module PIM::Services::TranslationService

Constants

CATEGORY_ATTRIBUTE_NAME
DATA_MODEL_ATTRIBUTE_TYPE
TYPE_DESCRIPTION
TYPE_ICON
TYPE_LABEL

Public Instance Methods

__translation_service() click to toggle source
# File services.rb, line 4250
def __translation_service
  __service(:translationService)
end
get_attribute(attribute_name) click to toggle source
# File services.rb, line 4235
def get_attribute(attribute_name)

  attribute = PIM.active_module.attribute(attribute_name.to_sym)

  if attribute.nil?
    PIM.all_modules.each do |module_name, data_module|
      attribute = data_module.attribute(attribute_name.to_sym)
      return attribute if attribute
    end
  end

  attribute
end
get_default_attribute_label(locales, attribute) click to toggle source
# File services.rb, line 4224
def get_default_attribute_label locales, attribute
  opts = { prefix: 'attribute.' }
  if attribute.is_a? PIM::Attribute
    opts[:default_value] = attribute.label
    translate_value(locales, attribute.name, opts)
  else
    translate_value(locales, attribute, opts)
  end
end
get_translation_from_object(attribute, type, lookup_languages, default_value_fn) click to toggle source
# File services.rb, line 4199
def get_translation_from_object(attribute, type, lookup_languages, default_value_fn)
  translation_map_key = "#{type}s".to_sym

  # Ignore non-objects or 'empty' objects
  unless attribute.is_a?(PIM::Attribute) && attribute.respond_to?(translation_map_key)
    return default_value_fn.call(lookup_languages, attribute)
  end

  result = nil
  translation_map = attribute.send translation_map_key

  # Try translations from translation map first
  if translation_map && !translation_map.empty?
    lookup_languages.each do |lookup_language|
      translation = translation_map[lookup_language]
      result = translation unless translation.nil?
    end
  end

  # Use default label/icon or key
  result ||= attribute[type] || default_value_fn.call(lookup_languages, attribute)
  result
end
get_translations(locale) click to toggle source
# File services.rb, line 3948
def get_translations locale
  all_translations = (PIM.active_module.cache['translations'] ||= {})
  locale_translations = all_translations[locale]
  if locale_translations.nil?
    locale_translations = __service(:translationService).getTranslationForLocale(locale)
    all_translations[locale] = locale_translations
  end
  locale_translations
end
translate(locale, key, params = {}) click to toggle source
# File services.rb, line 3958
def translate locale, key, params = {}
  key = key.to_s
  params = PIM::Utils.unsymbolized_hash(params)
  translations = get_translations(locale)
  translations.translate(key, params)
end
translate_attribute_label(lookup_languages, attribute) click to toggle source
# File services.rb, line 4194
def translate_attribute_label lookup_languages, attribute
  get_translation_from_object(attribute, 'label', lookup_languages, method(:get_default_attribute_label))
end
translate_codelist_key(locale, codelist, codelist_key, opts = {}) click to toggle source
# File services.rb, line 3965
def translate_codelist_key locale, codelist, codelist_key, opts = {}

  pair = PIM::Services::CodelistService.get_codelist_entry_pair(locale, codelist, codelist_key)
  value = pair.value unless pair.nil?
  value ||= codelist_key unless opts[:no_default_option_key]

  return value

end
translate_option_key(locale, option_key, opts = {}) click to toggle source
# File services.rb, line 3999
def translate_option_key locale, option_key, opts = {}

  type = opts[:type] || TYPE_LABEL
  attribute_or_name = opts[:attribute]
  option_attributes = (opts[:option_attributes] ||= [])

  option_key = option_key.to_s

  unless attribute_or_name.nil?

    attribute = PIM.active_module.attribute(attribute_or_name)
    unless attribute.nil?

      attribute_name = attribute.name
      option_attributes << attribute_name

      type_name = attribute.type_name
      codelist = attribute.param(:codelist)
      if not codelist.nil?
        return translate_codelist_key(locale, codelist, option_key, opts)
      end

      value_attribute = attribute.param(:value_attribute)
      if not value_attribute.nil?
        opts[:attribute] = value_attribute
        return translate_option_key(locale, option_key, opts)
      end

      options = attribute.param(:values) || attribute.param(:keys)
      if not PIM.is_hash?(options)
        option_list = options.to_s
        return translate_option_list_key(locale, option_list, option_key, opts)
      end

      if type == TYPE_LABEL
        default_label = PIM.get_value(options, option_key)
      end

    else
      option_attributes << attribute_or_name
    end

  end

  option_attributes.each do |attribute_name|

    prefix = 'attribute.' + attribute_name.to_s + '.' + case type.to_s
    when TYPE_DESCRIPTION
      'description'
    when TYPE_ICON
      'icon'
    else
      'option'
    end
    value = translate_value(locale, option_key, prefix: prefix, default_value: nil)

    return value unless value.nil?

  end

  prefix = case type
  when TYPE_DESCRIPTION
    'option_description'
  when TYPE_ICON
    'option_icon'
  else
    'option'
  end

  if type == TYPE_LABEL
    default_value = default_label
    default_value ||= option_key unless opts[:no_default_option_key]
  end
  value = translate_value(locale, option_key, prefix: prefix, default_value: default_value)

  return value

end
translate_option_list_key(locale, option_list, option_list_key, opts = {}) click to toggle source
# File services.rb, line 3975
def translate_option_list_key locale, option_list, option_list_key, opts = {}

  type = opts[:type] || TYPE_LABEL
  prefix = 'optionList.' + option_list.to_s + '.' + case type.to_s
  when TYPE_DESCRIPTION
    'description'
  when TYPE_ICON
    'icon'
  else
    'option'
  end
  value = translate_value(locale, option_list_key, prefix: prefix, default_value: nil)

  return value unless value.nil?

  # Get default label from option-list, if exists
  option_list = PIM.active_module.option_list(option_list)
  value = PIM.get_value(option_list.options, option_list_key) unless option_list.nil?
  value ||= option_list_key unless opts[:no_default_option_key]

  return value;

end
translate_validation_message(validation_msg, attribute_name, lookup_languages) click to toggle source
# File services.rb, line 4125
def translate_validation_message(validation_msg, attribute_name, lookup_languages)
  return nil if validation_msg.nil?

  key = "validationmessage.#{validation_msg[:name]}"
  key += ".#{validation_msg[:params][:msg_id]}" if validation_msg[:params]&.dig(:msg_id)

  translate_opts = { params: validation_msg[:params], message_format: true }

  # A prefix to the validation message that indicates in which exact entry the error/warning occurred can be
  # constructed for the following attribute types: MultiDimensional, Dimensional, Group, Collection and EnumSet.
  # The translation interpolcation params are extracted and added to `validation.params` and then that is used to
  # prepare the prefix translation and then the validation message translation.
  translated_prefix = ''
  translated_validation_message = ''
  prefix_key = 'VALIDATION_MESSAGE_LOCATION_PREFIX'

  if validation_msg[:params] && !validation_msg[:path].empty? && validation_msg[:path].length > 1

    exact_attribute_name = validation_msg[:path].last
    exact_attribute = get_attribute(exact_attribute_name)

    if exact_attribute.nil?
      PIM.log_error "Attribute #{exact_attribute_name} used in validation message was not found"
    else
      validation_msg[:params][:err_field] = translate_attribute_label(lookup_languages, exact_attribute)
    end

    # Now depending on the main/parent attribute type, we should add the appropriate prefix and translation parameters.
    parent_attribute = get_attribute(attribute_name)
    if parent_attribute.nil?
      PIM.log_error "Main attribute #{attribute_name} used in validation message was not found"
      return validation_msg[:message]
    end

    second_validation_path_entry = validation_msg[:path][1]

    case parent_attribute.type_name
    when DATA_MODEL_ATTRIBUTE_TYPE[:MULTI_DIMENSIONAL]
      prefix_key += '.MULTI_DIMENSIONAL'
    when DATA_MODEL_ATTRIBUTE_TYPE[:DIMENSIONAL]
      prefix_key += '.DIMENSIONAL'
    else
      validation_msg[:params][:err_row] = second_validation_path_entry + 1

      case parent_attribute.type_name
      when DATA_MODEL_ATTRIBUTE_TYPE[:GROUP]
        error_index = validation_msg[:path][-3]
        if error_index.is_a?(Numeric)
          validation_msg[:params][:err_row] = error_index + 1
        end
      when DATA_MODEL_ATTRIBUTE_TYPE[:COLLECTION]
        prefix_key += '.COLLECTION'
      when DATA_MODEL_ATTRIBUTE_TYPE[:ENUM_SET]
        prefix_key += '.ENUM_SET'
      end
    end

    translated_prefix = translate_value(lookup_languages, prefix_key, translate_opts)
  end

  translated_validation_message = translate_value(lookup_languages, key, translate_opts)

  if translated_validation_message == key || translated_prefix == prefix_key
    validation_msg[:message]
  else
    "#{translated_prefix} #{translated_validation_message}".strip
  end
end
translate_validation_messages(validations, lookup_languages) click to toggle source
# File services.rb, line 4115
def translate_validation_messages(validations, lookup_languages)
  validations.each do |attribute_name, validation_msgs|
    validation_msgs.each do |validation_msg|
      validation_msg[:translated_message] = translate_validation_message(validation_msg,
                                                                         attribute_name.to_s,
                                                                         lookup_languages)
    end
  end
end
translate_value(locale, key, opts = {}) click to toggle source

@param locale [String, Array<String>] locale(s) to translate the value for.

# File services.rb, line 4079
def translate_value locale, key, opts = {}

  locale_list = []

  if locale.is_a? Array
    locale_list = locale.dup
    locale = locale_list.shift
  end

  key_prefix = opts[:prefix]
  key_prefix += '.' unless key_prefix.nil? or key_prefix.end_with?('.')
  key_suffix = opts[:suffix]
  key_suffix = '.' + key_suffix unless key_suffix.nil? or key_suffix.start_with?('.')

  property_key = "#{key_prefix}#{key}#{key_suffix}"
  params = opts[:params]
  translations = get_translations(locale)

  default_value = opts.key?(:default_value) ? opts[:default_value] : key
  return default_value if translations.nil?

  unless opts.key?(:message_format)
    value = translations.translate(property_key, params)
  else
    value = translations.translate_message_format(property_key, params)
  end

  # Value was translated
  return value if value != property_key

  # Value was not translated; try to translate it for an alternate locale
  return translate_value(locale_list, key, opts) unless locale_list.empty?

  default_value
end