class LocGen

Constants

LANG_KEY
LOCAL_INDEX_KEY
OUTPUT_FILE_NAME
SHEET_INDEX_KEY
TEMPORARY_SHEET_FILE

Public Class Methods

new(url, outputDir, comment, key, baselang) click to toggle source
# File lib/LocGen.rb, line 12
def initialize(url, outputDir, comment, key, baselang)
  @url = url
  @outputDir = outputDir
  @commentColumnName = comment || "comment"
  @keyColumnName = key || "key"
  @baseLang = baselang
end

Public Instance Methods

process() click to toggle source
# File lib/LocGen.rb, line 20
def process()
  downloadFile(@url, TEMPORARY_SHEET_FILE)
  if File.exists?(TEMPORARY_SHEET_FILE)
    parseXLSX(TEMPORARY_SHEET_FILE)
    prepareIndices()
    readLocalizations()
    writeFiles(@outputDir, OUTPUT_FILE_NAME)
    writeBaseLocalizable(@outputDir, OUTPUT_FILE_NAME)
    deleteTempFile(TEMPORARY_SHEET_FILE)
  end
end

Private Instance Methods

deleteTempFile(tempFileName) click to toggle source
# File lib/LocGen.rb, line 43
def deleteTempFile(tempFileName)
  File.delete(tempFileName)
end
downloadFile(url, tempFileName) click to toggle source
# File lib/LocGen.rb, line 33
def downloadFile(url, tempFileName)
  File.open(tempFileName, "wb") do |saved_file|
    # the following "open" is provided by open-uri
    open(url, "rb") do |read_file|
      saved_file.write(read_file.read)
    end
  end
end
parseXLSX(file) click to toggle source
# File lib/LocGen.rb, line 48
def parseXLSX(file)
  @xlsx = RubyXL::Parser.parse(file)
  @sheet = @xlsx[0]
end
prepareIndices() click to toggle source
# File lib/LocGen.rb, line 54
def prepareIndices()
  @languages = []
  @localizations = []

  localIndex = 0
  firstRow = @sheet[0]

  firstRow && firstRow.cells.each_with_index { |cell,cellIndex|
    val = cell && cell.value
    # keyIndex column index
    if val == @keyColumnName
      @keyIndex = cellIndex
    elsif val == @commentColumnName
    # commentIndex column index
      @commentIndex = cellIndex
    else
      lang = val && ISO_639.find_by_code(val)
      if lang && lang.length > 0
        hash = {LANG_KEY => val, SHEET_INDEX_KEY => cellIndex, LOCAL_INDEX_KEY => localIndex}
        if !@languages.include?(hash)
          @languages.push(hash)
          @localizations[localIndex] = []
          localIndex = @localizations.length
        end
      end
    end
  }
end
readLocalizations() click to toggle source
# File lib/LocGen.rb, line 84
def readLocalizations()
  @sheet.each_with_index { |row,rowIndex|
    if rowIndex != 0
      if row
        # localized key
        if row.cells[@keyIndex]
          next if !row.cells[@keyIndex].value || row.cells[@keyIndex].value == ""
          key = row.cells[@keyIndex].value
        end
        # localized comment
        if row.cells[@commentIndex]
          comment = row.cells[@commentIndex].value
        end

        @languages.each_with_index{|hash,index|
          if row.cells[hash[SHEET_INDEX_KEY]]
            localization = row.cells[hash[SHEET_INDEX_KEY]].value
            unless key.to_s.strip.empty?
              @localizations[hash[LOCAL_INDEX_KEY]].push("//#{comment}\n\"#{key}\" = \"#{localization}\";")
            end
          end
        }
      end
    end
  }
end
writeBaseLocalizable(dir, fileName) click to toggle source
# File lib/LocGen.rb, line 124
def writeBaseLocalizable(dir, fileName)
  selectedHash = @languages[0]
  if @baseLang && @baseLang.length == 2
    hashes = @languages.select { |item|
      item[LANG_KEY] == @baseLang
    }
    if hashes.length > 0
      selectedHash = hashes[0]
    end
  end

  localIndex = selectedHash["localIndex"]
  dirName = "Base.lproj"
  dirPath = File.join(dir, dirName)
  Dir.mkdir(dirPath) unless File.exists?(dirPath)
  File.write("#{dirPath}/#{fileName}", @localizations[localIndex].join("\n\n"))
end
writeFiles(dir, fileName) click to toggle source
# File lib/LocGen.rb, line 112
def writeFiles(dir, fileName)
  @languages.each_with_index{ |hash,index|
    localIndex = hash["localIndex"]
    langId = hash["lang"]
    dirName = "#{langId}.lproj"
    dirPath = File.join(dir, dirName)
    Dir.mkdir(dirPath) unless File.exists?(dirPath)
    File.write("#{dirPath}/#{fileName}", @localizations[localIndex].join("\n\n"))
  }
end