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

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

lib/po-to-msg.pl: Fix matching quote check to really allow
for using « and » in a translation.

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