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

line_contentsproj-api-updatestereo
Last change on this file since 08e858b was 45c9d00, checked in by Olly Betts <olly@…>, 9 years ago

lib/po-to-msg.pl: Fix menu shortcut check to allow for non-ASCII
shortcuts. Remove code to check for menu shortcuts marked with '@'
as we no longer do that.

  • Property mode set to 100755
File size: 7.4 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\xc2-\xf4]/ && $orig !~ /\&[A-Za-z]/) {
180       print STDERR "Warning: $where has menu shortcut but original doesn't\n";
181   } elsif ($msg !~ /\&[A-Za-z\xc2-\xf4]/ && $orig =~ /\&[A-Za-z]/) {
182       print STDERR "Warning: $where is missing menu shortcut\n";
183   }
184
185   # Check for missing (or added) double quotes (“ and ”)
186   if (scalar($msg =~ s/(?:“|»)/$&/g) != scalar($orig =~ s/“/$&/g)) {
187       print STDERR "Warning: $where has different numbers of “\n";
188       print STDERR "$orig\n$msg\n\n";
189   }
190   if (scalar($msg =~ s/(?:”|«)/$&/g) != scalar($orig =~ s/”/$&/g)) {
191       print STDERR "Warning: $where has different numbers of ”\n";
192       print STDERR "$orig\n$msg\n\n";
193   }
194
195   # Check for missing (or added) menu accelerator "##"
196   if ($msg =~ /\#\#/ && $orig !~ /\#\#/) {
197       print STDERR "Warning: $where has menu accelerator but original doesn't\n";
198   } elsif ($msg !~ /\#\#/ && $orig =~ /\#\#/) {
199       print STDERR "Warning: $where is missing menu accelerator\n";
200   } elsif ($orig =~ /\#\#(.*)/) {
201       my $acc_o = $1;
202       my ($acc_m) = $msg =~ /\#\#(.*)/;
203       if ($acc_o ne $acc_m) {
204           print STDERR "Warning: $where has menu accelerator $acc_m instead of $acc_o\n";
205       }
206   }
207
208   # Check for missing (or added) menu accelerator "\t"
209   if ($msg =~ /\t/ && $orig !~ /\t/) {
210       print STDERR "Warning: $where has menu accelerator but original doesn't\n";
211   } elsif ($msg !~ /\t/ && $orig =~ /\t/) {
212       print STDERR "Warning: $where is missing menu accelerator\n";
213   } elsif ($orig =~ /\t(.*)/) {
214       my $acc_o = $1;
215       my ($acc_m) = $msg =~ /\t(.*)/;
216       if ($acc_o ne $acc_m) {
217           print STDERR "Warning: $where has menu accelerator $acc_m instead of $acc_o\n";
218       }
219   }
220}
221
222sub c_unescape {
223   my $str = shift @_;
224   $str =~ s/\\(x..|0|[0-7][0-7][0-7]|.)/&c_unescape_char($1)/ge;
225   return $str;
226}
227
228sub c_unescape_char {
229    my $ch = shift @_;
230    if ($ch eq '0' || $ch eq 'x00' || $ch eq '000') {
231        print STDERR "Nul byte in translation! (\\$ch)\n";
232        exit(1);
233    }
234    return $ch if $ch eq '"' || $ch eq '\\';
235    return "\n" if $ch eq "n";
236    return "\t" if $ch eq "t";
237    return "\r" if $ch eq "r";
238    return "\f" if $ch eq "f";
239    return chr(hex(substr($ch,1))) if $ch =~ /^x../;
240    return chr(oct($ch)) if $ch =~ /^[0-7][0-7][0-7]/;
241    print STDERR "Unknown C-escape in translation! (\\$ch)\n";
242    exit(1);
243}
Note: See TracBrowser for help on using the repository browser.