class Translatr::Merger

Attributes

source[RW]
source_filename[RW]
source_locale[RW]
target[RW]
target_filename[RW]
target_locale[RW]

Public Class Methods

new(source_filename = nil, target_filename = nil) click to toggle source
# 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

Public Instance Methods

has_variables?(text_string) click to toggle source
# File lib/translatr/merger.rb, line 61
def has_variables?(text_string)
  !text_string.gsub(/%\{(\w+)\}/).to_a.empty?
end
load_source(filename) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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