class Babelish::Strings2CSV

Attributes

csv_filename[RW]

default_lang is the the column to refer to if a value is missing actually default_lang = default_filename

default_lang[RW]

default_lang is the the column to refer to if a value is missing actually default_lang = default_filename

filenames[RW]

default_lang is the the column to refer to if a value is missing actually default_lang = default_filename

headers[RW]

default_lang is the the column to refer to if a value is missing actually default_lang = default_filename

Public Class Methods

new(args = {:filenames => []}) click to toggle source
Calls superclass method Babelish::Base2Csv::new
# File lib/babelish/strings2csv.rb, line 7
def initialize(args = {:filenames => []})
  super(args)
end

Public Instance Methods

load_strings(strings_filename) click to toggle source

Load all strings of a given file

# File lib/babelish/strings2csv.rb, line 28
def load_strings(strings_filename)
  strings = {}
  comments = {}
  # genstrings uses utf16, so that's what we expect. utf8 should not be impact
  file = File.open(strings_filename, "r:utf-16:utf-8")
  begin
    contents = file.read
    if RUBY_VERSION == "1.9.2"
      # fixes conversion, see http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
      require 'iconv'
      ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
      contents = ic.iconv(contents + ' ')[0..-2]
    end
  rescue Encoding::InvalidByteSequenceError => e
    # silent error
    # faults back to utf8
    contents = File.open(strings_filename, "r:utf-8")
  end
  previous_comment = nil
  contents.each_line do |line|
    key, value = self.parse_dotstrings_line(line)
    if key
      strings.merge!({key => value}) 
      comments[key] = previous_comment if previous_comment
    else
      previous_comment = self.parse_comment_line(line)
    end
  end

  [strings, comments]
end
parse_comment_line(line) click to toggle source
# File lib/babelish/strings2csv.rb, line 19
def parse_comment_line(line)
  line.strip!
  if line[0] != ?# && line[0] != ?=
    m = line.match(/^\/\*(.*)\*\/\s*$/)
    return m[1].strip! unless m.nil?
  end
end
parse_dotstrings_line(line) click to toggle source
# File lib/babelish/strings2csv.rb, line 11
def parse_dotstrings_line(line)
  line.strip!
  if line[0] != ?# && line[0] != ?= && line[0] != ?/
    m = line.match(/^[\s*]*\"(.+)\"[\s]*=\s*\"(.*)\"\s*;/)
    return m[1], m[2] unless m.nil?
  end
end