Sha256: 7ed91bf9b11ed306e122301cb13fa6e728b2debb873d1b64b8d9ebe90761f286

Contents?: true

Size: 1.48 KB

Versions: 358

Compression:

Stored size: 1.48 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)
      Helper.backticks("file --mime-encoding #{path.shellescape}", print: false).downcase
    end
  end
end

Version data entries

358 entries across 358 versions & 5 rubygems

Version Path
fastlane-2.125.1 frameit/lib/frameit/strings_parser.rb
fastlane-2.126.0.beta.20190602200102 frameit/lib/frameit/strings_parser.rb
fastlane-2.126.0.beta.20190601200101 frameit/lib/frameit/strings_parser.rb
fastlane-2.125.0 frameit/lib/frameit/strings_parser.rb
fastlane-2.125.0.beta.20190531200016 frameit/lib/frameit/strings_parser.rb
fastlane-2.125.0.beta.20190530200013 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190529200025 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190528200017 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190527200027 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190526200015 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190525200111 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190524200022 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190523200029 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190522200012 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190521200022 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190520200026 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190519200032 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190518200049 frameit/lib/frameit/strings_parser.rb
fastlane-2.124.0.beta.20190517200016 frameit/lib/frameit/strings_parser.rb