]> git.lyx.org Git - features.git/blob - po/pocheck.pl
Have pocheck.pl check for missing arguments, like %1$s, etc.
[features.git] / po / pocheck.pl
1 #! /usr/bin/perl -w
2
3 # file pocheck.pl
4 #
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # author: Michael Gerz, michael.gerz@teststep.org
9 #
10 # This script performs some consistency checks on po files:
11 #
12 #   1. Uniform translation of messages that are identical except
13 #      for capitalization, shortcuts, and shortcut notation.
14 #   2. Usage of the following elements in both the original and
15 #      the translated message (or no usage at all):
16 #      shortcuts ("&" and "|..."), trailing space, trailing colon
17 #
18 # Invocation:
19 #    pocheck.pl po_file po_file ...
20
21 foreach $pofilename ( @ARGV )
22 {
23   print "Processing po file '$pofilename'...\n";
24
25   open( INPUT, "<$pofilename" )
26     || die "Cannot read po file '$pofilename'";
27   @pofile = <INPUT>;
28   close( INPUT );
29
30   undef( %trans );
31   keys( %trans ) = 10000;
32
33   $noOfLines = $#pofile;
34
35   $warn = 0;
36
37   $i = 0;
38   while ($i <= $noOfLines) {
39     ( $msgid ) = ( $pofile[$i] =~ m/^msgid "(.*)"/ );
40     $i++;
41     next unless $msgid;
42     
43     # some msgid's are more than one line long, so add those.
44     while ( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) {
45       $msgid = $msgid . $more;
46       $i++;
47     }
48     
49     # now look for the associated msgstr.
50     until ( ( $msgstr ) = ( $pofile[$i] =~ m/^msgstr "(.*)"/ ) ) { $i++; };
51     $i++;
52     # again collect any extra lines.
53     while ( ( $i <= $noOfLines ) &&
54             ( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) ) {
55       $msgstr = $msgstr . $more;
56       $i++;
57     }
58
59     # nothing to do if one of them is empty. 
60     # (surely that is always $msgstr?)
61     next if ($msgid eq "" or $msgstr eq "");
62
63     # Check for matching %1$s, etc.
64     @argstrs = ( $msgid =~ m/%(\d)\$s/g );
65     if (@argstrs) {
66       $num = 0;
67       foreach $arg (@argstrs) { $num = $arg if $arg > $num; }
68       if ($num <= 0) { 
69         print "Problem finding arguments in:\n    $msgid!\n";
70         $warn++;
71       } else {
72         foreach $i (1..$num) {
73           $arg = "%$i\\\$s"; 
74           if ( $msgstr !~ m/$arg/ ) {
75             print "Missing argument `$arg'\n  '$msgid' ==> '$msgstr'\n";
76             $warn++;
77           }
78         }
79       }
80     }
81
82     # Check colon at the end of a message
83     if ( ( $msgid =~ m/: *(\|.*)?$/ ) != ( $msgstr =~ m/: *(\|.*)?$/ ) ) {
84       print( "Missing or unexpected colon:\n" );
85       print( "  '$msgid' => '$msgstr'\n" );
86       $warn++;
87     }
88
89     # Check period at the end of a message; uncomment code if you are paranoid
90     #if ( ( $msgid =~ m/\. *(\|.*)?$/ ) != ( $msgstr =~ m/\. *(\|.*)?$/ ) ) {
91     #  print( "Missing or unexpected period:\n" );
92     #  print( "  '$msgid' => '$msgstr'\n" );
93     #  $warn++;
94     #}
95
96     # Check space at the end of a message
97     if ( ( $msgid =~ m/  *?(\|.*)?$/ ) != ( $msgstr =~ m/  *?(\|.*)?$/ ) ) {
98       print( "Missing or unexpected space:\n" );
99       print( "  '$msgid' => '$msgstr'\n" );
100       $warn++;
101     }
102
103     # Check for "&" shortcuts
104     if ( ( $msgid =~ m/&[^ ]/ ) != ( $msgstr =~ m/&[^ ]/ ) ) {
105       print( "Missing or unexpected Qt shortcut:\n" );
106       print( "  '$msgid' => '$msgstr'\n" );
107       $warn++;
108     }
109
110     # Check for "|..." shortcuts
111     if ( ( $msgid =~ m/\|[^ ]/ ) != ( $msgstr =~ m/\|[^ ]/ ) ) {
112       print( "Missing or unexpected menu shortcut:\n" );
113       print( "  '$msgid' => '$msgstr'\n" );
114       $warn++;
115     }
116     
117     # we now collect these translations in a hash.
118     # this will allow us to check below if we have translated
119     # anything more than one way.
120     $msgid_clean  = lc($msgid);
121     $msgstr_clean = lc($msgstr);
122
123     $msgid_clean  =~ s/(.*)\|.*?$/$1/;  # strip menu shortcuts
124     $msgstr_clean =~ s/(.*)\|.*?$/$1/;
125     $msgid_clean  =~ s/&([^ ])/$1/;     # strip Qt shortcuts
126     $msgstr_clean =~ s/&([^ ])/$1/;
127
128     # this is a hash of hashes. the keys of the outer hash are
129     # cleaned versions of ORIGINAL strings. the keys of the inner hash 
130     # are the cleaned versions of their TRANSLATIONS. The value for the 
131     # inner hash is an array of the orignal string and translation.
132     $trans{$msgid_clean}{$msgstr_clean} = [ $msgid, $msgstr ];
133   }
134
135   foreach $msgid ( keys %trans ) {
136     # so $ref is a reference to the inner hash.
137     $ref = $trans{$msgid};
138     # @msgstrkeys is an array of the keys of that inner hash.
139     @msgstrkeys = keys %$ref;
140
141     # do we have more than one such key?
142     if ( $#msgstrkeys > 0 ) {
143       print( "Different translations for '$msgid':\n" );
144       foreach $msgstr ( @msgstrkeys ) {
145         print( "  '" . $trans{$msgid}{$msgstr}[0] . "' => '" . $trans{$msgid}{$msgstr}[1] . "'\n" );
146       }
147       $warn++;
148     }
149   }
150
151   print( "\nTotal number of warnings: $warn\n\n" );
152 }