source: osm/applications/editors/josm/i18n/convpreset.pl@ 31111

Last change on this file since 31111 was 31111, checked in by stoecker, 11 years ago

major speedup of i18n process, add some new features, split core translations into two parts

File size: 5.5 KB
Line 
1#! /usr/bin/perl -w
2
3# Written by Dirk Stöcker <openstreetmap@dstoecker.de>
4# Public domain, no rights reserved.
5
6use strict;
7
8my $item = "";
9my $group;
10my $combo_n;
11my $combo_type;
12my $result = 0;
13my $comment = 0;
14my $vctx;
15
16# This is a simple conversion and in no way a complete XML parser
17# but it works with a default Perl installation
18
19# Print a header to write valid Java code. No line break,
20# so that the input and output line numbers will match.
21print "class trans_preset { void tr(String s){} void f() {";
22
23sub fix($)
24{
25 my ($val) = @_;
26 $val =~ s/'/''/g;
27 $val =~ s/&lt;/</g;
28 $val =~ s/&gt;/>/g;
29 $val =~ s/&amp;/&/g;
30 return $val;
31}
32
33my $linenr = 0;
34while(my $line = <>)
35{
36 ++$linenr;
37 chomp($line);
38 print "tr(\"\"); ";
39 if($line =~ /<item\s+name=(".*?")/)
40 {
41 my $val = fix($1);
42 $item = $group ? "$group$val" : $val;
43 $item =~ s/""/\//;
44 if($line =~ /name_context=(".*?")/)
45 {
46 print "/* item $item */ trc($1, $val);\n";
47 }
48 else
49 {
50 print "/* item $item */ tr($val);\n";
51 }
52 }
53 elsif($line =~ /<group.*\s+name=(".*?")/)
54 {
55 my $gr = fix($1);
56 $group = $group ? "$group$gr" : $gr;
57 $group =~ s/\"\"/\//;
58 if($line =~ /name_context=(".*?")/)
59 {
60 print "/* group $group */ trc($1,$gr);\n";
61 }
62 else
63 {
64 print "/* group $group */ tr($gr);\n";
65 }
66 }
67 elsif($line =~ /<label.*\s+text=" "/)
68 {
69 print "/* item $item empty label */\n";
70 }
71 elsif($line =~ /<label.*\s+text=(".*?")/)
72 {
73 my $text = fix($1);
74 if($line =~ /text_context=(".*?")/)
75 {
76 print "/* item $item label $text */ trc($1,$text);\n";
77 }
78 else
79 {
80 print "/* item $item label $text */ tr($text);\n";
81 }
82 }
83 elsif($line =~ /<text.*\s+text=(".*?")/)
84 {
85 my $n = fix($1);
86 if($line =~ /text_context=(".*?")/)
87 {
88 print "/* item $item text $n */ trc($1,$n);\n";
89 }
90 else
91 {
92 print "/* item $item text $n */ tr($n);\n";
93 }
94 }
95 elsif($line =~ /<check.*\s+text=(".*?")/)
96 {
97 my $n = fix($1);
98 if($line =~ /text_context=(".*?")/)
99 {
100 print "/* item $item check $n */ trc($1,$n);\n";
101 }
102 else
103 {
104 print "/* item $item check $n */ tr($n);\n";
105 }
106 }
107 elsif($line =~ /<role.*\s+text=(".*?")/)
108 {
109 my $n = fix($1);
110 if($line =~ /text_context=(".*?")/)
111 {
112 print "/* item $item role $n */ trc($1,$n);\n";
113 }
114 else
115 {
116 print "/* item $item role $n */ tr($n);\n";
117 }
118 }
119 elsif($line =~ /<optional.*\s+text=(".*?")/)
120 {
121 my $n = fix($1);
122 if($line =~ /text_context=(".*?")/)
123 {
124 print "/* item $item optional $n */ trc($1,$n);\n";
125 }
126 else
127 {
128 print "/* item $item optional $n */ tr($n);\n";
129 }
130 }
131 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
132 {
133 my ($type,$n) = ($1,fix($2));
134 $combo_n = $n;
135 $combo_type = $type;
136 $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
137 # text
138 my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
139 print "/* item $item $type $n */" . ($tctx ? " trc($tctx, $n);" : " tr($n);");
140 # display_values / values
141 my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
142 my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
143 if($vals)
144 {
145 my @combo_values = split "\Q$sp\E" ,$vals;
146 foreach my $val (@combo_values)
147 {
148 next if $val =~ /^[0-9-]+$/; # search for non-numbers
149 $val = fix($val);
150 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
151 }
152 }
153 print "\n";
154 }
155 elsif(!$comment && $line =~ /<list_entry/)
156 {
157 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
158 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
159 if($line =~ /display_value=(".*?")/)
160 {
161 my $val = fix($1);
162 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
163 }
164 else
165 {
166 my $val = fix($value);
167 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
168 }
169 if($line =~ /short_description=(".*?")/)
170 {
171 my $val = fix($1);
172 print "/* item $item $combo_type $combo_n entry $value short description */ tr($val);";
173 }
174 print "\n";
175 }
176 elsif($line =~ /<\/group>/)
177 {
178 $group = 0 if !($group =~ s/(.*\/).*?$//);
179 print "\n";
180 }
181 elsif($line =~ /<\/item>/)
182 {
183 $item = "";
184 print "\n";
185 }
186 elsif($line =~ /<\/(combo|multiselect)/)
187 {
188 $combo_n = "";
189 print "\n";
190 }
191 elsif(!$line)
192 {
193 print "\n";
194 }
195 elsif($line =~ /^\s*$/
196 || $line =~ /<separator *\/>/
197 || $line =~ /<space *\/>/
198 || $line =~ /<\/?optional>/
199 || $line =~ /<key/
200 || $line =~ /<presets/
201 || $line =~ /<checkgroup/
202 || $line =~ /<\/checkgroup/
203 || $line =~ /<\/presets/
204 || $line =~ /roles/
205 || $line =~ /href=/
206 || $line =~ /<!--/
207 || $line =~ /<\?xml/
208 || $line =~ /-->/
209 || $line =~ /<\/?chunk/
210 || $line =~ /<reference/
211 || $line =~ /<preset_link/
212 || $comment)
213 {
214 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
215 print "/* $line */\n";
216 }
217 else
218 {
219 print "/* unparsed line $line */\n";
220 print STDERR "/* unparsed line $linenr $line */\n";
221 $result = 20
222 }
223
224 # note, these two must be in this order ore oneliners aren't handled
225 $comment = 1 if($line =~ /<!--/);
226 $comment = 0 if($line =~ /-->/);
227}
228
229print "}}\n";
230exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.