-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_converter.rb
65 lines (53 loc) · 1.83 KB
/
string_converter.rb
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/ruby
# based on https://github.com/tmurakam/cashflow/blob/0a01ac9e0350dfb04979986444244f8daf4cb5a8/android/convertStrings.rb
# support comments and Converter such as "%@", "%d", "%0.1f"...
# in your directory : ./main.rb Localizable.strings
puts '------------------------------------------'
puts 'converting ' + ARGV[0] + ', please wait...'
input_file = File.open(ARGV[0], "r")
file = File.open(ARGV[1], "w+")
file.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
file.puts "<resources>"
multiple_line_comment = false
input_file.each do |line|
if (line =~ /\"(.*)\"\s*=\s*\"(.*)\"/)
name = $1
value = $2
name.gsub!(/[ .]/, "_")
value.gsub!(/&/, "&")
value.gsub!(/</, "<")
# convert %@ to %1$s
i = 0
value.gsub!(/%([0-9.]*[@sScCdoxXfeEgabBhH])/) {|s|
i += 1
match = $1
match.gsub!(/@/, "s")
"%#{i}$#{match}"
}
# convert all @ to s just in case
value.gsub!(/@/, "s")
file.puts " <string name=\"#{name}\">#{value}</string>"
# one line comment // The cake is a lie
# multiple line comment on one line /* The cake is a lie */
elsif (line =~ /\/\/(.*)/ || line =~ /\/\*(.*)\*\//)
file.puts "<!--#{$1}-->"
# multiple line comment (start)
elsif (line =~ /\/\*(.*)/)
file.puts "<!--#{$1}"
multiple_line_comment = true
# multiple line comment (middle or end)
elsif (multiple_line_comment)
#end of the multiple line comment
if (line =~ /(.*)\*\//)
file.puts "#{$1}-->"
multiple_line_comment = false
else
file.puts line
end
elsif (line =~ /\n/)
file.puts line
end
end
file.puts "</resources>"
puts '----> done creating - ' + ARGV[1]
puts '------------------------------------------'