forked from unho/yaml2po
-
Notifications
You must be signed in to change notification settings - Fork 1
/
po2yaml
executable file
·105 lines (87 loc) · 2.57 KB
/
po2yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env ruby
#
# po2yaml, for converting gettext .po to the RoR translation YAML
#
# Developed from scripts found in http://git.openstreetmap.org/rails.git/tree/HEAD:/script/locale
#
# Usage:
# - To create a language's yaml from a given po file
# po2yaml de.po de.yml
#
#
# Copyright (C) 2012 Leandro Regueiro <leandro.regueiro AT gmail DOT com>
# Copyright (C) 2009 Thomas Wood
# Copyright (C) 2009 Tom Hughes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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.strip!
if line[0..8] == 'msgctxt "'
path = line[9..-2].split(':')
elsif line[0..7] == 'msgstr "'
# TODO convert escaped characters
msgstr = line[8..-2]
end
if !path.empty? and !msgstr.empty?
add_translation(trs, path, msgstr)
path = []
msgstr = ''
end
}
trs
end
def generate_yaml(filename, outfilename, langcode)
if File.exist? filename
pofile = File.open(filename, "r")
tr = {langcode => po2hash(pofile)}
outfile = File.new(outfilename, "w")
outfile.puts(tr.to_yaml)
outfile.close()
else
$stderr.puts("\nError: Specified PO file \"#{filename}\" does not exist.\n\n")
end
end
def print_usage()
puts("\nUsage:\n")
puts(" - To create a language's yaml from a given po file\n")
puts(" po2yaml de.po de.yml\n\n")
end
if ARGV.size == 2
langcode = File.basename(ARGV[0], '.po')
generate_yaml(ARGV[0], ARGV[1], langcode)
exit
end
if ARGV.size == 3
generate_yaml(ARGV[0], ARGV[1], ARGV[2])
exit
end
print_usage()