[19319] | 1 | #! /usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | use utf8;
|
---|
| 4 | use encoding "utf8";
|
---|
| 5 | use Term::ReadKey;
|
---|
| 6 | use Encode;
|
---|
| 7 |
|
---|
| 8 | my $waswarn = 0;
|
---|
[19496] | 9 | my $maxcount = 0;
|
---|
[19319] | 10 |
|
---|
| 11 | main();
|
---|
| 12 |
|
---|
| 13 | sub getdate
|
---|
| 14 | {
|
---|
| 15 | my @t=gmtime();
|
---|
| 16 | return sprintf("%04d-%02d-%02d %02d:%02d+0000",
|
---|
| 17 | 1900+$t[5],$t[4]+1,$t[3],$t[2],$t[1]);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | sub loadfiles($@)
|
---|
| 21 | {
|
---|
| 22 | my $desc;
|
---|
| 23 | my $all;
|
---|
| 24 | my ($lang,@files) = @_;
|
---|
| 25 | foreach my $file (@files)
|
---|
| 26 | {
|
---|
| 27 | die "Could not open file $file." if(!open FILE,"<:utf8",$file);
|
---|
| 28 | my $linenum = 0;
|
---|
| 29 |
|
---|
[19496] | 30 | my $cnt = -1; # don't count translators info
|
---|
[19319] | 31 | if($file =~ /\/(.._..)\.po$/ || $file =~ /\/(..)\.po$/)
|
---|
| 32 | {
|
---|
| 33 | my $l = $1;
|
---|
| 34 | ++$lang->{$l};
|
---|
| 35 | my %postate = (last => "", type => "");
|
---|
| 36 | my $linenum = 0;
|
---|
| 37 | print "Reading file $file\n";
|
---|
| 38 | while(<FILE>)
|
---|
| 39 | {
|
---|
| 40 | ++$linenum;
|
---|
| 41 | my $fn = "$file:$linenum";
|
---|
| 42 | chomp;
|
---|
| 43 | if($_ =~ /^#/ || !$_)
|
---|
| 44 | {
|
---|
| 45 | checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
|
---|
| 46 | $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
|
---|
| 47 | }
|
---|
| 48 | elsif($_ =~ /^"(.*)"$/) {$postate{last} .= $1;}
|
---|
| 49 | elsif($_ =~ /^(msg.+) "(.*)"$/)
|
---|
| 50 | {
|
---|
| 51 | my ($n, $d) = ($1, $2);
|
---|
[19496] | 52 | ++$cnt if $n eq "msgid";
|
---|
[19334] | 53 | my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
|
---|
[19319] | 54 | checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, $new);
|
---|
| 55 | $postate{last} = $d;
|
---|
| 56 | $postate{type} = $n;
|
---|
| 57 | $postate{src} = $fn if $new;
|
---|
| 58 | }
|
---|
| 59 | else
|
---|
| 60 | {
|
---|
| 61 | die "Strange line $linenum in $file: $_.";
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
|
---|
| 65 | }
|
---|
| 66 | else
|
---|
| 67 | {
|
---|
| 68 | die "File format not supported for file $file.";
|
---|
| 69 | }
|
---|
[19496] | 70 | $maxcount = $cnt if $cnt > $maxcount;
|
---|
[19319] | 71 | close(FILE);
|
---|
| 72 | }
|
---|
| 73 | return %all;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | my $alwayspo = 0;
|
---|
| 77 | my $alwaysup = 0;
|
---|
| 78 | my $noask = 0;
|
---|
| 79 | my $conflicts;
|
---|
| 80 | sub copystring($$$$$$$)
|
---|
| 81 | {
|
---|
| 82 | my ($data, $en, $l, $str, $txt, $context, $ispo) = @_;
|
---|
| 83 |
|
---|
| 84 | $en = "___${context}___$en" if $context;
|
---|
| 85 |
|
---|
| 86 | if(exists($data->{$en}{$l}) && $data->{$en}{$l} ne $str)
|
---|
| 87 | {
|
---|
| 88 | return if !$str;
|
---|
| 89 | if($l =~ /^_/)
|
---|
| 90 | {
|
---|
| 91 | $data->{$en}{$l} .= ";$str" if !($data->{$en}{$l} =~ /$str/);
|
---|
| 92 | }
|
---|
| 93 | elsif(!$data->{$en}{$l})
|
---|
| 94 | {
|
---|
| 95 | $data->{$en}{$l} = $str;
|
---|
| 96 | }
|
---|
| 97 | else
|
---|
| 98 | {
|
---|
| 99 |
|
---|
| 100 | my $f = $data->{$en}{_file} || "";
|
---|
| 101 | $f = ($f ? "$f;".$data->{$en}{"_src.$l"} : $data->{$en}{"_src.$l"}) if $data->{$en}{"_src.$l"};
|
---|
| 102 | my $isotherpo = ($f =~ /\.po\:/);
|
---|
| 103 | my $pomode = ($ispo && !$isotherpo) || (!$ispo && $isotherpo);
|
---|
| 104 |
|
---|
| 105 | my $mis = "String mismatch for '$en' **$str** ($txt) != **$data->{$en}{$l}** ($f)\n";
|
---|
| 106 | my $replace = 0;
|
---|
| 107 |
|
---|
| 108 | if(($conflicts{$l}{$str} || "") eq $data->{$en}{$l}) {}
|
---|
| 109 | elsif($pomode && $alwaysup) { $replace=$isotherpo; }
|
---|
| 110 | elsif($pomode && $alwayspo) { $replace=$ispo; }
|
---|
| 111 | elsif($noask) { print $mis; ++$waswarn; }
|
---|
| 112 | else
|
---|
| 113 | {
|
---|
| 114 | ReadMode 4; # Turn off controls keys
|
---|
| 115 | my $arg = "(l)eft, (r)ight";
|
---|
| 116 | $arg .= ", (p)o, (u)pstream[ts/mat], all p(o), all up(s)tream" if $pomode;
|
---|
| 117 | $arg .= ", e(x)it: ";
|
---|
| 118 | print "$mis$arg";
|
---|
| 119 | while((my $c = getc()))
|
---|
| 120 | {
|
---|
| 121 | if($c eq "l") { $replace=1; }
|
---|
| 122 | elsif($c eq "r") {}
|
---|
| 123 | elsif($c eq "p" && $pomode) { $replace=$ispo; }
|
---|
| 124 | elsif($c eq "u" && $pomode) { $replace=$isotherpo; }
|
---|
| 125 | elsif($c eq "o" && $pomode) { $alwayspo = 1; $replace=$ispo; }
|
---|
| 126 | elsif($c eq "s" && $pomode) { $alwaysup = 1; $replace=$isotherpo; }
|
---|
| 127 | elsif($c eq "x") { $noask = 1; ++$waswarn; }
|
---|
| 128 | else { print "\n$arg"; next; }
|
---|
| 129 | last;
|
---|
| 130 | }
|
---|
| 131 | print("\n");
|
---|
| 132 | ReadMode 0; # Turn on controls keys
|
---|
| 133 | }
|
---|
| 134 | if(!$noask)
|
---|
| 135 | {
|
---|
| 136 | if($replace)
|
---|
| 137 | {
|
---|
| 138 | $data->{$en}{$l} = $str;
|
---|
| 139 | $conflicts{$l}{$data->{$en}{$l}} = $str;
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
| 143 | $conflicts{$l}{$str} = $data->{$en}{$l};
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | else
|
---|
| 149 | {
|
---|
| 150 | $data->{$en}{$l} = $str;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | sub checkpo($$$$$$)
|
---|
| 155 | {
|
---|
| 156 | my ($postate, $data, $l, $txt, $keys, $new) = @_;
|
---|
| 157 |
|
---|
| 158 | if($postate->{type} eq "msgid") {$postate->{msgid} = $postate->{last};}
|
---|
| 159 | elsif($postate->{type} eq "msgid_plural") {$postate->{msgid_1} = $postate->{last};}
|
---|
| 160 | elsif($postate->{type} =~ /^msgstr(\[0\])?$/) {$postate->{msgstr} = $postate->{last};}
|
---|
| 161 | elsif($postate->{type} =~ /^msgstr\[(.+)\]$/) {$postate->{"msgstr_$1"} = $postate->{last};}
|
---|
| 162 | elsif($postate->{type} eq "msgctxt") {$postate->{context} = $postate->{last};}
|
---|
| 163 | elsif($postate->{type}) { die "Strange type $postate->{type} found\n" }
|
---|
| 164 |
|
---|
| 165 | if($new)
|
---|
| 166 | {
|
---|
| 167 | if((!$postate->{fuzzy}) && $postate->{msgstr} && $postate->{msgid})
|
---|
| 168 | {
|
---|
| 169 | copystring($data, $postate->{msgid}, $l, $postate->{msgstr},$txt,$postate->{context}, 1);
|
---|
| 170 | for($i = 1; exists($postate->{"msgstr_$i"}); ++$i)
|
---|
| 171 | { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
|
---|
| 172 | if($postate->{msgid_1})
|
---|
| 173 | { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
|
---|
| 174 | copystring($data, $postate->{msgid}, "_src.$l", $postate->{src},$txt,$postate->{context}, 1);
|
---|
| 175 | }
|
---|
| 176 | elsif($postate->{msgstr} && !$postate->{msgid})
|
---|
| 177 | {
|
---|
| 178 | my %k = ($postate->{msgstr} =~ /(.+?): +(.+?)\\n/g);
|
---|
| 179 | # take the first one!
|
---|
| 180 | for $a (sort keys %k)
|
---|
| 181 | {
|
---|
| 182 | $keys->{$l}{$a} = $k{$a} if !$keys->{$l}{$a};
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | foreach my $k (keys %{$postate})
|
---|
| 186 | {
|
---|
| 187 | delete $postate->{$k};
|
---|
| 188 | }
|
---|
| 189 | $postate->{type} = $postate->{last} = "";
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[19356] | 193 | sub makestring($)
|
---|
| 194 | {
|
---|
| 195 | my ($str) = @_;
|
---|
| 196 | $str =~ s/\\"/"/g;
|
---|
[21523] | 197 | $str =~ s/\\\\/\\/g;
|
---|
[19356] | 198 | $str =~ s/\\n/\n/g;
|
---|
| 199 | $str = encode("utf8", $str);
|
---|
| 200 | return pack("n",length($str)).$str;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[19319] | 203 | sub createlang($@)
|
---|
| 204 | {
|
---|
| 205 | my ($data, @files) = @_;
|
---|
| 206 | foreach my $file (@files)
|
---|
| 207 | {
|
---|
| 208 | my $la;
|
---|
[19496] | 209 | my $cnt = 0;
|
---|
[19319] | 210 | if($file =~ /[-_](.._..)\.lang$/ || $file =~ /^(?:.*\/)?(.._..)\.lang$/ ||
|
---|
| 211 | $file =~ /[-_](...?)\.lang$/ || $file =~ /^(?:.*\/)?(..)\.lang$/)
|
---|
| 212 | {
|
---|
| 213 | $la = $1;
|
---|
| 214 | }
|
---|
| 215 | else
|
---|
| 216 | {
|
---|
| 217 | die "Language for file $file unknown.";
|
---|
| 218 | }
|
---|
| 219 | die "Could not open outfile $file\n" if !open FILE,">:raw",$file;
|
---|
| 220 |
|
---|
| 221 | foreach my $en (sort keys %{$data})
|
---|
| 222 | {
|
---|
| 223 | next if $data->{$en}{"en.1"};
|
---|
| 224 | my $val;
|
---|
| 225 | if($la eq "en")
|
---|
| 226 | {
|
---|
[19496] | 227 | ++$cnt;
|
---|
[19319] | 228 | $val = $en;
|
---|
| 229 | $val =~ s/^___(.*)___/_:$1\n/;
|
---|
| 230 | }
|
---|
| 231 | else
|
---|
| 232 | {
|
---|
| 233 | my $ennoctx = $en;
|
---|
| 234 | $ennoctx =~ s/^___(.*)___//;
|
---|
| 235 | $val = (exists($data->{$en}{$la})) ? $data->{$en}{$la} : "";
|
---|
[19496] | 236 | ++$cnt if $val;
|
---|
[19319] | 237 | $val = "" if($ennoctx eq $val);
|
---|
| 238 | }
|
---|
[19356] | 239 | print FILE makestring($val);
|
---|
[19319] | 240 | }
|
---|
| 241 | print FILE pack "n",0xFFFF;
|
---|
| 242 | foreach my $en (sort keys %{$data})
|
---|
| 243 | {
|
---|
| 244 | next if !$data->{$en}{"en.1"};
|
---|
| 245 | my $num;
|
---|
| 246 | for($num = 1; exists($data->{$en}{"$la.$num"}); ++$num)
|
---|
| 247 | { }
|
---|
| 248 | my $val;
|
---|
| 249 | if($la eq "en")
|
---|
| 250 | {
|
---|
[19496] | 251 | ++$cnt;
|
---|
[19319] | 252 | $val = $en;
|
---|
| 253 | $val =~ s/^___(.*)___/_:$1\n/;
|
---|
| 254 | }
|
---|
| 255 | else
|
---|
| 256 | {
|
---|
| 257 | $val = (exists($data->{$en}{$la})) ? $data->{$en}{$la} : "";
|
---|
| 258 | --$num if(!$val);
|
---|
[19496] | 259 | ++$cnt if $val;
|
---|
[19319] | 260 | if($num == 2)
|
---|
| 261 | {
|
---|
| 262 | my $ennoctx = $en;
|
---|
| 263 | $ennoctx =~ s/^___(.*)___//;
|
---|
| 264 | $num = 0 if $val eq $ennoctx && $data->{$en}{"$la.1"} eq $data->{$en}{"en.1"};
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | print FILE pack "C",$num;
|
---|
| 269 | if($num)
|
---|
| 270 | {
|
---|
[19356] | 271 | print FILE makestring($val);
|
---|
[19319] | 272 | for($num = 1; exists($data->{$en}{"$la.$num"}); ++$num)
|
---|
| 273 | {
|
---|
[19356] | 274 | print FILE makestring($data->{$en}{"$la.$num"});
|
---|
[19319] | 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | close FILE;
|
---|
[19496] | 279 | print "Created file $file: Added $cnt strings out of $maxcount.\n";
|
---|
[19319] | 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | sub main
|
---|
| 284 | {
|
---|
| 285 | my %lang;
|
---|
| 286 | my @po;
|
---|
| 287 | my $basename = shift @ARGV;
|
---|
| 288 | foreach my $arg (@ARGV)
|
---|
| 289 | {
|
---|
| 290 | foreach my $f (glob $arg)
|
---|
| 291 | {
|
---|
| 292 | if($f =~ /\*/) { printf "Skipping $f\n"; }
|
---|
| 293 | elsif($f =~ /\.po$/) { push(@po, $f); }
|
---|
| 294 | else { die "unknown file extension."; }
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | my %data = loadfiles(\%lang,@po);
|
---|
| 298 | my @clang;
|
---|
| 299 | foreach my $la (sort keys %lang)
|
---|
| 300 | {
|
---|
| 301 | push(@clang, "${basename}$la.lang");
|
---|
| 302 | }
|
---|
| 303 | push(@clang, "${basename}en.lang");
|
---|
| 304 | die "There have been warning. No output.\n" if $waswarn;
|
---|
| 305 |
|
---|
| 306 | createlang(\%data, @clang);
|
---|
| 307 | }
|
---|