| 1 |
|
| 2 |
|
| 3 |
if ARGV.length < 2 |
| 4 |
$stderr.puts "Usage: gen-i-reverse INPUT-FILE OUTPUT-FILE" |
| 5 |
exit 1 |
| 6 |
end |
| 7 |
|
| 8 |
input = File.open(ARGV[0]) |
| 9 |
output = File.open(ARGV[1], "w") |
| 10 |
|
| 11 |
$state = :idle |
| 12 |
$singular = nil |
| 13 |
$plural = nil |
| 14 |
|
| 15 |
def reset_state |
| 16 |
$state = :idle |
| 17 |
$singular = nil |
| 18 |
$plural = nil |
| 19 |
end |
| 20 |
|
| 21 |
def translate(s) |
| 22 |
items = s.split(/ /) |
| 23 |
items = items.map do |chunk| |
| 24 |
case chunk |
| 25 |
when /^%/ |
| 26 |
chunk |
| 27 |
else |
| 28 |
chunk.split(/(\\n|\W+)/).map do |c| |
| 29 |
c =~ /^\w/ ? c.reverse : c |
| 30 |
end.join |
| 31 |
end |
| 32 |
end |
| 33 |
items.join(" ").gsub("\n", "\\n") |
| 34 |
end |
| 35 |
|
| 36 |
while line = input.gets |
| 37 |
line.chomp! |
| 38 |
case $state |
| 39 |
when :idle |
| 40 |
case line |
| 41 |
when /^msgid ""$/ |
| 42 |
$state = :copy |
| 43 |
output.puts line |
| 44 |
when /^msgid "(.*)"$/ |
| 45 |
$state = :msgid |
| 46 |
$singular = $1 |
| 47 |
output.puts line |
| 48 |
when /^msgid `(.*)$/ |
| 49 |
$state = :msgid_multi |
| 50 |
$singular = $1.gsub('"', "\\\"") + "\n" |
| 51 |
end |
| 52 |
when :copy |
| 53 |
if line == "" |
| 54 |
reset_state |
| 55 |
end |
| 56 |
output.puts line |
| 57 |
when :msgid_multi |
| 58 |
case line |
| 59 |
|
| 60 |
|
| 61 |
when /^(.*)`$/ |
| 62 |
$state = :msgid |
| 63 |
$singular += $1.gsub('"', "\\\"") |
| 64 |
output.puts "msgid \"#{$singular.gsub("\n", "\\n")}\"" |
| 65 |
else |
| 66 |
$singular += line.gsub('"', "\\\"") + "\n" |
| 67 |
end |
| 68 |
when :msgid_plural_multi |
| 69 |
case line |
| 70 |
when /^(.*)`$/ |
| 71 |
$state = :msgid |
| 72 |
$plural += $1.gsub('"', "\\\"") |
| 73 |
output.puts "msgid_plural \"#{$plural.gsub("\n", "\\n")}\"" |
| 74 |
else |
| 75 |
$plural += line.gsub('"', "\\\"") + "\n" |
| 76 |
end |
| 77 |
when :msgid |
| 78 |
case line |
| 79 |
when /^msgid_plural ""$/ |
| 80 |
output.puts line |
| 81 |
when /^msgid_plural "(.*)"$/ |
| 82 |
$plural = $1 |
| 83 |
output.puts line |
| 84 |
when /^msgid_plural `(.*)$/ |
| 85 |
$state = :msgid_plural_multi |
| 86 |
$plural = $1.gsub('"', "\\\"") + "\n" |
| 87 |
output.puts line |
| 88 |
when /^msgstr(\[0\])? ""$/ |
| 89 |
output.puts "msgstr#{$1} \"#{translate($singular)}\"" |
| 90 |
when /^msgstr\[1\] ""$/ |
| 91 |
output.puts "msgstr[1] \"#{translate($plural)}\"" |
| 92 |
when "" |
| 93 |
reset_state |
| 94 |
output.puts line |
| 95 |
end |
| 96 |
end |
| 97 |
end |