Module: ZendeskAppsSupport::BuildTranslation

Included in:
Package
Defined in:
lib/zendesk_apps_support/build_translation.rb

Constant Summary collapse

I18N_TITLE_KEY =
'title'
I18N_VALUE_KEY =
'value'
I18N_KEYS =
[I18N_TITLE_KEY, I18N_VALUE_KEY].freeze

Instance Method Summary collapse

Instance Method Details

#remove_zendesk_keys(scope, translations = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zendesk_apps_support/build_translation.rb', line 24

def remove_zendesk_keys(scope, translations = {})
  scope.each_key do |key|
    context = scope[key]

    if context.is_a?(Hash)

      if translation_hash?(context)
        translations[key] = context[I18N_VALUE_KEY]
      else
        translations[key] ||= {}
        translations[key] = remove_zendesk_keys(context, translations[key])
      end

    else
      translations[key] = context
    end
  end

  translations
end

#to_flattened_namespaced_hash(hash, target_key = nil, prefix = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zendesk_apps_support/build_translation.rb', line 9

def to_flattened_namespaced_hash(hash, target_key = nil, prefix = nil)
  hash.each_with_object({}) do |(key, value), result|
    key = [prefix, key].compact.join('.')
    if value.is_a?(Hash)
      if target_key && translation_hash?(value)
        result[key] = value[target_key]
      else
        result.update(to_flattened_namespaced_hash(value, target_key, key))
      end
    else
      result[key] = value
    end
  end
end