Sha256: 3176848c88ee736379452912a00ff74a82f871bbbedb9b852ce7ca1d89949dd3

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'yaml'

module Bling
  module API
    class Translator

      I18N_FILE ||= YAML
        .load_file(File.join(File.dirname(__FILE__), 'locales/translations.yml'))
        .each_with_object({}){|(k,v), result| result[k.to_sym] = v.to_sym}

      class << self
        def translate_url(key, id: nil)
          url = pt[key.to_sym].to_s
          url +="/#{id}" if id
          url
        end

        def translate_hash(hash, to: :en)
          conversion_hash = send(to)

          hash.inject({}) do |result, (key, value)|
            new_key = conversion_hash[underscore_symbol(key)]
            new_key = underscore_symbol(key) if new_key.nil?

            new_value = case value
                        when Hash then translate_hash(value, to: to)
                        when Array then value.map! { |v| translate_hash(v, to: to) }
                        else value
                        end

            result[new_key] = new_value
            result
          end
        end

        private

        def underscore_symbol(key)
          key.to_s.
            gsub(/::/, '/').
            gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
            gsub(/([a-z\d])([A-Z])/,'\1_\2').
            tr("-", "_").
            downcase.
            to_sym
        end

        def en
          I18N_FILE
        end

        def pt
          @pt ||= I18N_FILE
            .dup
            .each_with_object({}) { |(key, value), result|  result[value] = key }
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bling-ruby-api-0.0.1 lib/bling/api/translator.rb