1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#! /usr/bin/perl -w
use utf8;
use encoding "utf8";
binmode STDERR, ":encoding(utf8)";
use Term::ReadKey;
use Encode;
my $waswarn = 0;
my $maxcount = 0;
main();
sub getdate
{
my @t=gmtime();
return sprintf("%04d-%02d-%02d %02d:%02d+0000",
1900+$t[5],$t[4]+1,$t[3],$t[2],$t[1]);
}
sub loadfiles($@)
{
my $desc;
my $all;
my ($lang,@files) = @_;
foreach my $file (@files)
{
die "Could not open file $file." if(!open FILE,"<:utf8",$file);
my $linenum = 0;
my $cnt = -1; # don't count translators info
if($file =~ /\/(.._..)\.po$/ || $file =~ /\/(...?)\.po$/)
{
my $l = $1;
++$lang->{$l};
my %postate = (last => "", type => "");
my $linenum = 0;
print "Reading file $file\n";
while(<FILE>)
{
++$linenum;
my $fn = "$file:$linenum";
chomp;
if($_ =~ /^#/ || !$_)
{
checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
$postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
}
elsif($_ =~ /^"(.*)"$/) {$postate{last} .= $1;}
elsif($_ =~ /^(msg.+) "(.*)"$/)
{
my ($n, $d) = ($1, $2);
++$cnt if $n eq "msgid";
my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, $new);
$postate{last} = $d;
$postate{type} = $n;
$postate{src} = $fn if $new;
}
else
{
die "Strange line $linenum in $file: $_.";
}
}
checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
}
else
{
die "File format not supported for file $file.";
}
$maxcount = $cnt if $cnt > $maxcount;
close(FILE);
}
return %all;
}
my $alwayspo = 0;
my $alwaysup = 0;
my $noask = 0;
my $conflicts;
sub copystring($$$$$$$)
{
my ($data, $en, $l, $str, $txt, $context, $ispo) = @_;
$en = "___${context}___$en" if $context;
if(exists($data->{$en}{$l}) && $data->{$en}{$l} ne $str)
{
return if !$str;
if($l =~ /^_/)
{
$data->{$en}{$l} .= ";$str" if !($data->{$en}{$l} =~ /$str/);
}
elsif(!$data->{$en}{$l})
{
$data->{$en}{$l} = $str;
}
else
{
my $f = $data->{$en}{_file} || "";
$f = ($f ? "$f;".$data->{$en}{"_src.$l"} : $data->{$en}{"_src.$l"}) if $data->{$en}{"_src.$l"};
my $isotherpo = ($f =~ /\.po\:/);
my $pomode = ($ispo && !$isotherpo) || (!$ispo && $isotherpo);
my $mis = "String mismatch for '$en' **$str** ($txt) != **$data->{$en}{$l}** ($f)\n";
my $replace = 0;
if(($conflicts{$l}{$str} || "") eq $data->{$en}{$l}) {}
elsif($pomode && $alwaysup) { $replace=$isotherpo; }
elsif($pomode && $alwayspo) { $replace=$ispo; }
elsif($noask) { print $mis; ++$waswarn; }
else
{
ReadMode 4; # Turn off controls keys
my $arg = "(l)eft, (r)ight";
$arg .= ", (p)o, (u)pstream[ts/mat], all p(o), all up(s)tream" if $pomode;
$arg .= ", e(x)it: ";
print "$mis$arg";
while((my $c = getc()))
{
if($c eq "l") { $replace=1; }
elsif($c eq "r") {}
elsif($c eq "p" && $pomode) { $replace=$ispo; }
elsif($c eq "u" && $pomode) { $replace=$isotherpo; }
elsif($c eq "o" && $pomode) { $alwayspo = 1; $replace=$ispo; }
elsif($c eq "s" && $pomode) { $alwaysup = 1; $replace=$isotherpo; }
elsif($c eq "x") { $noask = 1; ++$waswarn; }
else { print "\n$arg"; next; }
last;
}
print("\n");
ReadMode 0; # Turn on controls keys
}
if(!$noask)
{
if($replace)
{
$data->{$en}{$l} = $str;
$conflicts{$l}{$data->{$en}{$l}} = $str;
}
else
{
$conflicts{$l}{$str} = $data->{$en}{$l};
}
}
}
}
else
{
$data->{$en}{$l} = $str;
}
}
sub checkpo($$$$$$)
{
my ($postate, $data, $l, $txt, $keys, $new) = @_;
if($postate->{type} eq "msgid") {$postate->{msgid} = $postate->{last};}
elsif($postate->{type} eq "msgid_plural") {$postate->{msgid_1} = $postate->{last};}
elsif($postate->{type} =~ /^msgstr(\[0\])?$/) {$postate->{msgstr} = $postate->{last};}
elsif($postate->{type} =~ /^msgstr\[(.+)\]$/) {$postate->{"msgstr_$1"} = $postate->{last};}
elsif($postate->{type} eq "msgctxt") {$postate->{context} = $postate->{last};}
elsif($postate->{type}) { die "Strange type $postate->{type} found\n" }
if($new)
{
if((!$postate->{fuzzy}) && $postate->{msgstr} && $postate->{msgid})
{
copystring($data, $postate->{msgid}, $l, $postate->{msgstr},$txt,$postate->{context}, 1);
for($i = 1; exists($postate->{"msgstr_$i"}); ++$i)
{ copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
if($postate->{msgid_1})
{ copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
copystring($data, $postate->{msgid}, "_src.$l", $postate->{src},$txt,$postate->{context}, 1);
}
elsif($postate->{msgstr} && !$postate->{msgid})
{
my %k = ($postate->{msgstr} =~ /(.+?): +(.+?)\\n/g);
# take the first one!
for $a (sort keys %k)
{
$keys->{$l}{$a} = $k{$a} if !$keys->{$l}{$a};
}
}
foreach my $k (keys %{$postate})
{
delete $postate->{$k};
}
$postate->{type} = $postate->{last} = "";
}
}
sub makestring($)
{
my ($str) = @_;
$str =~ s/\\"/"/g;
$str =~ s/\\\\/\\/g;
$str =~ s/\\n/\n/g;
$str = encode("utf8", $str);
return $str;
}
sub checkstring
{
my ($la, $tr, $en, $cnt, $en1, $eq) = @_;
$tr = makestring($tr);
$en = makestring($en);
$cnt = $cnt || 0;
$en1 = makestring($en1) if defined($en1);
my $error = 0;
# Test one - are there single quotes which don't occur twice
my $v = $tr;
$v =~ s/''//g; # replace all twice occuring single quotes
$v =~ s/'[{}]'//g; # replace all bracketquoting single quotes
if($v =~ /'/)#&& $la ne "en")
{
warn "JAVA translation issue for language $la: Mismatching single quotes:\nTranslated text: $tr\nOriginal text: $en\n";
$error = 1;
}
# Test two - check if there are {..} which should not be
my @fmt = ();
my $fmt;
my $fmte;
my $fmte1 = "";
my $trt = $tr; $trt =~ s/'[{}]'//g;
while($trt =~ /\{(.*?)\}/g) {push @fmt,$1}; $fmt = join("_", sort @fmt); @fmt = ();
my $ent = $en; $ent =~ s/'[{}]'//g;
while($ent =~ /\{(.*?)\}/g) {push @fmt,$1}; $fmte = join("_", sort @fmt); @fmt = ();
if($en1)
{
my $en1t = $en1; $en1t =~ s/'[{}]'//g;
while($en1t =~ /\{(.*?)\}/g) {push @fmt,$1}; $fmte1 = join("_", sort @fmt);
}
if($fmt ne $fmte && $fmt ne $fmte1)
{
if(!($fmte eq '0' && $fmt eq "" && $cnt == 1)) # Don't warn when a single value is left for first multi-translation
{
warn "JAVA translation issue for language $la ($cnt): Mismatching format entries:\nTranslated text: $tr\nOriginal text: $en\n";
$error = 1;
}
}
#$tr = "" if($error && $la ne "en");
return pack("n",65534) if $eq;
return pack("n",length($tr)).$tr;
}
sub createlang($@)
{
my ($data, @files) = @_;
my $maxlen = 0;
foreach my $file (@files)
{
my $len = length($file);
$maxlen = $len if $len > $maxlen;
}
foreach my $file (@files)
{
my $la;
my $cnt = 0;
if($file =~ /[-_](.._..)\.lang$/ || $file =~ /^(?:.*\/)?(.._..)\.lang$/ ||
$file =~ /[-_](...?)\.lang$/ || $file =~ /^(?:.*\/)?(...?)\.lang$/)
{
$la = $1;
}
else
{
die "Language for file $file unknown.";
}
die "Could not open outfile $file\n" if !open FILE,">:raw",$file;
foreach my $en (sort keys %{$data})
{
next if $data->{$en}{"en.1"};
my $val;
my $eq;
if($la eq "en")
{
++$cnt;
$val = $en;
$val =~ s/^___(.*)___/_:$1\n/;
}
else
{
my $ennoctx = $en;
$ennoctx =~ s/^___(.*)___//;
$val = (exists($data->{$en}{$la})) ? $data->{$en}{$la} : "";
++$cnt if $val;
if($ennoctx eq $val)
{
$val = ""; $eq = 1;
}
}
print FILE checkstring($la, $val, $en, undef, undef, $eq);
}
print FILE pack "n",0xFFFF;
foreach my $en (sort keys %{$data})
{
next if !$data->{$en}{"en.1"};
my $num;
for($num = 1; exists($data->{$en}{"$la.$num"}); ++$num)
{ }
my $val;
$eq = 0;
if($la eq "en")
{
++$cnt;
$val = $en;
$val =~ s/^___(.*)___/_:$1\n/;
}
else
{
$val = (exists($data->{$en}{$la})) ? $data->{$en}{$la} : "";
--$num if(!$val);
++$cnt if $val;
if($num == 2)
{
my $ennoctx = $en;
$ennoctx =~ s/^___(.*)___//;
if($val eq $ennoctx && $data->{$en}{"$la.1"} eq $data->{$en}{"en.1"})
{
$num = 0;
$eq = 1;
}
}
}
print FILE pack "C",$eq ? 0xFE : $num;
if($num)
{
print FILE checkstring($la, $val, $en, 1, $data->{$en}{"en.1"});
for($num = 1; exists($data->{$en}{"$la.$num"}); ++$num)
{
print FILE checkstring($la, $data->{$en}{"$la.$num"}, $en, $num+1, $data->{$en}{"en.1"});
}
}
}
close FILE;
if(!$cnt)
{
unlink $file;
printf "Skipped file %-${maxlen}s: Contained 0 strings out of %5d.\n",$file,$maxcount;
}
else
{
printf "Created file %-${maxlen}s: Added %5d strings out of %5d (%5.1f%%).\n",$file,$cnt,$maxcount,,$cnt*100.0/$maxcount;
}
}
}
sub main
{
my %lang;
my @po;
my $basename = shift @ARGV;
$basename .= "/" if !($basename =~ /[\/\\:]$/);
foreach my $arg (@ARGV)
{
foreach my $f (glob $arg)
{
if($f =~ /\*/) { printf "Skipping $f\n"; }
elsif($f =~ /\.po$/) { push(@po, $f); }
else { die "unknown file extension."; }
}
}
my %data = loadfiles(\%lang,@po);
my @clang;
foreach my $la (sort keys %lang)
{
push(@clang, "${basename}$la.lang");
}
push(@clang, "${basename}en.lang");
die "There have been warning. No output.\n" if $waswarn;
createlang(\%data, @clang);
}
|