Class: Translatr::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/translatr/merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_filename = nil, target_filename = nil) ⇒ Merger

Returns a new instance of Merger.



9
10
11
12
# File 'lib/translatr/merger.rb', line 9

def initialize(source_filename = nil, target_filename = nil)
  load_source(source_filename) if source_filename
  load_target(target_filename) if target_filename
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def source
  @source
end

#source_filenameObject

Returns the value of attribute source_filename.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def source_filename
  @source_filename
end

#source_localeObject

Returns the value of attribute source_locale.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def source_locale
  @source_locale
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def target
  @target
end

#target_filenameObject

Returns the value of attribute target_filename.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def target_filename
  @target_filename
end

#target_localeObject

Returns the value of attribute target_locale.



6
7
8
# File 'lib/translatr/merger.rb', line 6

def target_locale
  @target_locale
end

Instance Method Details

#has_variables?(text_string) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/translatr/merger.rb', line 61

def has_variables?(text_string)
  !text_string.gsub(/%\{(\w+)\}/).to_a.empty?
end

#load_source(filename) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/translatr/merger.rb', line 14

def load_source(filename)
  self.source_filename = filename
  I18n.backend.reload!
  I18n.backend.load_translations(filename)
  self.source_locale = I18n.backend.available_locales.first
  self.source = I18n.backend.send(:translations)[source_locale]
end

#load_target(filename) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/translatr/merger.rb', line 22

def load_target(filename)
  self.target_filename = filename
  I18n.backend.reload!
  I18n.backend.load_translations(filename)
  self.target_locale = I18n.backend.available_locales.first
  self.target = I18n.backend.send(:translations)[target_locale]
end

#matching_variables?(target_string, source_string) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/translatr/merger.rb', line 72

def matching_variables?(target_string, source_string)
  return true unless has_variables?(source_string)
  variables_in(target_string) & variables_in(source_string) == variables_in(source_string)
end

#merge(target_hash = nil, source_hash = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/translatr/merger.rb', line 30

def merge(target_hash = nil, source_hash = nil)
  target_hash ||= target
  source_hash ||= source
  source_hash.each_pair do |key, value|
    if target_hash.has_key?(key)
      if value.is_a?(Hash)
        merge(target_hash[key], source_hash[key])
      else
        if matching_variables?(target_hash[key], value)
          target_hash[key] = value
        end
      end
    end
  end
  target_hash
end

#store(filename = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/translatr/merger.rb', line 47

def store(filename = nil)
  filename ||= target_filename
  data = Hash.new
  data[target_locale] = merge
  data = data.deep_stringify_keys
  File.open(filename, "w") do |file|
    if data.respond_to?(:ya2yaml)
      file.write(data.ya2yaml(:syck_compatible => true))
    else
      YAML.dump(data, file)
    end
  end
end

#variables_in(text_string) ⇒ Object



65
66
67
68
69
70
# File 'lib/translatr/merger.rb', line 65

def variables_in(text_string)
  vars = text_string.gsub(/%\{(\w+)\}/).to_a
  return nil if vars.empty?
  return [vars[0]] if vars.size == 1
  vars.uniq
end