]> git.openstreetmap.org Git - rails.git/blobdiff - script/locale/yaml2po
Replace quova with support for local Maxmind GeoIP lookups
[rails.git] / script / locale / yaml2po
index b680a06402f66f0cc970d4bad090dfb7b13f0474..dbfa1eb7467dfd761c544e696f6310cf4f56f451 100755 (executable)
@@ -4,34 +4,61 @@
 # Use:
 #  - To create a 'master' .pot
 #    yaml2po > translations.pot
-#  - To create a partucular language's .po
+#  - To create a language's .po (includes from scratch)
 #    yaml2po de > de.po
+#  - To create all languages' .pos and a .pot (under /config/locales/po)
+#    yaml2po --all
 
 require "yaml"
+require "optparse"
 
-LOCALE_DIR = File.dirname(__FILE__) + '/../../config/locales/'
+LOCALE_DIR = File.dirname(__FILE__) + "/../../config/locales/"
+EN = YAML.load_file(LOCALE_DIR + "en.yml")
 
-def iterate(hash, fhash={}, path='')
-  hash.each {|key, val|
-    unless fhash.has_key? key
-     fhash[key] = {}
-    end
+def iterate(hash, fhash = {}, path = "", outfile = $stdout)
+  hash.each do |key, val|
+    fhash[key] = {} unless fhash.key? key
     if val.is_a? Hash
-      iterate(val, fhash[key], path+key+':')
+      fhash[key] = {} unless fhash[key].is_a? Hash
+      iterate(val, fhash[key], path + key + ":", outfile)
     else
-      puts "#: #{path}#{key}"
-      puts "msgid \"#{val}\""
-      puts "msgstr \"#{fhash[key]}\""
+      outfile.puts "msgctxt \"#{path}#{key}\""
+      outfile.puts "msgid \"#{val}\""
+      outfile.puts "msgstr \"#{fhash[key]}\""
     end
-  }
+  end
 end
 
-language = ARGV[0]
-oth = {}
-if language
-  oth = YAML::load_file(LOCALE_DIR+language+'.yml')
-  oth = oth[language]
+def lang2po(lang, outfile = $stdout)
+  puts lang
+  infile = LOCALE_DIR + lang + ".yml"
+  if File.exist? infile
+    oth = YAML.load_file(infile)
+    oth = oth[lang]
+    iterate(EN["en"], oth, "", outfile)
+  else
+    iterate(EN["en"], {}, "", outfile)
+  end
 end
 
-en = YAML::load_file(LOCALE_DIR+'en.yml')
-iterate(en['en'], oth)
+opt = ARGV[0]
+if opt == "--all"
+  # Produce .po files for all langs, and a .pot template
+  PO_DIR = LOCALE_DIR + "po/"
+  Dir.mkdir(PO_DIR) unless File.directory?(PO_DIR)
+  Dir.glob(LOCALE_DIR + "*.yml") do |filename|
+    lang = File.basename(filename, ".yml")
+    unless lang == "en"
+      outfile = File.new(PO_DIR + "#{lang}.po", "w")
+      lang2po(lang, outfile)
+      outfile.close
+    end
+  end
+  outfile = File.new(PO_DIR + "rails_port.pot", "w")
+  iterate(EN["en"], {}, "", outfile)
+  outfile.close
+elsif opt
+  lang2po(opt)
+else
+  iterate(EN["en"])
+end