Sha256: f18d79e6b5dee20d71cd959f8a0c2e3a5804d7a74e617179ed7a0b4b3404085c

Contents?: true

Size: 1.44 KB

Versions: 162

Compression:

Stored size: 1.44 KB

Contents

require_relative 'module'

module Frameit
  # This class will parse the .string files
  class StringsParser
    def self.parse(path)
      UI.user_error!("Couldn't find strings file at path '#{path}'") unless File.exist?(path)
      UI.user_error!("Must be .strings file, only got '#{path}'") unless path.end_with?(".strings")

      result = {}

      # A .strings file is UTF-16 encoded. We only want to deal with UTF-8
      encoding = encoding_type(path)
      if encoding.include?('utf-8') || encoding.include?('us-ascii')
        content = File.read(path)
      else
        content = `iconv -f UTF-16 -t UTF-8 "#{path}" 2>&1` # note: double quotes around path so command also works on Windows
      end

      content.split("\n").each_with_index do |line, index|
        begin
          # We don't care about comments and empty lines
          if line.start_with?('"')
            key = line.match(/"(.*)" \= /)[1]
            value = line.match(/ \= "(.*)"/)[1]

            result[key] = value
          end
        rescue => ex
          UI.error("Error parsing #{path} line #{index + 1}: '#{line}'")
          UI.verbose("#{ex.message}\n#{ex.backtrace.join('\n')}")
        end
      end

      if result.empty?
        UI.error("Empty parsing result for #{path}. Please make sure the file is valid and UTF16 Big-endian encoded")
      end

      result
    end

    def self.encoding_type(path)
      `file --mime-encoding #{path}`.downcase
    end
  end
end

Version data entries

162 entries across 162 versions & 1 rubygems

Version Path
fastlane-2.117.0.beta.20190217200019 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190216200056 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190215200101 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190214200107 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190213200055 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190212200112 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190211200054 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190210200028 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190209200055 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190208200116 frameit/lib/frameit/strings_parser.rb
fastlane-2.117.0.beta.20190207200055 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190206200014 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190205200046 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190204200009 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190203200034 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190202200022 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190201200019 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190131200016 frameit/lib/frameit/strings_parser.rb
fastlane-2.116.0.beta.20190130200045 frameit/lib/frameit/strings_parser.rb