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)
 
  19   hash.each do |key, val|
 
  20     fhash[key] = {} unless fhash.key? key
 
  22       fhash[key] = {} unless fhash[key].is_a? Hash
 
  23       iterate(val, fhash[key], path + key + ":", outfile)
 
  25       outfile.puts "msgctxt \"#{path}#{key}\""
 
  26       outfile.puts "msgid \"#{val}\""
 
  27       outfile.puts "msgstr \"#{fhash[key]}\""
 
  32 def lang2po(lang, outfile = $stdout)
 
  34   infile = LOCALE_DIR + lang + ".yml"
 
  36     oth = YAML.load_file(infile)
 
  38     iterate(EN["en"], oth, "", outfile)
 
  40     iterate(EN["en"], {}, "", outfile)
 
  46   # Produce .po files for all langs, and a .pot template
 
  47   PO_DIR = LOCALE_DIR + "po/"
 
  48   Dir.mkdir(PO_DIR) unless File.directory?(PO_DIR)
 
  49   Dir.glob(LOCALE_DIR + "*.yml") do |filename|
 
  50     lang = File.basename(filename, ".yml")
 
  52       outfile = File.new(PO_DIR + "#{lang}.po", "w")
 
  53       lang2po(lang, outfile)
 
  57   outfile = File.new(PO_DIR + "rails_port.pot", "w")
 
  58   iterate(EN["en"], {}, "", outfile)