Add the counterpart script, po2yaml, and use strctxt rather than #: for identifying...
authorThomas Wood <grand.edgemaster@gmail.com>
Fri, 5 Jun 2009 19:03:39 +0000 (19:03 +0000)
committerThomas Wood <grand.edgemaster@gmail.com>
Fri, 5 Jun 2009 19:03:39 +0000 (19:03 +0000)
script/locale/po2yaml [new file with mode: 0755]
script/locale/yaml2po

diff --git a/script/locale/po2yaml b/script/locale/po2yaml
new file mode 100755 (executable)
index 0000000..f2d2eb9
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/env ruby
+# po2yaml, for converting gettext .po to the RoR translation YAML
+# Use:
+#  - To create a language's yaml from a given po file
+#    po2yaml de.po > de.yml
+
+require "yaml"
+
+def add_translation(hash, keys, value)
+  key = keys.shift
+  if keys.empty?
+    hash[key] = value
+  else
+    unless hash.has_key? key
+      hash[key] = {}
+    end
+    add_translation(hash[key], keys, value)
+  end
+  hash
+end
+
+def po2hash(f)
+  trs = {}
+  path = []
+  msgstr = ''
+  f.each_line { |line|
+    line = line.strip
+    if line[0..8] == 'msgctxt "'
+      path = line[9..-2].split(':')
+    elsif line[0..7] == 'msgstr "'
+      msgstr = line[8..-2]
+    end
+    
+    if !path.empty? and !msgstr.empty?
+      add_translation(trs, path, msgstr)
+      path = []
+      msgstr = ''
+    end
+  }
+  trs
+end
+
+filename = ARGV[0]
+pofile = File.open(filename, "r")
+
+langcode = File.basename(filename, '.po')
+
+tr = {langcode => po2hash(pofile)}
+
+print tr.to_yaml
index b680a06..1837b06 100755 (executable)
@@ -4,7 +4,9 @@
 # Use:
 #  - To create a 'master' .pot
 #    yaml2po > translations.pot
-#  - To create a partucular language's .po
+#  - To create a language's .po from scratch
+#    yaml2po > de.po
+#  - To create a partucular language's .po from existing translations
 #    yaml2po de > de.po
 
 require "yaml"
@@ -19,7 +21,7 @@ def iterate(hash, fhash={}, path='')
     if val.is_a? Hash
       iterate(val, fhash[key], path+key+':')
     else
-      puts "#: #{path}#{key}"
+      puts "msgctxt \"#{path}#{key}\""
       puts "msgid \"#{val}\""
       puts "msgstr \"#{fhash[key]}\""
     end