]> git.openstreetmap.org Git - rails.git/blob - script/locale/po2yaml
Add /user_blocks/ to robots.txt disallow
[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     unless hash.has_key? key
15       hash[key] = {}
16     end
17     add_translation(hash[key], keys, value)
18   end
19   hash
20 end
21
22 def po2hash(f)
23   trs = {}
24   path = []
25   msgstr = ''
26   f.each_line { |line|
27     line.strip!
28     if line[0..8] == 'msgctxt "'
29       path = line[9..-2].split(':')
30     elsif line[0..7] == 'msgstr "'
31       msgstr = line[8..-2]
32     end
33     
34     if !path.empty? and !msgstr.empty?
35       add_translation(trs, path, msgstr)
36       path = []
37       msgstr = ''
38     end
39   }
40   trs
41 end
42
43 filename = ARGV[0]
44 pofile = File.open(filename, "r")
45
46 langcode = File.basename(filename, '.po')
47
48 tr = {langcode => po2hash(pofile)}
49
50 print tr.to_yaml