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

Last change on this file since 24832 was 24764, checked in by bastik, 14 years ago

adapted i18n script to new preset format

File size: 4.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_values;
12my $combo_idx;
13my $comment = 0;
14
15# This is a simple conversion and in no way a complete XML parser
16# but it works with a default Perl installation
17
18# Print a header to write valid Java code. No line break,
19# so that the input and output line numbers will match.
20print "class trans_preset { void tr(String s){} void f() {";
21
22while(my $line = <>)
23{
24 chomp($line);
25 if($line =~ /<item\s+name=(".*?")/)
26 {
27 my $val = $1;
28 $item = $group ? "$group$val" : $val;
29 $item =~ s/""/\//;
30 if($line =~ /name_context=(".*?")/)
31 {
32 print "/* item $item */ trc($1, $val);\n";
33 }
34 else
35 {
36 print "/* item $item */ tr($val);\n";
37 }
38 }
39 elsif($line =~ /<group.*\s+name=(".*?")/)
40 {
41 my $gr = $1;
42 $group = $group ? "$group$gr" : $gr;
43 $group =~ s/\"\"/\//;
44 if($line =~ /name_context=(".*?")/)
45 {
46 print "/* group $group */ trc($1,$gr);\n";
47 }
48 else
49 {
50 print "/* group $group */ tr($gr);\n";
51 }
52 }
53 elsif($line =~ /<label.*\s+text=" "/)
54 {
55 print "/* item $item empty label */\n";
56 }
57 elsif($line =~ /<label.*\s+text=(".*?")/)
58 {
59 my $text = $1;
60 if($line =~ /text_context=(".*?")/)
61 {
62 print "/* item $item label $text */ trc($1,$text);\n";
63 }
64 else
65 {
66 print "/* item $item label $text */ tr($text);\n";
67 }
68 }
69 elsif($line =~ /<text.*\s+text=(".*?")/)
70 {
71 my $n = $1;
72 if($line =~ /text_context=(".*?")/)
73 {
74 print "/* item $item text $n */ trc($1,$n);\n";
75 }
76 else
77 {
78 print "/* item $item text $n */ tr($n);\n";
79 }
80 }
81 elsif($line =~ /<check.*\s+text=(".*?")/)
82 {
83 my $n = $1;
84 if($line =~ /text_context=(".*?")/)
85 {
86 print "/* item $item check $n */ trc($1,$n);\n";
87 }
88 else
89 {
90 print "/* item $item check $n */ tr($n);\n";
91 }
92 }
93 elsif($line =~ /<role.*\s+text=(".*?")/)
94 {
95 my $n = $1;
96 if($line =~ /text_context=(".*?")/)
97 {
98 print "/* item $item role $n */ trc($1,$n);\n";
99 }
100 else
101 {
102 print "/* item $item role $n */ tr($n);\n";
103 }
104 }
105 # first handle display values
106 elsif($line =~ /<combo.*\s+text=(".*?").*\s+display_values="(.*?)"/)
107 {
108 my ($n,$vals) = ($1,$2);
109 $combo_n = $n;
110 $combo_idx = 0;
111 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
112 if($line =~ /text_context=(".*?")/)
113 {
114 print "/* item $item combo $n */ trc($1,$n);";
115 }
116 else
117 {
118 print "/* item $item combo $n */ tr($n);";
119 }
120 $vals =~ s/\\,/\x91/g;
121 @combo_values = split ",",$vals;
122 for (my $i=0; $i<@combo_values; ++$i) {
123 $combo_values[$i] =~ s/\x91/,/g;
124 next if $combo_values[$i] =~ /^[0-9-]+$/; # search for non-numbers
125 print $vctx ? " trc($vctx, \"$combo_values[$i]\");" : " tr(\"$combo_values[$i]\");";
126 }
127 print "\n";
128 }
129 elsif($line =~ /<combo.*\s+text=(".*?").*\s+values="(.*?)"/)
130 {
131 my ($n,$vals) = ($1,$2);
132 $combo_n = $n;
133 $combo_idx = 0;
134 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
135 if($line =~ /text_context=(".*?")/)
136 {
137 print "/* item $item combo $n */ trc($1,$n);";
138 }
139 else
140 {
141 print "/* item $item combo $n */ tr($n);";
142 }
143 @combo_values = split ",",$vals;
144 foreach my $val (@combo_values)
145 {
146 next if $val =~ /^[0-9-]+$/; # search for non-numbers
147 print $vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");";
148 }
149 print "\n";
150 }
151 elsif($line =~ /<short_description>(.*?)<\/short_description>/)
152 {
153 my $n = $1;
154 print "/* item $item combo $combo_n item \"$combo_values[$combo_idx]\" short description */ tr(\"$n\");\n";
155 $combo_idx++;
156 }
157 elsif($line =~ /<\/group>/)
158 {
159 $group = 0 if !($group =~ s/(.*\/).*?$//);
160 print "\n";
161 }
162 elsif($line =~ /<\/item>/)
163 {
164 $item = "";
165 print "\n";
166 }
167 elsif($line =~ /<\/combo/)
168 {
169 $combo_n = "";
170 $combo_idx = 0;
171 }
172 elsif(!$line)
173 {
174 print "\n";
175 }
176 elsif($line =~ /^\s*$/
177 || $line =~ /<separator *\/>/
178 || $line =~ /<space *\/>/
179 || $line =~ /<\/?optional>/
180 || $line =~ /<key/
181 || $line =~ /annotations/
182 || $line =~ /roles/
183 || $line =~ /href=/
184 || $line =~ /<!--/
185 || $line =~ /-->/
186 || $comment)
187 {
188 print "// $line\n";
189 }
190 else
191 {
192 print "/* unparsed line $line */\n";
193# print STDERR "Unparsed line $line\n";
194 }
195
196 # note, these two must be in this order ore oneliners aren't handled
197 $comment = 1 if($line =~ /<!--/);
198 $comment = 0 if($line =~ /-->/);
199}
200
201print "}}\n";
Note: See TracBrowser for help on using the repository browser.