]> git.openstreetmap.org Git - rails.git/commitdiff
Add bulk yaml production to yaml2po
authorThomas Wood <grand.edgemaster@gmail.com>
Fri, 5 Jun 2009 22:27:21 +0000 (22:27 +0000)
committerThomas Wood <grand.edgemaster@gmail.com>
Fri, 5 Jun 2009 22:27:21 +0000 (22:27 +0000)
script/locale/po2yaml
script/locale/yaml2po

index f2d2eb97cfe4fe7685677d4839ecd1707b63fc4e..3e05363e7e040eafbc0eb975c1bd3245c45b9342 100755 (executable)
@@ -24,7 +24,7 @@ def po2hash(f)
   path = []
   msgstr = ''
   f.each_line { |line|
-    line = line.strip
+    line.strip!
     if line[0..8] == 'msgctxt "'
       path = line[9..-2].split(':')
     elsif line[0..7] == 'msgstr "'
index 1837b068c71719c71dbe169f35919e2b6f2da52e..7a1750df6423c259f8aad5a270cd50f29b336f0e 100755 (executable)
@@ -4,36 +4,61 @@
 # Use:
 #  - To create a 'master' .pot
 #    yaml2po > translations.pot
-#  - To create a language's .po from scratch
-#    yaml2po > de.po
-#  - To create a partucular language's .po from existing translations
+#  - 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/'
+EN = YAML::load_file(LOCALE_DIR+'en.yml')
 
-def iterate(hash, fhash={}, path='')
+def iterate(hash, fhash={}, path='', outfile=$stdout)
+  postr = ''
   hash.each {|key, val|
-    unless fhash.has_key? key
-     fhash[key] = {}
-    end
+    fhash[key] = {} unless fhash.has_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 "msgctxt \"#{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
 
-language = ARGV[0]
-oth = {}
-if language
-  oth = YAML::load_file(LOCALE_DIR+language+'.yml')
-  oth = oth[language]
+def lang2po(lang, outfile=$stdout)
+  puts lang
+  oth = {}
+  infile = LOCALE_DIR+lang+'.yml'
+  if File.exists? 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") {|filename|
+    lang = File.basename(filename, '.yml')
+    outfile = File.new(PO_DIR+"#{lang}.po", 'w')
+    lang2po(lang, outfile)
+    outfile.close
+  }
+  outfile = File.new(PO_DIR+"rails_port.pot", 'w')
+  iterate(EN['en'], {}, '', outfile)
+  outfile.close
+elsif opt
+  lang2po(opt)
+else
+  iterate(EN['en'])
+end