]> git.openstreetmap.org Git - rails.git/blob - script/locale/yaml2po
Use an API key for thunderforest tile layers
[rails.git] / script / locale / yaml2po
1 #!/usr/bin/env ruby
2 # yaml2po, for converting RoR translation YAML to the standard gettext
3 #          for eventual use with a translation site such as Transifex
4 # Use:
5 #  - To create a 'master' .pot
6 #    yaml2po > translations.pot
7 #  - To create a language's .po (includes from scratch)
8 #    yaml2po de > de.po
9 #  - To create all languages' .pos and a .pot (under /config/locales/po)
10 #    yaml2po --all
11
12 require "yaml"
13 require "optparse"
14
15 LOCALE_DIR = File.dirname(__FILE__) + "/../../config/locales/"
16 EN = YAML.load_file(LOCALE_DIR + "en.yml")
17
18 def iterate(hash, fhash = {}, path = "", outfile = $stdout)
19   hash.each do |key, val|
20     fhash[key] = {} unless fhash.key? key
21     if val.is_a? Hash
22       fhash[key] = {} unless fhash[key].is_a? Hash
23       iterate(val, fhash[key], path + key + ":", outfile)
24     else
25       outfile.puts "msgctxt \"#{path}#{key}\""
26       outfile.puts "msgid \"#{val}\""
27       outfile.puts "msgstr \"#{fhash[key]}\""
28     end
29   end
30 end
31
32 def lang2po(lang, outfile = $stdout)
33   puts lang
34   infile = LOCALE_DIR + lang + ".yml"
35   if File.exist? infile
36     oth = YAML.load_file(infile)
37     oth = oth[lang]
38     iterate(EN["en"], oth, "", outfile)
39   else
40     iterate(EN["en"], {}, "", outfile)
41   end
42 end
43
44 opt = ARGV[0]
45 if opt == "--all"
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")
51     unless lang == "en"
52       outfile = File.new(PO_DIR + "#{lang}.po", "w")
53       lang2po(lang, outfile)
54       outfile.close
55     end
56   end
57   outfile = File.new(PO_DIR + "rails_port.pot", "w")
58   iterate(EN["en"], {}, "", outfile)
59   outfile.close
60 elsif opt
61   lang2po(opt)
62 else
63   iterate(EN["en"])
64 end