Sha256: 0d96b38f33a2fd2166f83290e9c146b9f98c8ee2f5e04e5ca062952232336f88

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module ZendeskAppsSupport
  module BuildTranslation

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

    def to_flattened_namespaced_hash(hash, prefix = nil)

      hash.inject({}) do |result, (key, value)|
        key = [ prefix, key ].compact.join('.')

        if value.kind_of?(Hash)

          if is_translation_hash?(value)
            result[key] = value[I18N_VALUE_KEY]
          else
            result.update( to_flattened_namespaced_hash(value, key) )
          end

        else
          result[key] = value
        end
        result
      end

    end

    def remove_zendesk_keys(scope, translations = {})

      scope.each_key do |key|
        context = scope[key]

        if context.is_a?(Hash)

          if is_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

    private

    def is_translation_hash?(hash)
      hash.keys.sort == I18N_KEYS
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zendesk_apps_support-1.7.0 lib/zendesk_apps_support/build_translation.rb