Class: YamlTranslator::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml-translator/locale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, lang) ⇒ Locale

Returns a new instance of Locale.



8
9
10
11
# File 'lib/yaml-translator/locale.rb', line 8

def initialize(values, lang)
  @lang = lang
  @values = values
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



6
7
8
# File 'lib/yaml-translator/locale.rb', line 6

def lang
  @lang
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'lib/yaml-translator/locale.rb', line 6

def values
  @values
end

Class Method Details

.load_file(file) ⇒ Object



46
47
48
49
50
# File 'lib/yaml-translator/locale.rb', line 46

def load_file(file)
  lang = File.basename(file).gsub(/.(yml|yaml)/, '')
  yaml = YAML.load(File.open(file, &:read))
  self.new(yaml, lang)
end

Instance Method Details

#diff(other) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yaml-translator/locale.rb', line 17

def diff(other)
  before_seq = flatten_hash.map { |k, v| "#{k}: #{v}" }
  after_seq = other.flatten_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
  Locale.new(Hash[diffs.compact], lang)
end

#flatten_hashObject



37
38
39
# File 'lib/yaml-translator/locale.rb', line 37

def flatten_hash
  flatten(values)
end

#save(dir = Dir.pwd) ⇒ Object



29
30
31
# File 'lib/yaml-translator/locale.rb', line 29

def save(dir=Dir.pwd)
  write_file(File.join(dir, "#{lang}.yml"))
end

#save_to(dir) ⇒ Object



33
34
35
# File 'lib/yaml-translator/locale.rb', line 33

def save_to(dir)
  save(dir)
end

#to_sObject



41
42
43
# File 'lib/yaml-translator/locale.rb', line 41

def to_s
  YAML.dump(values)
end

#translate(translator, options = {}) ⇒ Object



13
14
15
# File 'lib/yaml-translator/locale.rb', line 13

def translate(translator, options={})
  translator.translate(self, options)
end