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

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

fix dupliacte combo outputs in i18n translations

File size: 6.2 KB
RevLine 
[11529]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 = "";
[32241]9my $chunk = "";
[11529]10my $group;
[24764]11my $combo_n;
[28250]12my $combo_type;
[25155]13my $result = 0;
[11529]14my $comment = 0;
[28250]15my $vctx;
[11529]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
[13392]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
[26180]24sub fix($)
25{
26 my ($val) = @_;
27 $val =~ s/'/''/g;
[31076]28 $val =~ s/&lt;/</g;
29 $val =~ s/&gt;/>/g;
30 $val =~ s/&amp;/&/g;
[26180]31 return $val;
32}
33
[32241]34sub infoblock
35{
36 my $r = "";
37 $r .= " item $item" if $item;
38 $r .= " chunk $chunk" if $chunk;
39 $r .= " group $group" if $group;
[36396]40 $r .= " $combo_type $combo_n" if $combo_type;
[32241]41 $r .= " $_[0]" if $_[0];
42 return $r ? "/* $r */ " : "";
43}
44
[29933]45my $linenr = 0;
[11529]46while(my $line = <>)
47{
[29933]48 ++$linenr;
[11529]49 chomp($line);
[31901]50 print "tr(\"---DUMMY-MARKER---\"); ";
[33841]51 if($line =~ /<item\s+name=(".*?")/ || $line =~ /<item.* name=(".*?")/)
[11529]52 {
[26180]53 my $val = fix($1);
[17969]54 $item = $group ? "$group$val" : $val;
[11529]55 $item =~ s/""/\//;
[17969]56 if($line =~ /name_context=(".*?")/)
57 {
[32241]58 print infoblock() . "trc($1, $val);\n";
[17969]59 }
60 else
61 {
[32241]62 print infoblock() . "tr($val);\n";
[17969]63 }
[11529]64 }
[32280]65 elsif($line =~ /<chunk\s+id=(".*?")/)
[32241]66 {
67 $chunk = fix($1);
[36395]68 print "/* $line */\n";
[32241]69 }
[17969]70 elsif($line =~ /<group.*\s+name=(".*?")/)
[11529]71 {
[26180]72 my $gr = fix($1);
[17973]73 $group = $group ? "$group$gr" : $gr;
[17969]74 $group =~ s/\"\"/\//;
75 if($line =~ /name_context=(".*?")/)
76 {
[32241]77 print infoblock() . "trc($1,$gr);\n";
[17969]78 }
79 else
80 {
[32241]81 print infoblock() . "tr($gr);\n";
[17969]82 }
[11529]83 }
[17969]84 elsif($line =~ /<label.*\s+text=" "/)
[11529]85 {
[32241]86 print infoblock("empty label") . "\n";
[11529]87 }
[17969]88 elsif($line =~ /<label.*\s+text=(".*?")/)
[11529]89 {
[26180]90 my $text = fix($1);
[17969]91 if($line =~ /text_context=(".*?")/)
92 {
[32241]93 print infoblock("label $text") ."trc($1,$text);\n";
[17969]94 }
95 else
96 {
[32241]97 print infoblock("label $text") . "tr($text);\n";
[17969]98 }
[11529]99 }
[17969]100 elsif($line =~ /<text.*\s+text=(".*?")/)
[11529]101 {
[26180]102 my $n = fix($1);
[17969]103 if($line =~ /text_context=(".*?")/)
104 {
[32241]105 print infoblock("text $n") . "trc($1,$n);\n";
[17969]106 }
107 else
108 {
[32241]109 print infoblock("text $n") . "tr($n);\n";
[17969]110 }
[11529]111 }
[17969]112 elsif($line =~ /<check.*\s+text=(".*?")/)
[11529]113 {
[26180]114 my $n = fix($1);
[17969]115 if($line =~ /text_context=(".*?")/)
116 {
[32241]117 print infoblock("check $n") . "trc($1,$n);\n";
[17969]118 }
119 else
120 {
[32241]121 print infoblock("check $n") . "tr($n);\n";
[17969]122 }
[11529]123 }
[17969]124 elsif($line =~ /<role.*\s+text=(".*?")/)
[13469]125 {
[26180]126 my $n = fix($1);
[17969]127 if($line =~ /text_context=(".*?")/)
128 {
[32241]129 print infoblock("role $n") . "trc($1,$n);\n";
[17969]130 }
131 else
132 {
[32241]133 print infoblock("role $n") . "tr($n);\n";
[17969]134 }
[13469]135 }
[35422]136 elsif($line =~ /<(optional|preset_link).*\s+text=(".*?")/)
[29933]137 {
[35422]138 my ($type,$n) = ($1,fix($2));
[29933]139 if($line =~ /text_context=(".*?")/)
140 {
[35422]141 print infoblock("$type $n") . "trc($1,$n);\n";
[29933]142 }
143 else
144 {
[35422]145 print infoblock("$type $n") . "tr($n);\n";
[29933]146 }
147 }
[28250]148 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
[11529]149 {
[28250]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;
[36396]156 print infoblock() . ($tctx ? " trc($tctx, $n);" : " tr($n);");
[28250]157 # display_values / values
[30038]158 my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
[31111]159 my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
[31634]160 $vals = undef if ($line =~ /values_no_i18n="true"/);
[28250]161 if($vals)
[17969]162 {
[30038]163 my @combo_values = split "\Q$sp\E" ,$vals;
[28250]164 foreach my $val (@combo_values)
165 {
166 next if $val =~ /^[0-9-]+$/; # search for non-numbers
167 $val = fix($val);
[36396]168 print infoblock("display value") . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
[28250]169 }
[17969]170 }
[11529]171 print "\n";
172 }
[28250]173 elsif(!$comment && $line =~ /<list_entry/)
[11529]174 {
[28250]175 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
176 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
[33841]177 if($line =~ /[^.]display_value=(".*?")/)
[17969]178 {
[28250]179 my $val = fix($1);
[33841]180 print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]181 }
182 else
183 {
[28250]184 my $val = fix($value);
[33841]185 print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]186 }
[28250]187 if($line =~ /short_description=(".*?")/)
[11529]188 {
[28250]189 my $val = fix($1);
[33841]190 print infoblock("entry $value short description") . "tr($val);";
[11529]191 }
[28252]192 print "\n";
[11529]193 }
194 elsif($line =~ /<\/group>/)
195 {
[17969]196 $group = 0 if !($group =~ s/(.*\/).*?$//);
[11529]197 print "\n";
198 }
199 elsif($line =~ /<\/item>/)
200 {
201 $item = "";
202 print "\n";
203 }
[32241]204 elsif($line =~ /<\/chunk>/)
205 {
206 $chunk = "";
[36395]207 print "\n";
[32241]208 }
[31111]209 elsif($line =~ /<\/(combo|multiselect)/)
[24764]210 {
211 $combo_n = "";
[36395]212 $combo_type = "";
[29999]213 print "\n";
[24764]214 }
[35737]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 }
[13469]221 elsif(!$line)
222 {
223 print "\n";
224 }
[11529]225 elsif($line =~ /^\s*$/
[13140]226 || $line =~ /<separator *\/>/
227 || $line =~ /<space *\/>/
228 || $line =~ /<\/?optional>/
[11529]229 || $line =~ /<key/
[25155]230 || $line =~ /<presets/
[29933]231 || $line =~ /<checkgroup/
232 || $line =~ /<\/checkgroup/
[25155]233 || $line =~ /<\/presets/
[13469]234 || $line =~ /roles/
[34809]235 || $line =~ /<link wiki=/
[13469]236 || $line =~ /href=/
[11529]237 || $line =~ /<!--/
[25155]238 || $line =~ /<\?xml/
[11529]239 || $line =~ /-->/
[30159]240 || $line =~ /<\/?chunk/
241 || $line =~ /<reference/
242 || $line =~ /<preset_link/
[31139]243 || $line =~ /<item_separator\/>/
[11529]244 || $comment)
245 {
[31111]246 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
247 print "/* $line */\n";
[11529]248 }
249 else
250 {
251 print "/* unparsed line $line */\n";
[29933]252 print STDERR "/* unparsed line $linenr $line */\n";
[25155]253 $result = 20
[11529]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}
[13392]260
261print "}}\n";
[29208]262exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.