Debian Codegraph @

translations/i18n.pl

raw
1 #!/usr/bin/env perl -w
2
3 use utf8;
4 use encoding "utf8";
5 use Term::ReadKey;
6
7 my $nocontext = 1;
8 my $waswarn = 0;
9 my $mail = "Merkaartor <merkaartor\@openstreetmap.org>";
10 my %pokeys = (
11 );
12
13 # don't copy these from files
14 my %defkeys = (
15 "X-Generator" => "Merkaartor translation convert",
16 "MIME-Version" => "1.0",
17 "Content-Type" => "text/plain; charset=UTF-8",
18 "Content-Transfer-Encoding" => "8bit",
19 "Project-Id-Version" => "merkaartor_templates 1.0",
20 "Report-Msgid-Bugs-To" => $mail,
21 "POT-Creation-Date" => getdate(),
22 "PO-Revision-Date" => getdate(),
23 # "Last-Translator" => $mail,
24 # "Language-Team" => $mail,
25 # "X-Launchpad-Export-Date" => getdate(),
26 );
27
28 main();
29
30 sub getdate
31 {
32 my @t=gmtime();
33 return sprintf("%04d-%02d-%02d %02d:%02d+0000",
34 1900+$t[5],$t[4]+1,$t[3],$t[2],$t[1]);
35 }
36
37 sub loadfiles($$@)
38 {
39 my $desc;
40 my $all;
41 my ($lang,$keys,@files) = @_;
42 foreach my $file (@files)
43 {
44 die "Could not open file $file." if(!open FILE,"<:utf8",$file);
45 my $linenum = 0;
46 if($file =~ /\.mat$/)
47 {
48 while(my $line = <FILE>)
49 {
50 ++$linenum;
51 chomp $line;
52 if($line =~ /^ *<description +locale="([A-Za-z_]+)" *>(.*?)<\/description> *$/)
53 {
54 my $val = maketxt($2);
55 my $l = $1;
56 $desc{$l} = $val;
57 $desc{_file} = "$file:$linenum" if($l eq "en");
58 }
59 elsif($line =~ /description/)
60 {
61 die "Can't handle line $linenum in $file: $line";
62 }
63 elsif(%desc)
64 {
65 my $en = $desc{"en"};
66 die "No english string found in previous block line $linenum in $file: $line" if(!$en);
67 delete $desc{"en"};
68 foreach my $l (reverse sort keys %desc)
69 {
70 copystring(\%all, $en, $l, $desc{$l}, "line $linenum in $file", undef, 0);
71 ++$lang->{$l} if !($l =~ /^_/);
72 }
73 %desc = ();
74 }
75 }
76 }
77 elsif($file =~ /[-_](.._..)\.po$/ || $file =~ /^(?:.*\/)?(.._..)\.po$/ ||
78 $file =~ /[-_](...?)\.po$/ || $file =~ /^(?:.*\/)?(..)\.po$/)
79 {
80 my $l = $1;
81 ++$lang->{$l};
82 my %postate = (last => "", type => "");
83 my $linenum = 0;
84 while(<FILE>)
85 {
86 ++$linenum;
87 my $fn = "$file:$linenum";
88 chomp;
89 if($_ =~ /^#/ || !$_)
90 {
91 checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
92 $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
93 }
94 elsif($_ =~ /^"(.*)"$/) {$postate{last} .= $1;}
95 elsif($_ =~ /^(msg.+) "(.*)"$/)
96 {
97 my ($n, $d) = ($1, $2);
98 my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
99 checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, $new);
100 $postate{last} = $d;
101 $postate{type} = $n;
102 $postate{src} = $fn if $new;
103 }
104 else
105 {
106 die "Strange line $linenum in $file: $_.";
107 }
108 }
109 checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
110 }
111 elsif($file =~ /\.ts$/)
112 {
113 my $linenum = 0;
114 my $l;
115 my $ctx;
116 my $loc;
117 my $issource;
118 my $istrans;
119 my $source;
120 my @trans;
121 my $fuzzy;
122 my $numerus;
123 while(<FILE>)
124 {
125 s/\r//g;
126 ++$linenum;
127 if(/<name>(.*)<\/name>/) { $ctx = $1; }
128 elsif(/<location filename="(.*?)" line="(.*?)"\/>/) { $loc = "$1:$2"; }
129 elsif(/context>/){$ctx = undef;}
130 elsif(/message( numerus="yes")?>/)
131 {
132 my $n = $1;
133 die "No language found in file $file." if !$l;
134 if($source)
135 {
136 $source = maketxt($source);
137 if(!$fuzzy)
138 {
139 my $txt = "line $linenum in $file";
140 $txt .= ", $loc" if($loc);
141 for($i = 0; $i <= $#trans; ++$i)
142 {
143 copystring(\%all, $source, $i ? "$l.$i" : $l, maketxt($trans[$i]), $txt, $ctx, 0);
144 }
145 if(defined($numerus))
146 {
147 copystring(\%all, $source, "en.1", $source, $txt, $ctx, 0);
148 }
149 }
150 copystring(\%all, $source, "_file", $loc, $txt, $ctx, 0) if $loc;
151 copystring(\%all, $source, "_src.$l", "$file:$linenum", $txt, $ctx, 0);
152 }
153 @trans = undef;
154 $loc = $issource = $istrans = $source = $numerus = $fuzzy = undef;
155 $numerus = 0 if $n;
156 }
157 elsif(/<TS .* language="(.*)">/) { $l = getlang($1); ++$lang->{$l}; }
158 elsif(/<TS version.*>/) {}
159 elsif(/<\?xml/ || /<!DOCTYPE/ || /<\/TS>/ || /<defaultcodec>/){} # ignore
160 elsif(/comment>(.*)<\/comment>/){} # ignore
161 # source
162 elsif(/<source>(.*)<\/source>/){$source = $1;}
163 elsif(/<source>(.*)/){$source = "$1\n"; $issource = 1;}
164 elsif($issource && /(.*)<\/source>/){$source .= $1; $issource = undef;}
165 elsif($issource){$source .= $_;}
166 # translation
167 elsif(defined($numerus) && /translation(?: type="(unfinished|obsolete)")?>/) {$fuzzy=$1;}
168 elsif(defined($numerus) && /<numerusform>(.*)<\/numerusform>/){$trans[$numerus++] = $1;}
169 elsif(defined($numerus) && /<numerusform>(.*)/){$trans[$numerus] = "$1\n"; $istrans = 1;}
170 elsif(defined($numerus) && $istrans && /(.*)<\/numerusform>/){$trans[$numerus++] .= $1; $istrans = undef;}
171 elsif(/<translation(?: type="(unfinished|obsolete)")?>(.*)<\/translation>/){$trans[0] = $2;$fuzzy=$1;}
172 elsif(/<translation(?: type="(unfinished|obsolete)")?>(.*)/){$trans[0] = "$2\n"; $istrans = 1;$fuzzy=$1;}
173 elsif($istrans && /(.*)<\/translation>/){$trans[0] .= $1; $istrans = undef;}
174 elsif($istrans){$trans[$numerus ? $numerus : 0] .= $_;}
175 else
176 {
177 die "Strange line $linenum in $file: $_.";
178 }
179
180 }
181 }
182 else
183 {
184 die "File format not supported for file $file.";
185 }
186 close(FILE);
187 }
188 return %all;
189 }
190
191 my $alwayspo = 0;
192 my $alwaysup = 0;
193 my $noask = 0;
194 my $conflicts;
195 sub copystring($$$$$$$)
196 {
197 my ($data, $en, $l, $str, $txt, $context, $ispo) = @_;
198
199 $en = "___${context}___$en" if $context && !$nocontext;
200
201 if(exists($data->{$en}{$l}) && $data->{$en}{$l} ne $str)
202 {
203 return if !$str;
204 if($l =~ /^_/)
205 {
206 $data->{$en}{$l} .= ";$str" if !($data->{$en}{$l} =~ /$str/);
207 }
208 elsif(!$data->{$en}{$l})
209 {
210 $data->{$en}{$l} = $str;
211 }
212 else
213 {
214
215 my $f = $data->{$en}{_file} || "";
216 $f = ($f ? "$f;".$data->{$en}{"_src.$l"} : $data->{$en}{"_src.$l"}) if $data->{$en}{"_src.$l"};
217 my $isotherpo = ($f =~ /\.po\:/);
218 my $pomode = ($ispo && !$isotherpo) || (!$ispo && $isotherpo);
219
220 my $mis = "String mismatch for '$en' **$str** ($txt) != **$data->{$en}{$l}** ($f)\n";
221 my $replace = 0;
222
223 if(($conflicts{$l}{$str} || "") eq $data->{$en}{$l}) {}
224 elsif($pomode && $alwaysup) { $replace=$isotherpo; }
225 elsif($pomode && $alwayspo) { $replace=$ispo; }
226 elsif($noask) { print $mis; ++$waswarn; }
227 else
228 {
229 ReadMode 4; # Turn off controls keys
230 my $arg = "(l)eft, (r)ight";
231 $arg .= ", (p)o, (u)pstream[ts/mat], all p(o), all up(s)tream" if $pomode;
232 $arg .= ", e(x)it: ";
233 print "$mis$arg";
234 while((my $c = getc()))
235 {
236 if($c eq "l") { $replace=1; }
237 elsif($c eq "r") {}
238 elsif($c eq "p" && $pomode) { $replace=$ispo; }
239 elsif($c eq "u" && $pomode) { $replace=$isotherpo; }
240 elsif($c eq "o" && $pomode) { $alwayspo = 1; $replace=$ispo; }
241 elsif($c eq "s" && $pomode) { $alwaysup = 1; $replace=$isotherpo; }
242 elsif($c eq "x") { $noask = 1; ++$waswarn; }
243 else { print "\n$arg"; next; }
244 last;
245 }
246 print("\n");
247 ReadMode 0; # Turn on controls keys
248 }
249 if(!$noask)
250 {
251 if($replace)
252 {
253 $data->{$en}{$l} = $str;
254 $conflicts{$l}{$data->{$en}{$l}} = $str;
255 }
256 else
257 {
258 $conflicts{$l}{$str} = $data->{$en}{$l};
259 }
260 }
261 }
262 }
263 else
264 {
265 $data->{$en}{$l} = $str;
266 }
267 }
268
269 sub checkpo($$$$$$)
270 {
271 my ($postate, $data, $l, $txt, $keys, $new) = @_;
272
273 if($postate->{type} eq "msgid") {$postate->{msgid} = $postate->{last};}
274 elsif($postate->{type} eq "msgid_plural") {$postate->{msgid_1} = $postate->{last};}
275 elsif($postate->{type} =~ /^msgstr(\[0\])?$/) {$postate->{msgstr} = $postate->{last};}
276 elsif($postate->{type} =~ /^msgstr\[(.+)\]$/) {$postate->{"msgstr_$1"} = $postate->{last};}
277 elsif($postate->{type} eq "msgctxt") {$postate->{context} = $postate->{last};}
278 elsif($postate->{type}) { die "Strange type $postate->{type} found\n" }
279
280 if($new)
281 {
282 if((!$postate->{fuzzy}) && $postate->{msgstr} && $postate->{msgid})
283 {
284 copystring($data, $postate->{msgid}, $l, $postate->{msgstr},$txt,$postate->{context}, 1);
285 for($i = 1; exists($postate->{"msgstr_$i"}); ++$i)
286 { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
287 if($postate->{msgid_1})
288 { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
289 copystring($data, $postate->{msgid}, "_src.$l", $postate->{src},$txt,$postate->{context}, 1);
290 }
291 elsif($postate->{msgstr} && !$postate->{msgid})
292 {
293 my %k = ($postate->{msgstr} =~ /(.+?): +(.+?)\\n/g);
294 # take the first one!
295 for $a (sort keys %k)
296 {
297 $keys->{$l}{$a} = $k{$a} if !$keys->{$l}{$a};
298 }
299 }
300 foreach my $k (keys %{$postate})
301 {
302 delete $postate->{$k};
303 }
304 $postate->{type} = $postate->{last} = "";
305 }
306 }
307
308 sub createpos($$@)
309 {
310 my ($data, $keys, @files) = @_;
311 foreach my $file (@files)
312 {
313 my $head;
314 my $la;
315 if($file =~ /[-_](.._..)\.po$/ || $file =~ /^(?:.*\/)?(.._..)\.po$/ ||
316 $file =~ /[-_](...?)\.po$/ || $file =~ /^(?:.*\/)?(..)\.po$/)
317 {
318 $la = $1;
319 $head = "# translation into language $la file $file\n";
320 }
321 elsif($file =~ /\.pot$/)
322 {
323 $la = "en";
324 $head = "# template file $file\n";
325 }
326 else
327 {
328 die "Language for file $file unknown.";
329 }
330 die "Could not open outfile $file\n" if !open FILE,">:utf8",$file;
331 print FILE "${head}msgid \"\"\nmsgstr \"\"\n";
332 my %k;
333 foreach my $k (keys %{$keys->{$la}}) { $k{$k} = $keys->{$la}{$k}; }
334 foreach my $k (keys %defkeys) { $k{$k} = $defkeys{$k}; }
335 foreach my $k (sort keys %k)
336 {
337 print FILE "\"$k: $k{$k}\\n\"\n";
338 }
339 print FILE "\n";
340
341 foreach my $en (sort keys %{$data})
342 {
343 my $ctx;
344 my $ennc = $en;
345 $ctx = $1 if $ennc =~ s/^___(.*)___//;
346 my $str = ($la ne "en" && exists($data->{$en}{$la})) ? $data->{$en}{$la} : "";
347 if($data->{$en}{_file})
348 {
349 foreach my $f (split ";",$data->{$en}{_file})
350 {
351 print FILE "#: $f\n"
352 }
353 }
354 else
355 {
356 next;
357 # print FILE "#: unknown:0\n"
358 }
359 if($ennc =~ /\%[0-9n]/)
360 { print FILE "#, c-format, qt-format\n"; }
361 elsif($ennc =~ /\%[ds]/)
362 { print FILE "#, c-format\n"; }
363 print FILE "msgctxt \"$ctx\"\n" if $ctx;
364 print FILE "msgid \"$ennc\"\n";
365 print FILE "msgid_plural \"$data->{$en}{\"en.1\"}\"\n" if $data->{$en}{"en.1"};
366 if($la ne "en" && (exists($data->{$en}{"$la.1"}) || $data->{$en}{"en.1"}))
367 {
368 print FILE "msgstr[0] \"$str\"\n";
369 for($i = 1; exists($data->{$en}{"$la.$i"}); ++$i)
370 { print FILE "msgstr[$i] \"$data->{$en}{\"$la.$i\"}\"\n"; }
371 }
372 else
373 {
374 print FILE "msgstr \"$str\"\n";
375 }
376 print FILE "\n";
377 }
378 close FILE;
379 }
380 }
381
382 sub maketxt($)
383 {
384 my ($str) = @_;
385 $str =~ s/&gt;/>/g;
386 $str =~ s/&lt;/</g;
387 $str =~ s/"/\\"/g;
388 $str =~ s/&quot;/\\"/g;
389 $str =~ s/&apos;/'/g;
390 $str =~ s/&amp;/&/g;
391 $str =~ s/\n/\\n/g;
392 return $str;
393 }
394
395 sub makexml($)
396 {
397 my ($str) = @_;
398 $str =~ s/&/&amp;/g;
399 $str =~ s/</&lt;/g;
400 $str =~ s/>/&gt;/g;
401 $str =~ s/\\"/&quot;/g;
402 $str =~ s/'/&apos;/g;
403 $str =~ s/\\n/\n/g;
404 return $str;
405 }
406
407 sub getlang($)
408 {
409 my ($l) = @_;
410 if($l eq "ru_RU") {$l = "ru";}
411 elsif($l eq "pl_PL") {$l = "pl";}
412 return $l;
413 }
414
415 sub replacemat($$$$)
416 {
417 my ($start,$en,$end,$data) = @_;
418 $en = maketxt($en);
419 my $repl = "$start<desCRIPtion locale=\"en\" >".makexml($en)."</desCRIPtion>$end";
420 foreach my $l (sort keys %{$data->{$en}})
421 {
422 next if $l =~ /[._]/;
423 $repl .= "$start<desCRIPtion locale=\"$l\" >".makexml($data->{$en}{$l})."</desCRIPtion>$end";
424 }
425 return $repl;
426 }
427
428 sub createmat($@)
429 {
430 my ($data, @files) = @_;
431
432 foreach my $file (@files)
433 {
434 my $x = $/;
435 undef $/;
436 die "Could not open $file\n" if !open FILE,"<:utf8",$file;
437 my $content = <FILE>;
438 close FILE;
439 foreach my $en (keys %{$data})
440 {
441 my $ostr = qr/( *)<description +locale="en" *>(.*)<\/description>([\r\n]+)(?: *<description .*\/description>[\r\n]+)*/;
442 $content =~ s/$ostr/replacemat($1,$2,$3,$data)/eg;
443 }
444 if($content =~ /(<description .*)/)
445 {
446 die "Could not handle string $1.";
447 }
448
449 $content =~ s/desCRIPtion/description/g;
450
451 die "Could not open output $file\n" if !open FILE,">:utf8",$file;
452 print FILE $content;
453 close FILE;
454 }
455 }
456
457 sub makenumerus($$$$)
458 {
459 my ($data, $first, $last,$l) = @_;
460 my $repl = $first.makexml($data->{$l}).$last;
461 for($i = 1; exists($data->{"$l.$i"}); ++$i)
462 {
463 $repl .= "\n".$first.makexml($data->{"$l.$i"}).$last;
464 }
465 return $repl;
466 }
467
468 sub convert_ts_message($$$$)
469 {
470 my ($content,$data,$l,$ctx) = @_;
471 $content =~ /<source>(.*)<\/source>/s;
472 my $source = ($ctx ? "___${ctx}___" : "") .maketxt($1);
473 if(exists($data->{$source}{$l}))
474 {
475 if($content =~ /numerus="yes"/)
476 {
477 if(!($content =~ s/( +<numerusform>).*(<\/numerusform>)/makenumerus($data->{$source},$1,$2,$l)/se))
478 {
479 die sprintf "Could not replace string '%.10s'",$source;
480 }
481 }
482 else
483 {
484 my $repl = makexml($data->{$source}{$l});
485 if(!($content =~ s/(<translation).*(<\/translation>)/$1>$repl$2/s))
486 {
487 die sprintf "Could not replace string '%.10s'",$source;
488 }
489 }
490 }
491 return $content;
492 }
493
494 sub convert_ts_context($$$)
495 {
496 my ($content,$data,$l) = @_;
497 my $ctx;
498 $ctx = $1 if(!$nocontext && $content =~ /<name>(.*)<\/name>/);
499 $content =~ s/(<message.*?>.*?<\/message>)/convert_ts_message($1,$data,$l,$ctx)/seg;
500 return $content;
501 }
502
503 sub createts($@)
504 {
505 my ($data, @files) = @_;
506
507 foreach my $file (@files)
508 {
509 my $x = $/;
510 undef $/;
511 die "Could not open $file\n" if !open FILE,"<:utf8",$file;
512 my $content = <FILE>;
513 close FILE;
514 if(!($content =~ /<TS .* language="(.*)">/))
515 {
516 die "Could not find language for $file.";
517 }
518 my $l = getlang($1);
519 $content =~ s/(<context>.*?<\/context>)/convert_ts_context($1,$data,$l)/seg;
520
521 die "Could not open output $file\n" if !open FILE,">:utf8",$file;
522 print FILE $content;
523 close FILE;
524 }
525 }
526
527 sub main
528 {
529 my %lang;
530 my @mat;
531 my @po;
532 my @ts;
533 my $basename = shift @ARGV;
534 foreach my $f (@ARGV)
535 {
536 if($f =~ /\*/) { printf "Skipping $f\n"; }
537 elsif($f =~ /\.mat$/) { push(@mat, $f); }
538 elsif($f =~ /\.po$/) { push(@po, $f); }
539 elsif($f =~ /\.ts$/) { push(@ts, $f); }
540 else { die "unknown file extension."; }
541 }
542 my %data = loadfiles(\%lang,\%pokeys, @mat,@ts,@po);
543 my @cpo;
544 foreach my $la (sort keys %lang)
545 {
546 push(@cpo, "${basename}_$la.po");
547 }
548 push(@cpo, "$basename.pot");
549 die "There have been warning. No output.\n" if $waswarn;
550 createpos(\%data, \%pokeys, @cpo);
551
552 createmat(\%data, @mat) if @mat;
553 createts(\%data, @ts) if @ts;
554 }