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

Last change on this file since 36398 was 36396, checked in by stoecker, 3 days ago

fix dupliacte combo outputs in i18n translations

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