class Babelish::Csv2Base

Attributes

comments_column[RW]
csv_filename[RW]
csv_separator[RW]
default_lang[RW]
excluded_states[RW]
ignore_lang_path[RW]
keys_column[RW]
langs[RW]
languages[RW]
output_basename[RW]
output_dir[RW]
state_column[RW]

Public Class Methods

new(filename, langs, args = {}) click to toggle source
# File lib/babelish/csv2base.rb, line 13
def initialize(filename, langs, args = {})
  default_args = {
    :excluded_states => [],
    :state_column => nil,
    :keys_column => 0,
    :csv_separator => ","
  }

  args = default_args.merge!(args)
  args = Thor::CoreExt::HashWithIndifferentAccess.new(args)
  @langs = langs

  # check input types
  raise ArgumentError.new("wrong value of filename: #{filename.inspect}") unless filename.is_a?(String)
  if !@langs.is_a?(Hash) || @langs.size == 0
    raise ArgumentError.new("wrong format or/and langs parameter: #{@langs.inspect}")
  end

  @output_basename = args[:output_basename]
  @output_dir = args[:output_dir].to_s
  @csv_filename = filename
  @excluded_states = args[:excluded_states]
  @state_column = args[:state_column]
  @keys_column = args[:keys_column]
  @comments_column = args[:comments_column]
  @default_lang = args[:default_lang]
  @csv_separator = args[:csv_separator]
  @ignore_lang_path = args[:ignore_lang_path]
  @stripping = args[:stripping]
  @languages = []
  @comments = {}
end

Public Instance Methods

comments() click to toggle source
# File lib/babelish/csv2base.rb, line 64
def comments
  @comments  
end
convert(name = @csv_filename) click to toggle source

Convert csv file to multiple Localizable.strings files for each column

# File lib/babelish/csv2base.rb, line 97
def convert(name = @csv_filename)
  rowIndex     = 0
  excludedCols = []
  defaultCol   = 0

  CSV.foreach(name, :quote_char => '"', :col_sep => @csv_separator, :row_sep => :auto) do |row|

    if rowIndex == 0
      #check there's at least two columns
      return unless row.count > 1
    else
      #skip empty lines (or sections)
      next if row == nil or row[@keys_column].nil?
    end

    # go through columns
    row.size.times do |i|
      next if excludedCols.include? i

      #header
      if rowIndex == 0
        # defaultCol can be the keyValue
        defaultCol = i if self.default_lang == row[i]
        # ignore all headers not listed in langs to create files
        (excludedCols << i and next) unless @langs.has_key?(row[i])

        language = Language.new(row[i])
        if @langs[row[i]].is_a?(Array)
          @langs[row[i]].each do |id|
            language.add_language_id(id.to_s)
          end
        else
          language.add_language_id(@langs[row[i]].to_s)
        end
        @languages[i] = language
      elsif !@state_column || (row[@state_column].nil? || row[@state_column] == '' || !@excluded_states.include?(row[@state_column]))
        key = row[@keys_column]
        comment = @comments_column ? row[@comments_column] : nil
        key.strip! if @stripping
        default_value =  self.default_lang ? row[defaultCol] : nil
        value = self.process_value(row[i], default_value)
        @comments[key] = comment
        @languages[i].add_content_pair(key, value)
      end
    end

    rowIndex += 1
  end

  write_content
end
create_file_from_path(file_path) click to toggle source
# File lib/babelish/csv2base.rb, line 46
def create_file_from_path(file_path)
  path = File.dirname(file_path)
  FileUtils.mkdir_p path
  return File.new(file_path, "w")
end
default_filepath() click to toggle source
# File lib/babelish/csv2base.rb, line 78
def default_filepath
  Pathname.new(@output_dir) + "#{output_basename}.#{extension}"
end
extension() click to toggle source
# File lib/babelish/csv2base.rb, line 73
def extension
  #implement in subclass
  ""
end
get_row_format(row_key, row_value, comment = nil, indentation = 0) click to toggle source
# File lib/babelish/csv2base.rb, line 91
def get_row_format(row_key, row_value, comment = nil, indentation = 0)
  # ignoring comment by default
  "\"#{row_key}\"" + " " * indentation + " = \"#{row_value}\""
end
hash_to_output(content = {}) click to toggle source
# File lib/babelish/csv2base.rb, line 176
def hash_to_output(content = {})
  output = ''
  indentation = content.map(&:first).max { |a, b| a.length <=> b.length }.length
  if content && content.size > 0
    content.each do |key, value|
      comment = @comments[key]
      output += get_row_format(key, value, comment, indentation - key.length)
    end
  end
  return output
end
keys() click to toggle source
# File lib/babelish/csv2base.rb, line 52
def keys
  @languages.each do |lang|
    next unless lang
    return lang.content.keys unless lang.content.keys.empty?
  end
  return []
end
language_filepaths(language) click to toggle source
# File lib/babelish/csv2base.rb, line 68
def language_filepaths(language)
  #implement in subclass
  []
end
process_value(row_value, default_value) click to toggle source
# File lib/babelish/csv2base.rb, line 82
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!(/\n/, "\\n") # replace new lines with \n, dont strip
  value.strip! if @stripping
  return value.to_utf8
end
table() click to toggle source
# File lib/babelish/csv2base.rb, line 60
def table
  output_basename
end
write_content() click to toggle source
# File lib/babelish/csv2base.rb, line 149
def write_content
  info = "List of created files:\n"
  count = 0
  @languages.each do |language|
    next if language.nil?

    files = []
    if @ignore_lang_path
      files << create_file_from_path(default_filepath)
    else
      language_filepaths(language).each do |filename|
        files << create_file_from_path(filename)
      end
    end
    files.each do |file|
      file.write hash_to_output(language.content)
      info += "- #{File.absolute_path(file)}\n"
      count += 1

      file.close
    end
  end

  info = "Created #{count} files.\n" + info
  return info
end