Class: CSV2Strings
- Inherits:
-
Object
- Object
- CSV2Strings
- Defined in:
- lib/csvconverter/csv2strings.rb
Instance Attribute Summary collapse
-
#csv_filename ⇒ Object
Returns the value of attribute csv_filename.
-
#default_lang ⇒ Object
Returns the value of attribute default_lang.
-
#default_path ⇒ Object
Returns the value of attribute default_path.
-
#excluded_states ⇒ Object
Returns the value of attribute excluded_states.
-
#keys_column ⇒ Object
Returns the value of attribute keys_column.
-
#langs ⇒ Object
Returns the value of attribute langs.
-
#output_file ⇒ Object
Returns the value of attribute output_file.
-
#state_column ⇒ Object
Returns the value of attribute state_column.
Instance Method Summary collapse
- #create_file_from_path(file_path) ⇒ Object
-
#csv_to_dotstrings(name = self.csv_filename) ⇒ Object
Convert csv file to multiple Localizable.strings files for each column.
- #file_path_for_locale(locale) ⇒ Object
-
#initialize(filename, langs, args = {}) ⇒ CSV2Strings
constructor
A new instance of CSV2Strings.
- #process_header(excludedCols, files, row, index) ⇒ Object
- #process_value(row_value, default_value) ⇒ Object
Constructor Details
#initialize(filename, langs, args = {}) ⇒ CSV2Strings
Returns a new instance of CSV2Strings.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/csvconverter/csv2strings.rb', line 8 def initialize(filename, langs, args = {}) args.merge!({ :excluded_states => [], :state_column => nil, :keys_column => 0}) @csv_filename = filename @langs = langs if !@langs.is_a?(Hash) || @langs.size == 0 raise "wrong format or/and languages parameter" + @langs.inspect end @output_file = (@langs.size == 1) ? args[:output_file] : nil @default_path = args[:default_path].to_s @excluded_states = args[:excluded_states] @state_column = args[:state_column] @keys_column = args[:keys_column] @default_lang = args[:default_lang] end |
Instance Attribute Details
#csv_filename ⇒ Object
Returns the value of attribute csv_filename.
2 3 4 |
# File 'lib/csvconverter/csv2strings.rb', line 2 def csv_filename @csv_filename end |
#default_lang ⇒ Object
Returns the value of attribute default_lang.
3 4 5 |
# File 'lib/csvconverter/csv2strings.rb', line 3 def default_lang @default_lang end |
#default_path ⇒ Object
Returns the value of attribute default_path.
4 5 6 |
# File 'lib/csvconverter/csv2strings.rb', line 4 def default_path @default_path end |
#excluded_states ⇒ Object
Returns the value of attribute excluded_states.
5 6 7 |
# File 'lib/csvconverter/csv2strings.rb', line 5 def excluded_states @excluded_states end |
#keys_column ⇒ Object
Returns the value of attribute keys_column.
5 6 7 |
# File 'lib/csvconverter/csv2strings.rb', line 5 def keys_column @keys_column end |
#langs ⇒ Object
Returns the value of attribute langs.
3 4 5 |
# File 'lib/csvconverter/csv2strings.rb', line 3 def langs @langs end |
#output_file ⇒ Object
Returns the value of attribute output_file.
2 3 4 |
# File 'lib/csvconverter/csv2strings.rb', line 2 def output_file @output_file end |
#state_column ⇒ Object
Returns the value of attribute state_column.
5 6 7 |
# File 'lib/csvconverter/csv2strings.rb', line 5 def state_column @state_column end |
Instance Method Details
#create_file_from_path(file_path) ⇒ Object
29 30 31 32 33 |
# File 'lib/csvconverter/csv2strings.rb', line 29 def create_file_from_path(file_path) path = File.dirname(file_path) FileUtils.mkdir_p path return File.new(file_path,"w") end |
#csv_to_dotstrings(name = self.csv_filename) ⇒ Object
Convert csv file to multiple Localizable.strings files for each column
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/csvconverter/csv2strings.rb', line 79 def csv_to_dotstrings(name = self.csv_filename) files = {} rowIndex = 0 excludedCols = [] defaultCol = 0 nb_translations = 0 CSVParserClass.foreach(name, :quote_char => '"', :col_sep =>',', :row_sep => :auto) do |row| if rowIndex == 0 return unless row.count > 1 #check there's at least two columns else next if row == nil or row[self.keys_column].nil? #skip empty lines (or sections) end row.size.times do |i| next if excludedCols.include? i if rowIndex == 0 #header # ignore all headers not listed in langs to create files (excludedCols << i and next) unless self.langs.has_key?(row[i]) self.process_header(excludedCols, files, row, i) # define defaultCol defaultCol = i if self.default_lang == row[i] elsif !self.state_column || (row[self.state_column].nil? or row[self.state_column] == '' or !self.excluded_states.include? row[self.state_column]) # TODO: add option to strip the constant or referenced language key = row[self.keys_column].strip value = self.process_value(row[i], row[defaultCol]) # files for a given language, i.e could group english US with english UK. localized_files = files[i] if localized_files localized_files.each do |file| nb_translations += 1 file.write "\"#{key}\" = \"#{value}\";\n" end end end end rowIndex += 1 end info = "Created #{files.size} files. Content: #{nb_translations} translations\n" info += "List of created files:\n" # closing I/O files.each do |key,locale_files| locale_files.each do |file| info += "#{file.path.to_s}\n" file.close end end info end |
#file_path_for_locale(locale) ⇒ Object
62 63 64 65 |
# File 'lib/csvconverter/csv2strings.rb', line 62 def file_path_for_locale(locale) require 'pathname' Pathname.new(self.default_path) + "#{locale}.lproj" + "Localizable.strings" end |
#process_header(excludedCols, files, row, index) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/csvconverter/csv2strings.rb', line 35 def process_header(excludedCols, files, row, index) files[index] = [] lang_index = row[index] # create output files here if @output_file # one single file files[index] << self.create_file_from_path(@output_file) else # create one file for each languages if self.langs[lang_index].is_a?(Array) self.langs[lang_index].each do |locale| filename = self.file_path_for_locale(locale) files[index] << self.create_file_from_path(filename) end elsif self.langs[lang_index].is_a?(String) locale = self.langs[lang_index] filename = self.file_path_for_locale(locale) files[index] << self.create_file_from_path(filename) else raise "wrong format or/and languages parameter" end end end |
#process_value(row_value, default_value) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/csvconverter/csv2strings.rb', line 67 def process_value(row_value, default_value) value = row_value.nil? ? default_value : row_value value = "" if value.nil? value.gsub!(/\\*\"/, "\\\"") #escape double quotes value.gsub!(/\s*(\n|\\\s*n)\s*/, "\\n") #replace new lines with \n + strip value.gsub!(/%\s+([a-zA-Z@])([^a-zA-Z@]|$)/, "%\\1\\2") #repair string formats ("% d points" etc) value.gsub!(/([^0-9\s\(\{\[^])%/, "\\1 %") value.strip! return value end |