5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # author: Michael Gerz, michael.gerz@teststep.org
16 pocheck.pl [-acmpqst] po_file [po_file] ...
18 This script performs some consistency checks on po files.
20 We check for everything listed here, unless one or more of these
21 options is given, in which case we checks only for those requested.
22 -a: Check arguments, like %1\$s
23 -c: Check for colons at end
24 -m: Check for menu shortcuts
25 -p: Check for period at end
26 -q: Check Qt shortcuts
27 -s: Check for space at end
28 -t: Check for uniform translation
29 These options can be given with or without other options.
30 -f: Ignore fuzzy translations
31 -w: Only report summary total of errors
32 -i: Silent mode, report only errors
36 getopts(":hacfmpqstwi", \%options);
38 if (defined($options{h})) {
43 my $only_total = defined($options{w});
44 delete $options{w} if $only_total;
45 my $ignore_fuzzy = defined($options{f});
46 delete $options{f} if $ignore_fuzzy;
47 my $silent_mode = defined($options{i});
48 delete $options{i} if $silent_mode;
50 my $check_args = (!%options or defined($options{a}));
51 my $check_colons = (!%options or defined($options{c}));
52 my $check_spaces = (!%options or defined($options{m}));
53 my $check_periods = (!%options or defined($options{p}));
54 my $check_qt = (!%options or defined($options{q}));
55 my $check_menu = (!%options or defined($options{s}));
56 my $check_trans = (!%options or defined($options{t}));
60 foreach my $pofilename ( @ARGV ) {
63 print "Processing po file '$pofilename'...\n";
66 open( INPUT, "<$pofilename" )
67 || die "Cannot read po file '$pofilename'";
72 keys( %trans ) = 10000;
74 my $noOfLines = $#pofile;
79 my ($msgid, $msgstr, $more);
81 while ($i <= $noOfLines) {
83 ( $msgid ) = ( $pofile[$i] =~ m/^msgid "(.*)"/ );
87 my $previous = $pofile[$i - 2];
88 next if $previous =~ m/#,.*fuzzy/;
91 # some msgid's are more than one line long, so add those.
92 while ( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) {
93 $msgid = $msgid . $more;
97 # now look for the associated msgstr.
98 until ( ( $msgstr ) = ( $pofile[$i] =~ m/^msgstr "(.*)"/ ) ) { $i++; };
100 # again collect any extra lines.
101 while ( ( $i <= $noOfLines ) &&
102 ( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) ) {
103 $msgstr = $msgstr . $more;
107 # nothing to do if one of them is empty.
108 # (surely that is always $msgstr?)
109 next if ($msgid eq "" or $msgstr eq "");
111 # Check for matching %1$s, etc.
113 my @argstrs = ( $msgid =~ m/%(\d)\$s/g );
116 foreach my $arg (@argstrs) { $n = $arg if $arg > $n; }
118 print "$pofilename, line $linenum: Problem finding arguments in:\n $msgid!\n"
120 ++$bad{"Missing arguments"};
123 foreach my $i (1..$n) {
124 my $arg = "%$i\\\$s";
125 if ( $msgstr !~ m/$arg/ ) {
126 print "$pofilename, line $linenum: Missing argument `$arg'\n '$msgid' ==> '$msgstr'\n"
128 ++$bad{"Missing arguments"};
137 # Check colon at the end of a message
138 if ( ( $msgid =~ m/: *(\|.*)?$/ ) != ( $msgstr =~ m/: *(\|.*)?$/ ) ) {
139 print "Line $linenum: Missing or unexpected colon:\n '$msgid' => '$msgstr'\n"
141 ++$bad{"Bad colons"};
146 if ($check_periods) {
147 # Check period at the end of a message; uncomment code if you are paranoid
148 if ( ( $msgid =~ m/\. *(\|.*)?$/ ) != ( $msgstr =~ m/\. *(\|.*)?$/ ) ) {
149 print "Line $linenum: Missing or unexpected period:\n '$msgid' => '$msgstr'\n"
151 ++$bad{"Bad periods"};
157 # Check space at the end of a message
158 if ( ( $msgid =~ m/ *?(\|.*)?$/ ) != ( $msgstr =~ m/ *?(\|.*)?$/ ) ) {
159 print "Line $linenum: Missing or unexpected space:\n '$msgid' => '$msgstr'\n"
161 ++$bad{"Bad spaces"};
167 # Check for "&" shortcuts
168 if ( ( $msgid =~ m/&[^ ]/ ) != ( $msgstr =~ m/&[^ ]/ ) ) {
169 print "Line $linenum: Missing or unexpected Qt shortcut:\n '$msgid' => '$msgstr'\n"
171 ++$bad{"Bad Qt shortcuts"};
177 # Check for "|..." shortcuts
178 if ( ( $msgid =~ m/\|[^ ]/ ) != ( $msgstr =~ m/\|[^ ]/ ) ) {
179 print "Line $linenum: Missing or unexpected menu shortcut:\n '$msgid' => '$msgstr'\n"
181 ++$bad{"Bad menu shortcuts"};
186 next unless $check_trans;
188 # we now collect these translations in a hash.
189 # this will allow us to check below if we have translated
190 # anything more than one way.
191 my $msgid_clean = lc($msgid);
192 my $msgstr_clean = lc($msgstr);
194 $msgid_clean =~ s/(.*)\|.*?$/$1/; # strip menu shortcuts
195 $msgstr_clean =~ s/(.*)\|.*?$/$1/;
196 $msgid_clean =~ s/&([^ ])/$1/; # strip Qt shortcuts
197 $msgstr_clean =~ s/&([^ ])/$1/;
199 # this is a hash of hashes. the keys of the outer hash are
200 # cleaned versions of ORIGINAL strings. the keys of the inner hash
201 # are the cleaned versions of their TRANSLATIONS. The value for the
202 # inner hash is an array of the orignal string and translation.
203 $trans{$msgid_clean}{$msgstr_clean} = [ $msgid, $msgstr, $linenum ];
207 foreach $msgid ( keys %trans ) {
208 # so $ref is a reference to the inner hash.
209 my $ref = $trans{$msgid};
210 # @msgstrkeys is an array of the keys of that inner hash.
211 my @msgstrkeys = keys %$ref;
213 # do we have more than one such key?
214 if ( $#msgstrkeys > 0 ) {
216 print "Different translations for '$msgid':\n";
217 foreach $msgstr ( @msgstrkeys ) {
218 print "Line $ref->{$msgstr}[2]: '" .
219 $ref->{$msgstr}[0] . "' => '" .
220 $ref->{$msgstr}[1] . "'\n";
223 ++$bad{"Inconsistent translations"};
230 while (my ($k, $v) = each %bad) { print "$k: $v\n"; }
231 if (scalar(keys %bad) > 1) {
232 print "Total warnings: $warn\n";
235 print "No warnings!\n";