module PIM::Services::TranslationService

Constants

TYPE_DESCRIPTION
TYPE_ICON
TYPE_LABEL

Public Instance Methods

__translation_service() click to toggle source
# File services.rb, line 2760
def __translation_service
  __service(:translationService)
end
get_translations(locale) click to toggle source
# File services.rb, line 2612
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 2622
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_codelist_key(locale, codelist, codelist_key, opts = {}) click to toggle source
# File services.rb, line 2629
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 2663
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 2639
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_value(locale, key, opts = {}) click to toggle source
# File services.rb, line 2742
def translate_value locale, key, opts = {}

  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}"

  translations = get_translations(locale)
  params = opts[:params]
  value = translations.translate(property_key, params) unless translations.nil?

  default_value = opts.key?(:default_value) ? opts[:default_value] : key
  return value == property_key ? default_value : value

end