Sha256: c120de7e35f6785ac43deda00b5c74b789093df15dcc13cf1713e9548636650f

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'yaml'
module BabelI18n
  class Adapter

    attr_accessor :response, :from, :to, :key, :file_name

    def initialize(path, to, key)
      data = YAML::load_file(path)
      @from = BabelI18n::Adapter.get_language_by(data)
      @response = BabelI18n::Adapter.get_response(data)
      @file_name = BabelI18n::Adapter.get_file_name(path, to)
      @to = to
      @key = key
    end

    def convert(response)
      response.each do |key, value|
        convert(value) if value.is_a?(Hash)
        if value.is_a?(String)
          response[key] = BabelI18n::Base.new(value, @key).from(@from).to(@to).translate
        end
      end
    end

    def converted
      { @to => convert(@response) }
    end

    def write_file
      File.open("#{@file_name}", "w") { |f| f.write converted.to_yaml }
    end

    protected

    def self.get_file_name(path, to)
      compost_name = path.split('/').last.split('.')
      compost_name.size > 2 ? "#{compost_name.first}.#{to}.yml" : "#{to}.yml"
    end

    def self.get_language_by(data)
      data.keys[0]
    end

    def self.get_response(data)
      data.delete(get_language_by(data))
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
babel_i18n-0.0.6 lib/babel_i18n/adapter.rb
babel_i18n-0.0.5 lib/babel_i18n/adapter.rb
babel_i18n-0.0.4 lib/babel_i18n/adapter.rb
babel_i18n-0.0.3 lib/babel_i18n/adapter.rb