]> git.openstreetmap.org Git - rails.git/blob - script/locale/yaml2po
Merge 17067 from trunk.
[rails.git] / script / locale / yaml2po
1 #!/usr/bin/env ruby
2 # yaml2po, for converting RoR translation YAML to the standard gettext
3 #          for eventual use with a translation site such as Transifex
4 # Use:
5 #  - To create a 'master' .pot
6 #    yaml2po > translations.pot
7 #  - To create a language's .po (includes from scratch)
8 #    yaml2po de > de.po
9 #  - To create all languages' .pos and a .pot (under /config/locales/po)
10 #    yaml2po --all
11
12 require "yaml"
13 require "optparse"
14
15 LOCALE_DIR = File.dirname(__FILE__) + '/../../config/locales/'
16 EN = YAML::load_file(LOCALE_DIR+'en.yml')
17
18 def iterate(hash, fhash={}, path='', outfile=$stdout)
19   postr = ''
20   hash.each {|key, val|
21     fhash[key] = {} unless fhash.has_key? key
22     if val.is_a? Hash
23       fhash[key] = {} unless fhash[key].is_a? Hash
24       iterate(val, fhash[key], path+key+':', outfile)
25     else
26       outfile.puts "msgctxt \"#{path}#{key}\""
27       outfile.puts "msgid \"#{val}\""
28       outfile.puts "msgstr \"#{fhash[key]}\""
29     end
30   }
31 end
32
33 def lang2po(lang, outfile=$stdout)
34   puts lang
35   oth = {}
36   infile = LOCALE_DIR+lang+'.yml'
37   if File.exists? infile
38     oth = YAML::load_file(infile)
39     oth = oth[lang]
40     iterate(EN['en'], oth, '', outfile)
41   else
42     iterate(EN['en'], {}, '', outfile)
43   end
44 end
45
46 opt = ARGV[0]
47 if opt == '--all'
48   # Produce .po files for all langs, and a .pot template
49   PO_DIR = LOCALE_DIR+'po/'
50   Dir.mkdir(PO_DIR) unless File.directory?(PO_DIR)
51   Dir.glob(LOCALE_DIR+"*.yml") {|filename|
52     lang = File.basename(filename, '.yml')
53     unless lang == 'en'
54       outfile = File.new(PO_DIR+"#{lang}.po", 'w')
55       lang2po(lang, outfile)
56       outfile.close
57     end
58   }
59   outfile = File.new(PO_DIR+"rails_port.pot", 'w')
60   iterate(EN['en'], {}, '', outfile)
61   outfile.close
62 elsif opt
63   lang2po(opt)
64 else
65   iterate(EN['en'])
66 end