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 %n = (); |
---|
37 | my $num_list = Locale::PO->load_file_asarray("$srcdir/survex.pot"); |
---|
38 | foreach my $po_entry (@{$num_list}) { |
---|
39 | my $ref = $po_entry->reference; |
---|
40 | (defined $ref && $ref =~ /^n:(\d+)$/m) or next; |
---|
41 | my $msgno = $1; |
---|
42 | my $key = $po_entry->msgid; |
---|
43 | my $msg = c_unescape($po_entry->dequote($key)); |
---|
44 | if (${$msgs{'en'}}[$msgno]) { |
---|
45 | print STDERR "Warning: already had message $msgno for language 'en'\n"; |
---|
46 | } |
---|
47 | ${$msgs{'en'}}[$msgno] = $msg; |
---|
48 | ++$n{$msgno}; |
---|
49 | } |
---|
50 | my $last = 0; |
---|
51 | for (sort { $a <=> $b } keys %n) { |
---|
52 | if ($_ > $last + 1) { |
---|
53 | print STDERR "Unused msg numbers: ", join(" ", $last + 1 .. $_ - 1), "\n"; |
---|
54 | } |
---|
55 | $last = $_; |
---|
56 | } |
---|
57 | print STDERR "Last used msg number: $last\n"; |
---|
58 | %n = (); |
---|
59 | |
---|
60 | for my $po_file (@ARGV) { |
---|
61 | my $language = $po_file; |
---|
62 | $language =~ s/\.po$//; |
---|
63 | |
---|
64 | my $po_hash = Locale::PO->load_file_ashash("$srcdir/$po_file"); |
---|
65 | |
---|
66 | foreach my $po_entry (@{$num_list}) { |
---|
67 | my $msgno = $po_entry->dequote($po_entry->msgstr); |
---|
68 | next if $msgno !~ /^\d+$/; |
---|
69 | my $key = $po_entry->msgid; |
---|
70 | my $ent = $$po_hash{$key}; |
---|
71 | if (defined $ent) { |
---|
72 | my $msg = c_unescape($po_entry->dequote($ent->msgstr)); |
---|
73 | next if $msg eq ''; |
---|
74 | if (${$msgs{$language}}[$msgno]) { |
---|
75 | print STDERR "Warning: already had message $msgno for language $language\n"; |
---|
76 | } |
---|
77 | ${$msgs{$language}}[$msgno] = $msg; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | # local $_; |
---|
82 | # open FINDUSES, "grep -no '*/$msgno\\>' \Q$srcdir\E/../src/*.cc \Q$srcdir\E../src/*.c \Q$srcdir\E/../src/*.h|" or die $!; |
---|
83 | # while (<FINDUSES>) { |
---|
84 | # push @{$uses{$msgno}}, $1 if /^([^:]*:\d+):/; |
---|
85 | # } |
---|
86 | # close FINDUSES; |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | my $lang; |
---|
91 | my @langs = sort grep ! /_\*$/, keys %msgs; |
---|
92 | |
---|
93 | my $num_msgs = -1; |
---|
94 | foreach $lang (@langs) { |
---|
95 | my $aref = $msgs{$lang}; |
---|
96 | $num_msgs = scalar @$aref if scalar @$aref > $num_msgs; |
---|
97 | } |
---|
98 | |
---|
99 | foreach $lang (@langs) { |
---|
100 | my $fnm = $lang; |
---|
101 | $fnm =~ s/(_.*)$/\U$1/; |
---|
102 | open OUT, ">$fnm.msg" or die $!; |
---|
103 | |
---|
104 | my $aref = $msgs{$lang}; |
---|
105 | |
---|
106 | my $parentaref; |
---|
107 | my $mainlang = $lang; |
---|
108 | $parentaref = $msgs{$mainlang} if $mainlang =~ s/_.*$//; |
---|
109 | |
---|
110 | print OUT $magic or die $!; |
---|
111 | print OUT pack("CCn", $major, $minor, $num_msgs) or die $!; |
---|
112 | |
---|
113 | my $buff = ''; |
---|
114 | my $missing = 0; |
---|
115 | my $retranslate = 0; |
---|
116 | |
---|
117 | for my $n (0 .. $num_msgs - 1) { |
---|
118 | my $warned = 0; |
---|
119 | my $msg = $$aref[$n]; |
---|
120 | if (!defined $msg) { |
---|
121 | $msg = $$parentaref[$n] if defined $parentaref; |
---|
122 | if (!defined $msg) { |
---|
123 | $msg = ${$msgs{'en'}}[$n]; |
---|
124 | # don't report if we have a parent (as the omission will be |
---|
125 | # reported there) |
---|
126 | if (defined $msg && $msg ne '' && !defined $parentaref) { |
---|
127 | ++$missing; |
---|
128 | $warned = 1; |
---|
129 | } else { |
---|
130 | $msg = '' if !defined $msg; |
---|
131 | } |
---|
132 | } |
---|
133 | } else { |
---|
134 | if ($lang ne 'en') { |
---|
135 | sanity_check("Message $n in language $lang", $msg, ${$msgs{'en'}}[$n]); |
---|
136 | } |
---|
137 | } |
---|
138 | $buff .= $msg . "\0"; |
---|
139 | } |
---|
140 | |
---|
141 | print OUT pack('N',length($buff)), $buff or die $!; |
---|
142 | close OUT or die $!; |
---|
143 | |
---|
144 | if ($missing || $retranslate) { |
---|
145 | print STDERR "Warning: "; |
---|
146 | if ($missing) { |
---|
147 | print STDERR "$lang: $missing missing message(s)"; |
---|
148 | if ($retranslate) { |
---|
149 | print STDERR " and $retranslate requiring retranslation"; |
---|
150 | } |
---|
151 | } else { |
---|
152 | print STDERR "$lang: $retranslate message(s) requiring retranslation"; |
---|
153 | } |
---|
154 | print STDERR "\n"; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | sub sanity_check { |
---|
159 | my ($where, $msg, $orig) = @_; |
---|
160 | # FIXME: Only do this if the message has the "c-format" flag. |
---|
161 | # check printf-like specifiers match |
---|
162 | # allow valid printf specifiers, or %<any letter> to support strftime |
---|
163 | # and other printf-like formats. |
---|
164 | my @pcent_m = grep /\%/, split /(%(?:[-#0 +'I]*(?:[0-9]*|\*|\*m\$)(?:\.[0-9]*)?(?:hh|ll|[hlLqjzt])?[diouxXeEfFgGaAcsCSpn]|[a-zA-Z]))/, $msg; |
---|
165 | my @pcent_o = grep /\%/, split /(%(?:[-#0 +'I]*(?:[0-9]*|\*|\*m\$)(?:\.[0-9]*)?(?:hh|ll|[hlLqjzt])?[diouxXeEfFgGaAcsCSpn]|[a-zA-Z]))/, $orig; |
---|
166 | while (scalar @pcent_m || scalar @pcent_o) { |
---|
167 | if (!scalar @pcent_m) { |
---|
168 | print STDERR "Warning: $where misses out \%spec $pcent_o[0]\n"; |
---|
169 | } elsif (!scalar @pcent_o) { |
---|
170 | print STDERR "Warning: $where has extra \%spec $pcent_m[0]\n"; |
---|
171 | } elsif ($pcent_m[0] ne $pcent_o[0]) { |
---|
172 | print STDERR "Warning: $where has \%spec $pcent_m[0] instead of $pcent_o[0]\n"; |
---|
173 | } |
---|
174 | pop @pcent_m; |
---|
175 | pop @pcent_o; |
---|
176 | } |
---|
177 | |
---|
178 | # Check for missing (or added) ellipses (...) |
---|
179 | if ($msg =~ /\.\.\./ && $orig !~ /\.\.\./) { |
---|
180 | print STDERR "Warning: $where has ellipses but original doesn't\n"; |
---|
181 | } elsif ($msg !~ /\.\.\./ && $orig =~ /\.\.\./) { |
---|
182 | print STDERR "Warning: $where is missing ellipses\n"; |
---|
183 | } |
---|
184 | |
---|
185 | # Check for missing (or added) menu shortcut (@) |
---|
186 | if ($msg =~ /\@[A-Za-z]/ && $orig !~ /\@[A-Za-z]/) { |
---|
187 | print STDERR "Warning: $where has menu shortcut but original doesn't\n"; |
---|
188 | } elsif ($msg !~ /\@[A-Za-z]/ && $orig =~ /\@[A-Za-z]/) { |
---|
189 | print STDERR "Warning: $where is missing menu shortcut\n"; |
---|
190 | } |
---|
191 | |
---|
192 | # Check for missing (or added) menu shortcut (&) |
---|
193 | if ($msg =~ /\&[A-Za-z]/ && $orig !~ /\&[A-Za-z]/) { |
---|
194 | print STDERR "Warning: $where has menu shortcut but original doesn't\n"; |
---|
195 | } elsif ($msg !~ /\&[A-Za-z]/ && $orig =~ /\&[A-Za-z]/) { |
---|
196 | print STDERR "Warning: $where is missing menu shortcut\n"; |
---|
197 | } |
---|
198 | |
---|
199 | # Check for missing (or added) double quotes (“ and ”) |
---|
200 | if (scalar($msg =~ s/“/$&/g) != scalar($orig =~ s/(?:“|»)/$&/g)) { |
---|
201 | print STDERR "Warning: $where has different numbers of “\n"; |
---|
202 | print STDERR "$orig\n$msg\n\n"; |
---|
203 | } |
---|
204 | if (scalar($msg =~ s/”/$&/g) != scalar($orig =~ s/(?:”|«)/$&/g)) { |
---|
205 | print STDERR "Warning: $where has different numbers of ”\n"; |
---|
206 | print STDERR "$orig\n$msg\n\n"; |
---|
207 | } |
---|
208 | |
---|
209 | # Check for missing (or added) menu accelerator "##" |
---|
210 | if ($msg =~ /\#\#/ && $orig !~ /\#\#/) { |
---|
211 | print STDERR "Warning: $where has menu accelerator but original doesn't\n"; |
---|
212 | } elsif ($msg !~ /\#\#/ && $orig =~ /\#\#/) { |
---|
213 | print STDERR "Warning: $where is missing menu accelerator\n"; |
---|
214 | } elsif ($orig =~ /\#\#(.*)/) { |
---|
215 | my $acc_o = $1; |
---|
216 | my ($acc_m) = $msg =~ /\#\#(.*)/; |
---|
217 | if ($acc_o ne $acc_m) { |
---|
218 | print STDERR "Warning: $where has menu accelerator $acc_m instead of $acc_o\n"; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | # Check for missing (or added) menu accelerator "\t" |
---|
223 | if ($msg =~ /\t/ && $orig !~ /\t/) { |
---|
224 | print STDERR "Warning: $where has menu accelerator but original doesn't\n"; |
---|
225 | } elsif ($msg !~ /\t/ && $orig =~ /\t/) { |
---|
226 | print STDERR "Warning: $where is missing menu accelerator\n"; |
---|
227 | } elsif ($orig =~ /\t(.*)/) { |
---|
228 | my $acc_o = $1; |
---|
229 | my ($acc_m) = $msg =~ /\t(.*)/; |
---|
230 | if ($acc_o ne $acc_m) { |
---|
231 | print STDERR "Warning: $where has menu accelerator $acc_m instead of $acc_o\n"; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | sub c_unescape { |
---|
237 | my $str = shift @_; |
---|
238 | $str =~ s/\\(x..|0|[0-7][0-7][0-7]|.)/&c_unescape_char($1)/ge; |
---|
239 | return $str; |
---|
240 | } |
---|
241 | |
---|
242 | sub c_unescape_char { |
---|
243 | my $ch = shift @_; |
---|
244 | if ($ch eq '0' || $ch eq 'x00' || $ch eq '000') { |
---|
245 | print STDERR "Nul byte in translation! (\\$ch)\n"; |
---|
246 | exit(1); |
---|
247 | } |
---|
248 | return $ch if $ch eq '"' || $ch eq '\\'; |
---|
249 | return "\n" if $ch eq "n"; |
---|
250 | return "\t" if $ch eq "t"; |
---|
251 | return "\r" if $ch eq "r"; |
---|
252 | return "\f" if $ch eq "f"; |
---|
253 | return chr(hex(substr($ch,1))) if $ch =~ /^x../; |
---|
254 | return chr(oct($ch)) if $ch =~ /^[0-7][0-7][0-7]/; |
---|
255 | print STDERR "Unknown C-escape in translation! (\\$ch)\n"; |
---|
256 | exit(1); |
---|
257 | } |
---|