2 # yaml2po, for converting RoR translation YAML to the standard gettext
 
   3 #          for eventual use with a translation site such as Transifex
 
   5 #  - To create a 'master' .pot
 
   6 #    yaml2po > translations.pot
 
   7 #  - To create a language's .po (includes from scratch)
 
   9 #  - To create all languages' .pos and a .pot (under /config/locales/po)
 
  15 LOCALE_DIR = File.dirname(__FILE__) + '/../../config/locales/'
 
  16 EN = YAML::load_file(LOCALE_DIR+'en.yml')
 
  18 def iterate(hash, fhash={}, path='', outfile=$stdout)
 
  21     fhash[key] = {} unless fhash.has_key? key
 
  23       fhash[key] = {} unless fhash[key].is_a? Hash
 
  24       iterate(val, fhash[key], path+key+':', outfile)
 
  26       outfile.puts "msgctxt \"#{path}#{key}\""
 
  27       outfile.puts "msgid \"#{val}\""
 
  28       outfile.puts "msgstr \"#{fhash[key]}\""
 
  33 def lang2po(lang, outfile=$stdout)
 
  36   infile = LOCALE_DIR+lang+'.yml'
 
  37   if File.exists? infile
 
  38     oth = YAML::load_file(infile)
 
  40     iterate(EN['en'], oth, '', outfile)
 
  42     iterate(EN['en'], {}, '', outfile)
 
  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')
 
  54       outfile = File.new(PO_DIR+"#{lang}.po", 'w')
 
  55       lang2po(lang, outfile)
 
  59   outfile = File.new(PO_DIR+"rails_port.pot", 'w')
 
  60   iterate(EN['en'], {}, '', outfile)