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

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereowalls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since a570c81 was a570c81, checked in by Olly Betts <olly@…>, 12 years ago

lib/po-to-msg.pl: Update to read message numbers from references.

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