Class: Strings2CSV
- Inherits:
-
Object
- Object
- Strings2CSV
- Defined in:
- lib/csvconverter/strings2csv.rb
Instance Attribute Summary collapse
-
#csv_filename ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename.
-
#default_lang ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename.
-
#filenames ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename.
-
#headers ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename.
Instance Method Summary collapse
- #basename(file_path) ⇒ Object
-
#create_csv_file(keys, strings) ⇒ Object
Create the resulting file.
- #default_headers ⇒ Object
-
#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.
-
#initialize(args = {:filenames => []}) ⇒ Strings2CSV
constructor
A new instance of Strings2CSV.
-
#load_strings(strings_filename) ⇒ Object
Load all strings of a given file.
- #parse_dotstrings_line(line) ⇒ Object
Constructor Details
#initialize(args = {:filenames => []}) ⇒ Strings2CSV
Returns a new instance of Strings2CSV.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/csvconverter/strings2csv.rb', line 6 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_filename ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename
4 5 6 |
# File 'lib/csvconverter/strings2csv.rb', line 4 def csv_filename @csv_filename end |
#default_lang ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename
4 5 6 |
# File 'lib/csvconverter/strings2csv.rb', line 4 def default_lang @default_lang end |
#filenames ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename
4 5 6 |
# File 'lib/csvconverter/strings2csv.rb', line 4 def filenames @filenames end |
#headers ⇒ Object
default_lang is the the column to refer to if a value is missing actually default_lang = default_filename
4 5 6 |
# File 'lib/csvconverter/strings2csv.rb', line 4 def headers @headers end |
Instance Method Details
#basename(file_path) ⇒ Object
72 73 74 75 |
# File 'lib/csvconverter/strings2csv.rb', line 72 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/csvconverter/strings2csv.rb', line 78 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_headers ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/csvconverter/strings2csv.rb', line 19 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/csvconverter/strings2csv.rb', line 51 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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/csvconverter/strings2csv.rb', line 29 def load_strings(strings_filename) strings = ORDERED_HASH_CLASS.new File.open(strings_filename, 'r') do |strings_file| strings_file.read.each_line do |line| hash = self.parse_dotstrings_line(line) strings.merge!(hash) if hash end end strings end |
#parse_dotstrings_line(line) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/csvconverter/strings2csv.rb', line 40 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 |