source: git/lib/po-to-msg.pl @ b772727

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since b772727 was b772727, checked in by Olly Betts <olly@…>, 11 years ago

lib/po-to-msg.pl: Fix quote check to handle “” vs »«.

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