openstreetmap-website/script/locale/po2yaml
2015-02-20 19:47:26 +00:00

49 lines
892 B
Ruby
Executable file

#!/usr/bin/env ruby
# po2yaml, for converting gettext .po to the RoR translation YAML
# Use:
# - To create a language's yaml from a given po file
# po2yaml de.po > de.yml
require "yaml"
def add_translation(hash, keys, value)
key = keys.shift
if keys.empty?
hash[key] = value
else
hash[key] ||= {}
add_translation(hash[key], keys, value)
end
hash
end
def po2hash(f)
trs = {}
path = []
msgstr = ""
f.each_line do |line|
line.strip!
if line[0..8] == 'msgctxt "'
path = line[9..-2].split(":")
elsif line[0..7] == 'msgstr "'
msgstr = line[8..-2]
end
next if path.empty? || msgstr.empty?
add_translation(trs, path, msgstr)
path = []
msgstr = ""
end
trs
end
filename = ARGV[0]
pofile = File.open(filename, "r")
langcode = File.basename(filename, ".po")
tr = { langcode => po2hash(pofile) }
print tr.to_yaml