Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/web/translations/translate.pl
Parent Directory
|
Revision Log
Revision 1538 -
(show annotations)
(download)
(as text)
Wed Apr 2 18:11:25 2014 UTC (10 years, 11 months ago) by amb
File MIME type: text/x-perl
File size: 9578 byte(s)
Wed Apr 2 18:11:25 2014 UTC (10 years, 11 months ago) by amb
File MIME type: text/x-perl
File size: 9578 byte(s)
Don't use HTML character entity encoding for UTF-8 characters in the HTML.
1 | #!/usr/bin/perl |
2 | # |
3 | # Routino translation replacement Perl script |
4 | # |
5 | # Part of the Routino routing software. |
6 | # |
7 | # This file Copyright 2014 Andrew M. Bishop |
8 | # |
9 | # This program is free software: you can redistribute it and/or modify |
10 | # it under the terms of the GNU Affero General Public License as published by |
11 | # the Free Software Foundation, either version 3 of the License, or |
12 | # (at your option) any later version. |
13 | # |
14 | # This program is distributed in the hope that it will be useful, |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | # GNU Affero General Public License for more details. |
18 | # |
19 | # You should have received a copy of the GNU Affero General Public License |
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | # |
22 | |
23 | use strict; |
24 | |
25 | # Constants |
26 | |
27 | my @translation_files=(<translation.*.txt>); |
28 | my $xml_output_file="../../xml/routino-translations.xml"; |
29 | my $html_output_dir="../www/routino"; |
30 | |
31 | my @html_template_files=(<*.html>); |
32 | |
33 | my %languages=(); |
34 | my %translations=(); |
35 | |
36 | |
37 | # Read in the translations |
38 | |
39 | foreach my $translation_file (@translation_files) |
40 | { |
41 | $translation_file =~ m%translation.([^.]+).txt%; |
42 | |
43 | # Add to list of languages |
44 | |
45 | my $language=$1; |
46 | |
47 | if(! defined $languages{$language}) |
48 | { |
49 | $languages{$language}=1; |
50 | |
51 | $translations{$language}={}; |
52 | $translations{$language}->{codes}={}; |
53 | $translations{$language}->{html}=0; |
54 | $translations{$language}->{xml}=0; |
55 | } |
56 | |
57 | # Process the file |
58 | |
59 | open(FILE,"<$translation_file"); |
60 | |
61 | while(<FILE>) |
62 | { |
63 | s%\r*\n%%; |
64 | |
65 | next if(m%^#%); |
66 | next if(m%^$%); |
67 | |
68 | # Single line HTML entries |
69 | |
70 | if(m%\@\@%) |
71 | { |
72 | my($code,$text)=split("\t"); |
73 | |
74 | if(defined $translations{$language}->{codes}->{$code}) |
75 | { |
76 | print STDERR "Language: $language DUPLICATED codeword '$code'\n"; |
77 | } |
78 | else |
79 | { |
80 | $translations{$language}->{html}++; |
81 | $translations{$language}->{codes}->{$code}={}; |
82 | $translations{$language}->{codes}->{$code}->{text}=$text; |
83 | $translations{$language}->{codes}->{$code}->{used}=0; |
84 | } |
85 | } |
86 | |
87 | # Multi-line HTML entries |
88 | |
89 | if(m%(\$\$[^\$]+\$\$)%) |
90 | { |
91 | my($code,$text)=($1,""); |
92 | |
93 | while(<FILE>) |
94 | { |
95 | last if(m%\$\$%); |
96 | |
97 | $text=$text.$_; |
98 | } |
99 | |
100 | $text =~ s%\r*\n$%%; |
101 | |
102 | if(defined $translations{$language}->{codes}->{$code}) |
103 | { |
104 | print STDERR "Language: $language DUPLICATED codeword '$code'\n"; |
105 | } |
106 | else |
107 | { |
108 | $translations{$language}->{html}++; |
109 | $translations{$language}->{codes}->{$code}={}; |
110 | $translations{$language}->{codes}->{$code}->{text}=$text; |
111 | $translations{$language}->{codes}->{$code}->{used}=0; |
112 | } |
113 | } |
114 | |
115 | # Single line XML entries |
116 | |
117 | if(m%\%\%%) |
118 | { |
119 | my($code,$text)=split("\t"); |
120 | |
121 | if(defined $translations{$language}->{codes}->{$code}) |
122 | { |
123 | print STDERR "Language: $language DUPLICATED codeword '$code'\n"; |
124 | } |
125 | else |
126 | { |
127 | $translations{$language}->{xml}++; |
128 | $translations{$language}->{codes}->{$code}={}; |
129 | $translations{$language}->{codes}->{$code}->{text}=$text; |
130 | $translations{$language}->{codes}->{$code}->{used}=0; |
131 | } |
132 | } |
133 | } |
134 | |
135 | close(FILE); |
136 | } |
137 | |
138 | |
139 | # Sort out the languages |
140 | |
141 | my @languages=(); |
142 | |
143 | push(@languages,"en"); |
144 | |
145 | foreach my $language (sort (keys %languages)) |
146 | { |
147 | push(@languages,$language) if($language ne "en"); |
148 | } |
149 | |
150 | |
151 | # Create the HTML files |
152 | |
153 | foreach my $html_template_file (@html_template_files) |
154 | { |
155 | foreach my $language (@languages) |
156 | { |
157 | next if(!$translations{$language}->{html}); |
158 | |
159 | print "Language: $language File: $html_template_file\n"; |
160 | |
161 | my $language_meta=0; |
162 | my $language_meta_string=""; |
163 | |
164 | open(HTML_IN ,"<$html_template_file"); |
165 | open(HTML_OUT,">$html_output_dir/$html_template_file.$language"); |
166 | |
167 | while(<HTML_IN>) |
168 | { |
169 | my $line=$_; |
170 | |
171 | # Language selection - special handling |
172 | |
173 | if($line =~ m%\*\*LANGUAGES-META\*\*%) |
174 | { |
175 | $language_meta=1-$language_meta; |
176 | |
177 | if($language_meta==0) |
178 | { |
179 | foreach my $language2 (@languages) |
180 | { |
181 | my $LANGUAGE2=$language2; |
182 | $LANGUAGE2 =~ tr%a-z%A-Z%; |
183 | |
184 | $line=$language_meta_string; |
185 | |
186 | if($language eq $language2) |
187 | { |
188 | $line =~ s%~~CHECKED~~%checked%g; |
189 | } |
190 | else |
191 | { |
192 | $line =~ s%~~CHECKED~~%%g; |
193 | } |
194 | |
195 | $line =~ s%~~lang~~%$language2%g; |
196 | $line =~ s%~~LANG~~%$LANGUAGE2%g; |
197 | |
198 | if(!$translations{$language2}->{html}) |
199 | { |
200 | $line =~ s%<a.+</a>%%; |
201 | } |
202 | |
203 | if(!$translations{$language2}->{xml}) |
204 | { |
205 | $line =~ s%<input .+>%%; |
206 | } |
207 | |
208 | foreach my $code (keys $translations{$language2}->{codes}) |
209 | { |
210 | if($line =~ s%$code%$translations{$language2}->{codes}->{$code}->{text}%g) |
211 | {$translations{$language2}->{codes}->{$code}->{used} = 1;} |
212 | } |
213 | |
214 | if($line =~ m%((\@\@|\$\$|\*\*|\~\~)[^\@\$*~]+(\@\@|\$\$|\*\*|\~\~))%) |
215 | { |
216 | print STDERR " Unmatched codeword '$1' in line: $line"; |
217 | } |
218 | |
219 | # Remove un-needed spaces |
220 | |
221 | $line =~ s%[\t ]+% %g; |
222 | $line =~ s%\n %\n%g; |
223 | $line =~ s%^ %%g; |
224 | |
225 | print HTML_OUT $line; |
226 | } |
227 | } |
228 | |
229 | next; |
230 | } |
231 | |
232 | if($language_meta) |
233 | { |
234 | $language_meta_string.=$line; |
235 | next; |
236 | } |
237 | |
238 | # Replace with translated phrases |
239 | |
240 | foreach my $code (keys $translations{$language}->{codes}) |
241 | { |
242 | if($line =~ s%\Q$code\E%$translations{$language}->{codes}->{$code}->{text}%g) |
243 | {$translations{$language}->{codes}->{$code}->{used} = 1;} |
244 | } |
245 | |
246 | # Replace what is left with English phrases |
247 | |
248 | foreach my $code (keys $translations{$languages[0]}->{codes}) |
249 | { |
250 | $line =~ s%\Q$code\E%$translations{$languages[0]}->{codes}->{$code}->{text}%g; |
251 | } |
252 | |
253 | if($line =~ m%((\@\@|\$\$|\*\*|\~\~)[^\@\$*~]+(\@\@|\$\$|\*\*|\~\~))%) |
254 | { |
255 | print STDERR " Unmatched codeword '$1' in line: $line"; |
256 | } |
257 | |
258 | # Remove un-needed spaces |
259 | |
260 | $line =~ s%[\t ]+% %g; |
261 | $line =~ s%\n %\n%g; |
262 | $line =~ s%^ %%g; |
263 | |
264 | print HTML_OUT $line; |
265 | } |
266 | |
267 | close(HTML_IN); |
268 | close(HTML_OUT); |
269 | } |
270 | } |
271 | |
272 | |
273 | # Create the XML file |
274 | |
275 | open(XML_OUT,">$xml_output_file"); |
276 | |
277 | open(XML_IN ,"<translations-head.xml"); |
278 | |
279 | while(<XML_IN>) |
280 | { |
281 | print XML_OUT; |
282 | } |
283 | |
284 | close(XML_IN); |
285 | |
286 | foreach my $language (@languages) |
287 | { |
288 | next if(!$translations{$language}->{xml}); |
289 | |
290 | print "Language: $language File: translations.xml\n"; |
291 | |
292 | open(XML_IN ,"<translations-body.xml"); |
293 | |
294 | while(<XML_IN>) |
295 | { |
296 | my $line=$_; |
297 | |
298 | $line =~ s%~~lang~~%$language%g; |
299 | |
300 | # Replace with translated phrases |
301 | |
302 | foreach my $code (keys $translations{$language}->{codes}) |
303 | { |
304 | if($line =~ s%$code%$translations{$language}->{codes}->{$code}->{text}%g) |
305 | {$translations{$language}->{codes}->{$code}->{used} = 1;} |
306 | } |
307 | |
308 | # Replace what is left with a note about missing translations |
309 | |
310 | if($line =~ m%\%\%%) |
311 | { |
312 | foreach my $code (keys $translations{$languages[0]}->{codes}) |
313 | { |
314 | $line =~ s%$code%$translations{$languages[0]}->{codes}->{$code}->{text}%g; |
315 | } |
316 | |
317 | $line =~ s%<%<!-- TRANSLATION REQUIRED: %; |
318 | $line =~ s%>% -->%; |
319 | |
320 | if($line =~ m%((\%\%|\~\~)[^\%~]+(\%\%|\~\~))%) |
321 | { |
322 | print STDERR " Unmatched codeword '$1' in line: $line"; |
323 | } |
324 | } |
325 | |
326 | print XML_OUT $line; |
327 | } |
328 | |
329 | close(XML_IN); |
330 | } |
331 | |
332 | open(XML_IN ,"<translations-tail.xml"); |
333 | |
334 | while(<XML_IN>) |
335 | { |
336 | print XML_OUT; |
337 | } |
338 | |
339 | close(XML_IN); |
340 | |
341 | close(XML_OUT); |
342 | |
343 | |
344 | # Check the languages |
345 | |
346 | foreach my $language (@languages) |
347 | { |
348 | foreach my $code (keys $translations{$language}->{codes}) |
349 | { |
350 | if(! $translations{$language}->{codes}->{$code}->{used}) |
351 | { |
352 | print STDERR "Language: $language UNUSED codeword: $code\n"; |
353 | } |
354 | } |
355 | } |
356 | |
357 | |
358 | # Print the translation coverage |
359 | |
360 | print "\n"; |
361 | |
362 | print "Translation Coverage\n"; |
363 | print "====================\n"; |
364 | |
365 | print "\n"; |
366 | print " Number Fraction\n"; |
367 | print "Language HTML XML HTML XML\n"; |
368 | print "-------- ---- --- ---- ---\n"; |
369 | |
370 | foreach my $language (@languages) |
371 | { |
372 | printf("%-6s %3d %3d %4.0f%% %4.0f%%\n",$language, |
373 | $translations{$language}->{html}, |
374 | $translations{$language}->{xml}, |
375 | 100.0*$translations{$language}->{html}/$translations{$languages[0]}->{html}, |
376 | 100.0*$translations{$language}->{xml} /$translations{$languages[0]}->{xml}); |
377 | } |
Properties
Name | Value |
---|---|
svn:executable | * |