Class: Babelish::Strings2CSV

Inherits:
Base2Csv show all
Defined in:
lib/babelish/strings2csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base2Csv

#convert

Constructor Details

#initialize(args = {:filenames => []}) ⇒ Strings2CSV

Returns a new instance of Strings2CSV.



7
8
9
# File 'lib/babelish/strings2csv.rb', line 7

def initialize(args = {:filenames => []})
  super(args)
end

Instance Attribute Details

#csv_filenameObject

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



5
6
7
# File 'lib/babelish/strings2csv.rb', line 5

def csv_filename
  @csv_filename
end

#default_langObject

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



5
6
7
# File 'lib/babelish/strings2csv.rb', line 5

def default_lang
  @default_lang
end

#filenamesObject

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



5
6
7
# File 'lib/babelish/strings2csv.rb', line 5

def filenames
  @filenames
end

#headersObject

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



5
6
7
# File 'lib/babelish/strings2csv.rb', line 5

def headers
  @headers
end

Instance Method Details

#load_strings(strings_filename) ⇒ Object

Load all strings of a given file



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



19
20
21
22
23
24
25
# 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) ⇒ Object



11
12
13
14
15
16
17
# 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