Class: Strings2CSV

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Strings2CSV.

Raises:

  • (ArgumentError)


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

def initialize(args = {:filenames => []})
  raise ArgumentError.new("No filenames given") unless args[:filenames]
  if 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

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



5
6
7
# File 'lib/csvconverter/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/csvconverter/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/csvconverter/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/csvconverter/strings2csv.rb', line 5

def headers
  @headers
end

Instance Method Details

#basename(file_path) ⇒ Object



78
79
80
81
# File 'lib/csvconverter/strings2csv.rb', line 78

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

#create_csv_file(keys, strings) ⇒ Object

Create the resulting file



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/csvconverter/strings2csv.rb', line 84

def create_csv_file(keys, 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]
      @filenames.each do |fname|
        lang = fname
        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



20
21
22
23
24
25
26
# File 'lib/csvconverter/strings2csv.rb', line 20

def default_headers
  headers = ["Variables"]
  @filenames.each do |fname|
    headers << fname
  end
  headers
end

#dotstrings_to_csv(write_to_file = true) ⇒ Object

Convert Localizable.strings files to one CSV file output: strings hash has filename for keys and the content of csv



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/csvconverter/strings2csv.rb', line 57

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

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

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

#load_strings(strings_filename) ⇒ Object

Load all strings of a given file



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/csvconverter/strings2csv.rb', line 30

def load_strings(strings_filename)
  strings = ORDERED_HASH_CLASS.new

  # Make sure we use correct encoding
  contents = File.open(strings_filename).read
  detection = CharlockHolmes::EncodingDetector.detect(contents)
  utf8_encoded_content = CharlockHolmes::Converter.convert contents, detection[:encoding], 'UTF-8'

  utf8_encoded_content.each_line do |line|
    hash = self.parse_dotstrings_line(line)
    strings.merge!(hash) if hash
  end

  strings
end

#parse_dotstrings_line(line) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/csvconverter/strings2csv.rb', line 46

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