module StringsXmlLocalizer

Constants

DEFAULT_FROM
DEFAULT_SOURCE_LANGUAGE
DEFAULT_TARGET_LANGUANGE
DEFAULT_TO
VERSION
XML_HEADER

Public Class Methods

default_options() click to toggle source
# File lib/strings_xml_localizer.rb, line 57
def self.default_options
  {
    from: DEFAULT_FROM,
    to: DEFAULT_TO,
    sl: DEFAULT_SOURCE_LANGUAGE,
    tl: DEFAULT_TARGET_LANGUANGE
  }
end
file_to_file(options = {}) click to toggle source
# File lib/strings_xml_localizer.rb, line 7
def self.file_to_file(options = {})
  options = default_options.merge(options)
  string_to_file(File.open(options[:from],"r:UTF-8",&:read), options)
end
file_to_string(options = {}) click to toggle source
# File lib/strings_xml_localizer.rb, line 12
def self.file_to_string(options = {})
  options = default_options.merge(options)
  string_to_string(File.open(options[:from],"r:UTF-8",&:read))
end
generate_out_resources(in_resources, options = {}) click to toggle source
# File lib/strings_xml_localizer.rb, line 36
def self.generate_out_resources(in_resources, options = {})
  out_resources = Ox::Element.new('resources')
  i = 0
  while 1
    begin
      out = Ox::Element.new('string')
      out[:name] = in_resources.string(i).attributes[:name]
      out << translate_string_at(in_resources, i, options[:sl], options[:tl])
      out_resources << out
      i += 1
    rescue
      break
    end
  end
  out_resources
end
string_to_file(src, options = {}) click to toggle source
# File lib/strings_xml_localizer.rb, line 17
def self.string_to_file(src, options = {})
  options = default_options.merge(options)
  File.open(options[:to], 'w') do |f|
    f.write(XML_HEADER)
    f.write(string_to_string(src))
  end
end
string_to_string(src, options = {}) click to toggle source
# File lib/strings_xml_localizer.rb, line 25
def self.string_to_string(src, options = {})
  in_xml = Ox.parse(src)
  output = Ox::Document.new(version: '1.0', encoding: 'utf-8')

  in_resources = in_xml.resources
  out_resources = generate_out_resources(in_resources, options)
  output << out_resources

  Ox.dump(output)
end
translate_string_at(resources, index, from, to) click to toggle source
# File lib/strings_xml_localizer.rb, line 53
def self.translate_string_at(resources, index, from, to)
  GoTranslator.translate(resources.string(index).text, from: from, to: to)
end