Class: YamlTranslator::Locale
- Inherits:
-
Object
- Object
- YamlTranslator::Locale
- Defined in:
- lib/yaml-translator/locale.rb
Instance Attribute Summary collapse
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #diff(other) ⇒ Object
-
#initialize(values, lang) ⇒ Locale
constructor
A new instance of Locale.
- #merge(locale) ⇒ Object
- #merge_to(locale) ⇒ Object
- #merge_to_file(file) ⇒ Object
- #merge_to_s(s) ⇒ Object
- #save(dir = Dir.pwd) ⇒ Object
- #save_to(dir) ⇒ Object
- #to_s ⇒ Object
- #to_single_key_hash ⇒ Object
- #translate(translator, options = {}) ⇒ Object
Constructor Details
#initialize(values, lang) ⇒ Locale
Returns a new instance of Locale.
9 10 11 12 |
# File 'lib/yaml-translator/locale.rb', line 9 def initialize(values, lang) @lang = lang @values = values end |
Instance Attribute Details
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
7 8 9 |
# File 'lib/yaml-translator/locale.rb', line 7 def lang @lang end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
7 8 9 |
# File 'lib/yaml-translator/locale.rb', line 7 def values @values end |
Class Method Details
.load(s) ⇒ Object
68 69 70 71 72 |
# File 'lib/yaml-translator/locale.rb', line 68 def load(s) yaml = YAML.load(s) lang = yaml.keys.first # FIXME check support language self.new(yaml, lang) end |
.load_file(file) ⇒ Object
74 75 76 |
# File 'lib/yaml-translator/locale.rb', line 74 def load_file(file) load(File.open(file, &:read)) end |
Instance Method Details
#diff(other) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yaml-translator/locale.rb', line 18 def diff(other) before_seq = to_single_key_hash.map { |k, v| "#{k}: #{v}" } after_seq = other.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 single_key_hash = Hash[diffs.compact] Locale.new(single_key_hash.to_tree, lang) end |
#merge(locale) ⇒ Object
31 32 33 34 35 |
# File 'lib/yaml-translator/locale.rb', line 31 def merge(locale) s = to_single_key_hash o = locale.to_single_key_hash Locale.new(s.merge(o).to_tree, lang) end |
#merge_to(locale) ⇒ Object
37 38 39 |
# File 'lib/yaml-translator/locale.rb', line 37 def merge_to(locale) locale.merge(self) end |
#merge_to_file(file) ⇒ Object
41 42 43 44 |
# File 'lib/yaml-translator/locale.rb', line 41 def merge_to_file(file) target_locale = Locale.load_file(file) target_locale.merge(self) end |
#merge_to_s(s) ⇒ Object
46 47 48 49 |
# File 'lib/yaml-translator/locale.rb', line 46 def merge_to_s(s) target_locale = Locale.load(s) target_locale.merge(self) end |
#save(dir = Dir.pwd) ⇒ Object
51 52 53 |
# File 'lib/yaml-translator/locale.rb', line 51 def save(dir=Dir.pwd) write_file(File.join(dir, "#{lang}.yml")) end |
#save_to(dir) ⇒ Object
55 56 57 |
# File 'lib/yaml-translator/locale.rb', line 55 def save_to(dir) save(dir) end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/yaml-translator/locale.rb', line 63 def to_s YAML.dump(values) end |
#to_single_key_hash ⇒ Object
59 60 61 |
# File 'lib/yaml-translator/locale.rb', line 59 def to_single_key_hash values.to_single_key end |
#translate(translator, options = {}) ⇒ Object
14 15 16 |
# File 'lib/yaml-translator/locale.rb', line 14 def translate(translator, ={}) translator.translate(self, ) end |