Class: YamlTranslator::Locale

Inherits:
OpenStruct
  • 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(config) ⇒ Locale

Returns a new instance of Locale.



20
21
22
23
# File 'lib/yaml-translator/locale.rb', line 20

def initialize(config)
  super(config)
  @lang = config.keys.first.to_sym # FIXME: check support language
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



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

def lang
  @lang
end

Class Method Details

.load(s) ⇒ Object



11
12
13
# File 'lib/yaml-translator/locale.rb', line 11

def load(s)
  self.new(YAML.load(s))
end

.load_file(file) ⇒ Object



15
16
17
# File 'lib/yaml-translator/locale.rb', line 15

def load_file(file)
  load(File.open(file, &:read))
end

Instance Method Details

#diff(other_locale) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_textsObject



25
26
27
# File 'lib/yaml-translator/locale.rb', line 25

def locale_texts
  self[lang]
end

#merge(other_locale) ⇒ Object



48
49
50
51
52
# 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) ⇒ Object



54
55
56
# File 'lib/yaml-translator/locale.rb', line 54

def merge_to(locale)
  locale.merge(self)
end

#merge_to_file(file) ⇒ Object



58
59
60
61
# 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) ⇒ Object



63
64
65
66
# 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 = {}) ⇒ Object

Save the file

Parameters:

  • dir (String) (defaults to: Dir.pwd)

    Directory path to save the file

  • options (Hash) (defaults to: {})

    Options for saving

Returns:

  • int



73
74
75
76
# 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 = {}) ⇒ Object



78
79
80
# File 'lib/yaml-translator/locale.rb', line 78

def save_to(dir, options = {})
  save(dir, options)
end

#to_sObject



87
88
89
# File 'lib/yaml-translator/locale.rb', line 87

def to_s
  to_yaml
end

#to_single_key_hashObject

Covert to a flatten hash



83
84
85
# File 'lib/yaml-translator/locale.rb', line 83

def to_single_key_hash
  compact_of(to_h, KeyPath.new)
end

#to_yaml(options = {}) ⇒ Object



91
92
93
# 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 = {}) ⇒ Object



29
30
31
32
# 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