6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'frameit/lib/frameit/strings_parser.rb', line 6
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 = {}
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` end
content.split("\n").each_with_index do |line, index|
begin
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
|