]> git.openstreetmap.org Git - rails.git/blob - script/locale/po2yaml
Added sortable headers + search + reportable Notes
[rails.git] / script / locale / po2yaml
1 #!/usr/bin/env ruby
2 # po2yaml, for converting gettext .po to the RoR translation YAML
3 # Use:
4 #  - To create a language's yaml from a given po file
5 #    po2yaml de.po > de.yml
6
7 require "yaml"
8
9 def add_translation(hash, keys, value)
10   key = keys.shift
11   if keys.empty?
12     hash[key] = value
13   else
14     hash[key] ||= {}
15
16     add_translation(hash[key], keys, value)
17   end
18   hash
19 end
20
21 def po2hash(f)
22   trs = {}
23   path = []
24   msgstr = ""
25   f.each_line do |line|
26     line.strip!
27     if line[0..8] == 'msgctxt "'
28       path = line[9..-2].split(":")
29     elsif line[0..7] == 'msgstr "'
30       msgstr = line[8..-2]
31     end
32
33     next if path.empty? || msgstr.empty?
34
35     add_translation(trs, path, msgstr)
36     path = []
37     msgstr = ""
38   end
39   trs
40 end
41
42 filename = ARGV[0]
43 pofile = File.open(filename, "r")
44
45 langcode = File.basename(filename, ".po")
46
47 tr = { langcode => po2hash(pofile) }
48
49 print tr.to_yaml