git-lfs/script/gen-i-reverse
brian m. carlson 9daf439cf0
Add support for a fake locale for testing
Sometimes we'd like to test to see how much of or whether our code is
localized.  However, it's very burdensome to create a full localization
in order to do so.  To help us, let's create a special, autogenerated
locale, i-reverse, which is generated by simply reversing all the
English words.  Add a script to do this automatically for us.

However, don't generate this locale on typical builds, since users might
not have xgotext installed, and adding this locale would bloat the
binary needlessly in most cases.
2022-01-18 17:05:02 +00:00

98 lines
2.1 KiB
Ruby
Executable File

#!/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