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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#!/usr/bin/env ruby
if ARGV.length < 2
$stderr.puts "Usage: gen-i-reverse INPUT-FILE OUTPUT-FILE"
exit 1
end
input = File.open(ARGV[0])
output = File.open(ARGV[1], "w")
$state = :idle
$singular = nil
$plural = nil
def reset_state
$state = :idle
$singular = nil
$plural = nil
end
def translate(s)
items = s.split(/ /)
items = items.map do |chunk|
case chunk
when /^%/
chunk
else
chunk.split(/(\\n|\W+)/).map do |c|
c =~ /^\w/ ? c.reverse : c
end.join
end
end
items.join(" ").gsub("\n", "\\n")
end
while line = input.gets
line.chomp!
case $state
when :idle
case line
when /^msgid ""$/
$state = :copy
output.puts line
when /^msgid "(.*)"$/
$state = :msgid
$singular = $1
output.puts line
when /^msgid `(.*)$/
$state = :msgid_multi
$singular = $1.gsub('"', "\\\"") + "\n"
end
when :copy
if line == ""
reset_state
end
output.puts line
when :msgid_multi
case line
# Note that PO files are not supposed to contain backtick-delimited strings,
# but xgotext emits them anyway, so we fix them up until it gets fixed.
when /^(.*)`$/
$state = :msgid
$singular += $1.gsub('"', "\\\"")
output.puts "msgid \"#{$singular.gsub("\n", "\\n")}\""
else
$singular += line.gsub('"', "\\\"") + "\n"
end
when :msgid_plural_multi
case line
when /^(.*)`$/
$state = :msgid
$plural += $1.gsub('"', "\\\"")
output.puts "msgid_plural \"#{$plural.gsub("\n", "\\n")}\""
else
$plural += line.gsub('"', "\\\"") + "\n"
end
when :msgid
case line
when /^msgid_plural ""$/
output.puts line
when /^msgid_plural "(.*)"$/
$plural = $1
output.puts line
when /^msgid_plural `(.*)$/
$state = :msgid_plural_multi
$plural = $1.gsub('"', "\\\"") + "\n"
output.puts line
when /^msgstr(\[0\])? ""$/
output.puts "msgstr#{$1} \"#{translate($singular)}\""
when /^msgstr\[1\] ""$/
output.puts "msgstr[1] \"#{translate($plural)}\""
when ""
reset_state
output.puts line
end
end
end
|