class YamlTranslator::Locale

Attributes

lang[R]

Public Class Methods

load(s) click to toggle source
# File lib/yaml-translator/locale.rb, line 11
def load(s)
  self.new(YAML.load(s))
end
load_file(file) click to toggle source
# File lib/yaml-translator/locale.rb, line 15
def load_file(file)
  load(File.open(file, &:read))
end
new(config) click to toggle source
Calls superclass method
# File lib/yaml-translator/locale.rb, line 20
def initialize(config)
  super(config)
  @lang = config.keys.first.to_sym # FIXME: check support language
end

Public Instance Methods

diff(other_locale) click to toggle source
# File lib/yaml-translator/locale.rb, line 34
def diff(other_locale)
  before_seq = to_single_key_hash.map { |k, v| "#{k}: #{v}" }
  after_seq = other_locale.to_single_key_hash.map { |k, v| "#{k}: #{v}" }
  diffs = Diff::LCS.diff(before_seq, after_seq).flatten.map do |operation|
    type, position, element = *operation
    next if type == '-'
    key, text = *element.split(':')
    [key, text.strip]
  end
  tree_hash = tree_of(Hash[diffs.compact])
  tree_hash[lang] = {} if tree_hash.empty?
  Locale.new(tree_hash)
end
locale_texts() click to toggle source
# File lib/yaml-translator/locale.rb, line 25
def locale_texts
  self[lang]
end
merge(other_locale) click to toggle source
# File lib/yaml-translator/locale.rb, line 48
def merge(other_locale)
  s = to_single_key_hash
  o = other_locale.to_single_key_hash
  Locale.new(tree_of(s.merge(o)))
end
merge_to(locale) click to toggle source
# File lib/yaml-translator/locale.rb, line 54
def merge_to(locale)
  locale.merge(self)
end
merge_to_file(file) click to toggle source
# File lib/yaml-translator/locale.rb, line 58
def merge_to_file(file)
  target_locale = Locale.load_file(file)
  target_locale.merge(self)
end
merge_to_s(s) click to toggle source
# File lib/yaml-translator/locale.rb, line 63
def merge_to_s(s)
  target_locale = Locale.load(s)
  target_locale.merge(self)
end
save(dir = Dir.pwd, options = {}) click to toggle source

Save the file

@param dir [String] Directory path to save the file @param options [Hash] Options for saving @return int

# File lib/yaml-translator/locale.rb, line 73
def save(dir = Dir.pwd, options = {})
  prefix = options[:prefix] if options.key?(:prefix)
  write_file(File.join(dir, "#{prefix}#{lang}.yml"), options)
end
save_to(dir, options = {}) click to toggle source
# File lib/yaml-translator/locale.rb, line 78
def save_to(dir, options = {})
  save(dir, options)
end
to_s() click to toggle source
# File lib/yaml-translator/locale.rb, line 87
def to_s
  to_yaml
end
to_single_key_hash() click to toggle source

Covert to a flatten hash

# File lib/yaml-translator/locale.rb, line 83
def to_single_key_hash
  compact_of(to_h, KeyPath.new)
end
to_yaml(options = {}) click to toggle source
# File lib/yaml-translator/locale.rb, line 91
def to_yaml(options = {})
  Hash[lang.to_s, locale_texts].to_yaml(options)
end
translate(translator, options = {}) click to toggle source
# File lib/yaml-translator/locale.rb, line 29
def translate(translator, options = {})
  translated_values = translator.translate(compact_of(locale_texts), options)
  Locale.new(Hash[options[:to], tree_of(translated_values)])
end

Private Instance Methods

compact_of(values = {}, path = KeyPath.new) click to toggle source

Covert to a flatten hash

# File lib/yaml-translator/locale.rb, line 98
def compact_of(values = {}, path = KeyPath.new)
  result = {}
  values.each_with_index do |(i, v)|
    path.move_to(i)
    if v.is_a?(Hash)
      result.merge!(compact_of(v, path))
    else
      result[path.to_s] = v
    end
    path.leave
  end
  result
end
tree_of(values) click to toggle source

Returning the flattened structure to the tree structure

@param [Hash] values flatten Hash @return [Hash] translated hash

# File lib/yaml-translator/locale.rb, line 116
def tree_of(values)
  result = {}
  current = result
  values.each do |k, v|
    keys = k.to_s.split('.')
    last_key = keys.pop
    keys.each do |ks|
      current = if current.key?(ks)
                  current[ks]
                else
                  current[ks] = {}
                  current[ks]
                end
    end
    current[last_key] = v
    current = result
  end
  result
end
write_file(file_path, options = {}) click to toggle source
# File lib/yaml-translator/locale.rb, line 136
def write_file(file_path, options = {})
  File.write(file_path, to_yaml(options))
end