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

Last change on this file since 31634 was 31634, checked in by simon04, 9 years ago

JOSM/i18n: Option to prevent translation of values in presets - #josm11696

File size: 5.6 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 $vals = undef if ($line =~ /values_no_i18n="true"/);
144 if($vals)
145 {
146 my @combo_values = split "\Q$sp\E" ,$vals;
147 foreach my $val (@combo_values)
148 {
149 next if $val =~ /^[0-9-]+$/; # search for non-numbers
150 $val = fix($val);
151 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
152 }
153 }
154 print "\n";
155 }
156 elsif(!$comment && $line =~ /<list_entry/)
157 {
158 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
159 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
160 if($line =~ /display_value=(".*?")/)
161 {
162 my $val = fix($1);
163 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
164 }
165 else
166 {
167 my $val = fix($value);
168 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
169 }
170 if($line =~ /short_description=(".*?")/)
171 {
172 my $val = fix($1);
173 print "/* item $item $combo_type $combo_n entry $value short description */ tr($val);";
174 }
175 print "\n";
176 }
177 elsif($line =~ /<\/group>/)
178 {
179 $group = 0 if !($group =~ s/(.*\/).*?$//);
180 print "\n";
181 }
182 elsif($line =~ /<\/item>/)
183 {
184 $item = "";
185 print "\n";
186 }
187 elsif($line =~ /<\/(combo|multiselect)/)
188 {
189 $combo_n = "";
190 print "\n";
191 }
192 elsif(!$line)
193 {
194 print "\n";
195 }
196 elsif($line =~ /^\s*$/
197 || $line =~ /<separator *\/>/
198 || $line =~ /<space *\/>/
199 || $line =~ /<\/?optional>/
200 || $line =~ /<key/
201 || $line =~ /<presets/
202 || $line =~ /<checkgroup/
203 || $line =~ /<\/checkgroup/
204 || $line =~ /<\/presets/
205 || $line =~ /roles/
206 || $line =~ /href=/
207 || $line =~ /<!--/
208 || $line =~ /<\?xml/
209 || $line =~ /-->/
210 || $line =~ /<\/?chunk/
211 || $line =~ /<reference/
212 || $line =~ /<preset_link/
213 || $line =~ /<item_separator\/>/
214 || $comment)
215 {
216 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
217 print "/* $line */\n";
218 }
219 else
220 {
221 print "/* unparsed line $line */\n";
222 print STDERR "/* unparsed line $linenr $line */\n";
223 $result = 20
224 }
225
226 # note, these two must be in this order ore oneliners aren't handled
227 $comment = 1 if($line =~ /<!--/);
228 $comment = 0 if($line =~ /-->/);
229}
230
231print "}}\n";
232exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.