Class: Strings2CSV::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/strings2csv/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Converter.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/strings2csv/converter.rb', line 6

def initialize(args = {:filenames => []})
  if args[:filenames] && args[:headers]
    raise ArgumentError.new("number of headers and files don't match, don't forget the constant column") unless args[:headers].size == (args[:filenames].size + 1)
  end

  @filenames = args[:filenames]     

  @csv_filename = args[:csv_filename] || "translations.csv"
  @default_lang = args[:default_lang]
  @headers = args[:headers] || self.default_headers
end

Instance Attribute Details

#csv_filenameObject

Returns the value of attribute csv_filename.



4
5
6
# File 'lib/strings2csv/converter.rb', line 4

def csv_filename
  @csv_filename
end

#default_langObject

Returns the value of attribute default_lang.



4
5
6
# File 'lib/strings2csv/converter.rb', line 4

def default_lang
  @default_lang
end

#filenamesObject

Returns the value of attribute filenames.



4
5
6
# File 'lib/strings2csv/converter.rb', line 4

def filenames
  @filenames
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/strings2csv/converter.rb', line 4

def headers
  @headers
end

Instance Method Details

#basename(file_path) ⇒ Object



70
71
72
73
# File 'lib/strings2csv/converter.rb', line 70

def basename(file_path)
  filename = File.basename(file_path)
  return filename.split('.')[0].to_sym if file_path
end

#create_csv_file(keys, lang_order, strings) ⇒ Object

Create the resulting file



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/strings2csv/converter.rb', line 76

def create_csv_file(keys, lang_order, strings)
  raise "csv_filename must not be nil" unless self.csv_filename
  CSVParserClass.open(self.csv_filename, "wb") do |csv|
    csv << @headers
    keys.each do |key|
      line = [key]
      default_val = strings[self.default_lang][key] if strings[self.default_lang]
      lang_order.each do |lang|
        current_val = strings[lang][key]
        line << ((lang != self.default_lang and current_val == default_val) ? '' : current_val)
      end
      csv << line
    end
    puts "Done"
  end
end

#default_headersObject



18
19
20
21
22
23
# File 'lib/strings2csv/converter.rb', line 18

def default_headers
  headers = ['Variables']
  @filenames.each do |fname|
    headers << basename(fname)
  end
end

#dotstrings_to_csv(write_to_file = true) ⇒ Object

Convert Localizable.strings files to one CSV file output:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/strings2csv/converter.rb', line 48

def dotstrings_to_csv(write_to_file = true)
  # Parse .strings files
  strings = {}
  keys = nil
  lang_order = []

  @filenames.each do |fname|
    header = basename(fname)
    strings[header] = load_strings(fname)
    lang_order << header       
    keys ||= strings[header].keys
  end

  if(write_to_file)
    # Create csv file
    puts "Creating #{@csv_filename}"
    create_csv_file(keys, lang_order, strings)        
  else
    return keys, lang_order, strings
  end
end

#load_strings(strings_filename) ⇒ Object

Load all strings of a given file



27
28
29
30
31
32
33
34
35
# File 'lib/strings2csv/converter.rb', line 27

def load_strings(strings_filename)
  strings = {}
  File.open(strings_filename, 'r') do |strings_file|
    strings_file.read.each_line do |line|     
      strings.merge!(self.parse_dotstrings_line(line))
    end
  end
  strings
end

#parse_dotstrings_line(line) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/strings2csv/converter.rb', line 37

def parse_dotstrings_line(line)
  line.strip!
  if (line[0] != ?# and line[0] != ?=)
    m = line.match(/^[^\"]*\"(.+)\"[^=]+=[^\"]*\"(.*)\";/)
    return {m[1] => m[2]} unless m.nil?
  end
end