| 1 | #!/usr/bin/perl -w |
|---|
| 2 | require 5.008; |
|---|
| 3 | use bytes; |
|---|
| 4 | use strict; |
|---|
| 5 | use Locale::PO; |
|---|
| 6 | |
|---|
| 7 | use integer; |
|---|
| 8 | |
|---|
| 9 | # Magic identifier (12 bytes) |
|---|
| 10 | my $magic = "Svx\nMsg\r\n\xfe\xff\0"; |
|---|
| 11 | # Designed to be corrupted by ASCII ftp, top bit stripping (or |
|---|
| 12 | # being used for parity). Contains a zero byte so more likely |
|---|
| 13 | # to be flagged as data (e.g. by perl's "-B" test). |
|---|
| 14 | |
|---|
| 15 | my $srcdir = $0; |
|---|
| 16 | $srcdir =~ s!/[^/]+$!!; |
|---|
| 17 | |
|---|
| 18 | my $major = 0; |
|---|
| 19 | my $minor = 8; |
|---|
| 20 | |
|---|
| 21 | # File format (multi-byte numbers in network order (bigendian)): |
|---|
| 22 | # 12 bytes: Magic identifier |
|---|
| 23 | # 1 byte: File format major version number (0) |
|---|
| 24 | # 1 byte: File format minor version number (8) |
|---|
| 25 | # 2 bytes: Number of messages (N) |
|---|
| 26 | # 4 bytes: Offset from XXXX to end of file |
|---|
| 27 | # XXXX: |
|---|
| 28 | # N*: |
|---|
| 29 | # <message> NUL |
|---|
| 30 | |
|---|
| 31 | my %msgs = (); |
|---|
| 32 | ${$msgs{'en'}}[0] = '©'; |
|---|
| 33 | |
|---|
| 34 | # my %uses = (); |
|---|
| 35 | |
|---|
| 36 | my $file; |
|---|
| 37 | |
|---|
| 38 | my %n = (); |
|---|
| 39 | my %loc = (); |
|---|
| 40 | $file = "$srcdir/survex.pot"; |
|---|
| 41 | my $num_list = Locale::PO->load_file_asarray($file); |
|---|
| 42 | foreach my $po_entry (@{$num_list}) { |
|---|
| 43 | my $ref = $po_entry->reference; |
|---|
| 44 | (defined $ref && $ref =~ /^n:(\d+)$/m) or next; |
|---|
| 45 | my $msgno = $1; |
|---|
| 46 | my $key = $po_entry->msgid; |
|---|
| 47 | my $msg = c_unescape($po_entry->dequote($key)); |
|---|
| 48 | my $where = $file . ":" . $po_entry->loaded_line_number; |
|---|
| 49 | ${$loc{'en'}}[$msgno] = $where; |
|---|
| 50 | if (${$msgs{'en'}}[$msgno]) { |
|---|
| 51 | print STDERR "$where: warning: already had message $msgno for language 'en'\n"; |
|---|
| 52 | } |
|---|
| 53 | ${$msgs{'en'}}[$msgno] = $msg; |
|---|
| 54 | ++$n{$msgno}; |
|---|
| 55 | } |
|---|
| 56 | my $last = 0; |
|---|
| 57 | for (sort { $a <=> $b } keys %n) { |
|---|
| 58 | if ($_ > $last + 1) { |
|---|
| 59 | print STDERR "$file: Unused msg numbers: ", join(" ", $last + 1 .. $_ - 1), "\n"; |
|---|
| 60 | } |
|---|
| 61 | $last = $_; |
|---|
| 62 | } |
|---|
| 63 | print STDERR "$file: Last used msg number: $last\n"; |
|---|
| 64 | %n = (); |
|---|
| 65 | |
|---|
| 66 | my %fuzzy; |
|---|
| 67 | for my $po_file (@ARGV) { |
|---|
| 68 | my $language = $po_file; |
|---|
| 69 | $language =~ s/\.po$//; |
|---|
| 70 | |
|---|
| 71 | $file = "$srcdir/$po_file"; |
|---|
| 72 | my $po_hash = Locale::PO->load_file_ashash($file); |
|---|
| 73 | |
|---|
| 74 | if (exists $$po_hash{'""'}) { |
|---|
| 75 | if ($$po_hash{'""'}->msgstr =~ /^(?:.*\\n)?Language:\s*([^\s\\]+)/im) { |
|---|
| 76 | if ($language ne $1) { |
|---|
| 77 | my $line = 3 + scalar(@{[$& =~ /(\\n)/g]}); |
|---|
| 78 | print STDERR "$file:$line: Language code '$1' doesn't match '$language' from filename\n"; |
|---|
| 79 | } |
|---|
| 80 | } else { |
|---|
| 81 | my $line = 2 + scalar(@{[$$po_hash{'""'}->msgstr =~ /(\\n)/g]}); |
|---|
| 82 | print STDERR "$file:$line: No suitable 'Language:' field in header\n"; |
|---|
| 83 | } |
|---|
| 84 | } else { |
|---|
| 85 | print STDERR "$file:1: Expected 'msgid \"\"' with header\n"; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | my $fuzzy = 0; |
|---|
| 89 | foreach my $po_entry (@{$num_list}) { |
|---|
| 90 | my $ref = $po_entry->reference; |
|---|
| 91 | (defined $ref && $ref =~ /^n:(\d+)$/m) or next; |
|---|
| 92 | my $msgno = $1; |
|---|
| 93 | my $key = $po_entry->msgid; |
|---|
| 94 | my $ent = $$po_hash{$key}; |
|---|
| 95 | my $where = $file . ":" . $po_entry->loaded_line_number; |
|---|
| 96 | ${$loc{$language}}[$msgno] = $where; |
|---|
| 97 | if (defined $ent) { |
|---|
| 98 | my $msg = c_unescape($po_entry->dequote($ent->msgstr)); |
|---|
| 99 | next if $msg eq ''; |
|---|
| 100 | if (${$msgs{$language}}[$msgno]) { |
|---|
| 101 | print STDERR "$where: warning: already had message $msgno for language $language\n"; |
|---|
| 102 | } |
|---|
| 103 | ${$msgs{$language}}[$msgno] = $msg; |
|---|
| 104 | $ent->fuzzy() and ++$fuzzy; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | $fuzzy{$language} = $fuzzy; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | my $lang; |
|---|
| 111 | my @langs = sort grep ! /_\*$/, keys %msgs; |
|---|
| 112 | |
|---|
| 113 | my $num_msgs = -1; |
|---|
| 114 | foreach $lang (@langs) { |
|---|
| 115 | my $aref = $msgs{$lang}; |
|---|
| 116 | $num_msgs = scalar @$aref if scalar @$aref > $num_msgs; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | foreach $lang (@langs) { |
|---|
| 120 | my $fnm = $lang; |
|---|
| 121 | $file = "$srcdir/$lang.po"; |
|---|
| 122 | $fnm =~ s/(_.*)$/\U$1/; |
|---|
| 123 | open OUT, ">$fnm.msg" or die $!; |
|---|
| 124 | |
|---|
| 125 | my $aref = $msgs{$lang}; |
|---|
| 126 | |
|---|
| 127 | my $parentaref; |
|---|
| 128 | my $mainlang = $lang; |
|---|
| 129 | $parentaref = $msgs{$mainlang} if $mainlang =~ s/_.*$//; |
|---|
| 130 | |
|---|
| 131 | print OUT $magic or die $!; |
|---|
| 132 | print OUT pack("CCn", $major, $minor, $num_msgs) or die $!; |
|---|
| 133 | |
|---|
| 134 | my $buff = ''; |
|---|
| 135 | my $missing = 0; |
|---|
| 136 | |
|---|
| 137 | for my $n (0 .. $num_msgs - 1) { |
|---|
| 138 | my $warned = 0; |
|---|
| 139 | my $msg = $$aref[$n]; |
|---|
| 140 | if (!defined $msg) { |
|---|
| 141 | $msg = $$parentaref[$n] if defined $parentaref; |
|---|
| 142 | if (!defined $msg) { |
|---|
| 143 | $msg = ${$msgs{'en'}}[$n]; |
|---|
| 144 | # don't report if we have a parent (as the omission will be |
|---|
| 145 | # reported there) |
|---|
| 146 | if (defined $msg && $msg ne '' && !defined $parentaref) { |
|---|
| 147 | ++$missing; |
|---|
| 148 | $warned = 1; |
|---|
| 149 | } else { |
|---|
| 150 | $msg = '' if !defined $msg; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } else { |
|---|
| 154 | if ($lang ne 'en') { |
|---|
| 155 | sanity_check("Message $n in language $lang", $msg, ${$msgs{'en'}}[$n], ${$loc{$lang}}[$n]); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | $buff .= $msg . "\0"; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | print OUT pack('N',length($buff)), $buff or die $!; |
|---|
| 162 | close OUT or die $!; |
|---|
| 163 | |
|---|
| 164 | my $fuzzy = $fuzzy{$lang}; |
|---|
| 165 | if ($missing || $fuzzy) { |
|---|
| 166 | print STDERR "Warning: $file: "; |
|---|
| 167 | if ($missing) { |
|---|
| 168 | print STDERR "$missing missing message(s)"; |
|---|
| 169 | if ($fuzzy) { |
|---|
| 170 | print STDERR " and $fuzzy fuzzy message(s)"; |
|---|
| 171 | } |
|---|
| 172 | } else { |
|---|
| 173 | print STDERR "$fuzzy fuzzy message(s)"; |
|---|
| 174 | } |
|---|
| 175 | print STDERR " for $lang\n"; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | sub sanity_check { |
|---|
| 180 | my ($what, $msg, $orig, $where) = @_; |
|---|
| 181 | # FIXME: Only do this if the message has the "c-format" flag. |
|---|
| 182 | # check printf-like specifiers match |
|---|
| 183 | # allow valid printf specifiers, or %<any letter> to support strftime |
|---|
| 184 | # and other printf-like formats. |
|---|
| 185 | my @pcent_m = grep /\%/, split /(%(?:[-#0 +'I]*(?:[0-9]*|\*|\*m\$)(?:\.[0-9]*)?(?:hh|ll|[hlLqjzt])?[diouxXeEfFgGaAcsCSpn]|[a-zA-Z]))/, $msg; |
|---|
| 186 | my @pcent_o = grep /\%/, split /(%(?:[-#0 +'I]*(?:[0-9]*|\*|\*m\$)(?:\.[0-9]*)?(?:hh|ll|[hlLqjzt])?[diouxXeEfFgGaAcsCSpn]|[a-zA-Z]))/, $orig; |
|---|
| 187 | while (scalar @pcent_m || scalar @pcent_o) { |
|---|
| 188 | if (!scalar @pcent_m) { |
|---|
| 189 | print STDERR "$where: warning: $what misses out \%spec $pcent_o[0]\n"; |
|---|
| 190 | } elsif (!scalar @pcent_o) { |
|---|
| 191 | print STDERR "$where: warning: $what has extra \%spec $pcent_m[0]\n"; |
|---|
| 192 | } elsif ($pcent_m[0] ne $pcent_o[0]) { |
|---|
| 193 | print STDERR "$where: warning: $what has \%spec $pcent_m[0] instead of $pcent_o[0]\n"; |
|---|
| 194 | } |
|---|
| 195 | pop @pcent_m; |
|---|
| 196 | pop @pcent_o; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | # Check for missing (or added) ellipses (...) |
|---|
| 200 | if ($msg =~ /\.\.\./ && $orig !~ /\.\.\./) { |
|---|
| 201 | print STDERR "$where: warning: $what has ellipses but original doesn't\n"; |
|---|
| 202 | } elsif ($msg !~ /\.\.\./ && $orig =~ /\.\.\./) { |
|---|
| 203 | print STDERR "$where: warning: $what is missing ellipses\n"; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | # Check for missing (or added) menu shortcut (&) |
|---|
| 207 | if ($msg =~ /\&[A-Za-z\xc2-\xf4]/ && $orig !~ /\&[A-Za-z]/) { |
|---|
| 208 | print STDERR "$where: warning: $what has menu shortcut but original doesn't\n"; |
|---|
| 209 | } elsif ($msg !~ /\&[A-Za-z\xc2-\xf4]/ && $orig =~ /\&[A-Za-z]/) { |
|---|
| 210 | print STDERR "$where: warning: $what is missing menu shortcut\n"; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | # Check for missing (or added) double quotes (“ and ”) |
|---|
| 214 | if (scalar($msg =~ s/(?:“|»)/$&/g) != scalar($orig =~ s/“/$&/g)) { |
|---|
| 215 | print STDERR "$where: warning: $what has different numbers of “\n"; |
|---|
| 216 | print STDERR "$orig\n$msg\n\n"; |
|---|
| 217 | } |
|---|
| 218 | if (scalar($msg =~ s/(?:”|«)/$&/g) != scalar($orig =~ s/”/$&/g)) { |
|---|
| 219 | print STDERR "$where: warning: $what has different numbers of ”\n"; |
|---|
| 220 | print STDERR "$orig\n$msg\n\n"; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | # Check for missing (or added) menu accelerator "##" |
|---|
| 224 | if ($msg =~ /\#\#/ && $orig !~ /\#\#/) { |
|---|
| 225 | print STDERR "$where: warning: $what has menu accelerator but original doesn't\n"; |
|---|
| 226 | } elsif ($msg !~ /\#\#/ && $orig =~ /\#\#/) { |
|---|
| 227 | print STDERR "$where: warning: $what is missing menu accelerator\n"; |
|---|
| 228 | } elsif ($orig =~ /\#\#(.*)/) { |
|---|
| 229 | my $acc_o = $1; |
|---|
| 230 | my ($acc_m) = $msg =~ /\#\#(.*)/; |
|---|
| 231 | if ($acc_o ne $acc_m) { |
|---|
| 232 | print STDERR "$where: warning: $what has menu accelerator $acc_m instead of $acc_o\n"; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | # Check for missing (or added) menu accelerator "\t" |
|---|
| 237 | if ($msg =~ /\t/ && $orig !~ /\t/) { |
|---|
| 238 | print STDERR "$where: warning: $what has menu accelerator but original doesn't\n"; |
|---|
| 239 | } elsif ($msg !~ /\t/ && $orig =~ /\t/) { |
|---|
| 240 | print STDERR "$where: warning: $what is missing menu accelerator\n"; |
|---|
| 241 | } elsif ($orig =~ /\t(.*)/) { |
|---|
| 242 | my $acc_o = $1; |
|---|
| 243 | my ($acc_m) = $msg =~ /\t(.*)/; |
|---|
| 244 | if ($acc_o ne $acc_m) { |
|---|
| 245 | print STDERR "$where: warning: $what has menu accelerator $acc_m instead of $acc_o\n"; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | sub c_unescape { |
|---|
| 251 | my $str = shift @_; |
|---|
| 252 | $str =~ s/\\(x..|0|[0-7][0-7][0-7]|.)/&c_unescape_char($1)/ge; |
|---|
| 253 | return $str; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | sub c_unescape_char { |
|---|
| 257 | my $ch = shift @_; |
|---|
| 258 | if ($ch eq '0' || $ch eq 'x00' || $ch eq '000') { |
|---|
| 259 | print STDERR "Nul byte in translation! (\\$ch)\n"; |
|---|
| 260 | exit(1); |
|---|
| 261 | } |
|---|
| 262 | return $ch if $ch eq '"' || $ch eq '\\'; |
|---|
| 263 | return "\n" if $ch eq "n"; |
|---|
| 264 | return "\t" if $ch eq "t"; |
|---|
| 265 | return "\r" if $ch eq "r"; |
|---|
| 266 | return "\f" if $ch eq "f"; |
|---|
| 267 | return chr(hex(substr($ch,1))) if $ch =~ /^x../; |
|---|
| 268 | return chr(oct($ch)) if $ch =~ /^[0-7][0-7][0-7]/; |
|---|
| 269 | print STDERR "Unknown C-escape in translation! (\\$ch)\n"; |
|---|
| 270 | exit(1); |
|---|
| 271 | } |
|---|