2004-10-28 14:35:53 +00:00
|
|
|
#! /usr/bin/perl -w
|
2003-03-10 12:57:57 +00:00
|
|
|
|
|
|
|
# file pocheck.pl
|
|
|
|
#
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
#
|
2005-09-06 17:20:44 +00:00
|
|
|
# author: Michael Gerz, michael.gerz@teststep.org
|
2003-03-10 12:57:57 +00:00
|
|
|
#
|
|
|
|
|
2011-04-05 22:13:48 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2011-04-05 22:32:32 +00:00
|
|
|
use Getopt::Std;
|
|
|
|
|
|
|
|
my $usage = <<EOT;
|
|
|
|
pocheck.pl [-acmpqst] po_file [po_file] ...
|
|
|
|
|
2011-04-05 22:58:08 +00:00
|
|
|
This script performs some consistency checks on po files.
|
2011-04-05 22:32:32 +00:00
|
|
|
|
2011-04-05 22:58:08 +00:00
|
|
|
We check for everything listed here, unless one or more of these
|
|
|
|
options is given, in which case we checks only for those requested.
|
2011-04-05 22:32:32 +00:00
|
|
|
-a: Check arguments, like %1\$s
|
|
|
|
-c: Check for colons at end
|
|
|
|
-m: Check for menu shortcuts
|
|
|
|
-p: Check for period at end
|
|
|
|
-q: Check Qt shortcuts
|
|
|
|
-s: Check for space at end
|
2011-04-05 22:58:08 +00:00
|
|
|
-t: Check for uniform translation
|
2011-04-06 00:38:55 +00:00
|
|
|
These options can be given with or without other options.
|
2011-04-06 10:17:51 +00:00
|
|
|
-f: Ignore fuzzy translations
|
2011-04-05 22:58:08 +00:00
|
|
|
-w: Only report summary total of errors
|
2011-04-06 10:17:51 +00:00
|
|
|
-i: Silent mode, report only errors
|
2011-04-05 22:32:32 +00:00
|
|
|
EOT
|
|
|
|
|
|
|
|
my %options;
|
2011-04-06 10:17:51 +00:00
|
|
|
getopts(":hacfmpqstwi", \%options);
|
2011-04-05 22:32:32 +00:00
|
|
|
|
|
|
|
if (defined($options{h})) {
|
|
|
|
print $usage;
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2011-04-05 22:58:08 +00:00
|
|
|
my $only_total = defined($options{w});
|
|
|
|
delete $options{w} if $only_total;
|
2011-04-06 00:38:55 +00:00
|
|
|
my $ignore_fuzzy = defined($options{f});
|
|
|
|
delete $options{f} if $ignore_fuzzy;
|
2011-04-06 10:17:51 +00:00
|
|
|
my $silent_mode = defined($options{i});
|
|
|
|
delete $options{i} if $silent_mode;
|
2011-04-05 22:58:08 +00:00
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
my $check_args = (!%options or defined($options{a}));
|
|
|
|
my $check_colons = (!%options or defined($options{c}));
|
2012-07-03 12:36:44 +00:00
|
|
|
my $check_spaces = (!%options or defined($options{s}));
|
2011-04-05 22:32:32 +00:00
|
|
|
my $check_periods = (!%options or defined($options{p}));
|
|
|
|
my $check_qt = (!%options or defined($options{q}));
|
2012-07-03 12:36:44 +00:00
|
|
|
my $check_menu = (!%options or defined($options{m}));
|
2011-04-05 22:32:32 +00:00
|
|
|
my $check_trans = (!%options or defined($options{t}));
|
2011-04-05 22:13:48 +00:00
|
|
|
|
|
|
|
my %trans;
|
|
|
|
|
2013-10-12 00:35:33 +00:00
|
|
|
my $total_warn = 0;
|
|
|
|
|
2011-04-05 22:58:08 +00:00
|
|
|
foreach my $pofilename ( @ARGV ) {
|
|
|
|
my %bad;
|
2011-04-06 10:17:51 +00:00
|
|
|
if (!$silent_mode) {
|
|
|
|
print "Processing po file '$pofilename'...\n";
|
|
|
|
}
|
2003-03-10 12:57:57 +00:00
|
|
|
|
2004-10-28 14:35:53 +00:00
|
|
|
open( INPUT, "<$pofilename" )
|
2003-03-10 12:57:57 +00:00
|
|
|
|| die "Cannot read po file '$pofilename'";
|
2011-04-05 22:13:48 +00:00
|
|
|
my @pofile = <INPUT>;
|
2003-03-10 12:57:57 +00:00
|
|
|
close( INPUT );
|
|
|
|
|
|
|
|
undef( %trans );
|
|
|
|
keys( %trans ) = 10000;
|
|
|
|
|
2011-04-05 22:13:48 +00:00
|
|
|
my $noOfLines = $#pofile;
|
|
|
|
|
|
|
|
my $warn = 0;
|
2004-10-28 14:35:53 +00:00
|
|
|
|
2011-04-05 22:13:48 +00:00
|
|
|
my $i = 0;
|
|
|
|
my ($msgid, $msgstr, $more);
|
2004-10-28 14:35:53 +00:00
|
|
|
|
2003-03-10 12:57:57 +00:00
|
|
|
while ($i <= $noOfLines) {
|
2011-04-05 22:36:30 +00:00
|
|
|
my $linenum = $i;
|
2011-04-05 21:48:55 +00:00
|
|
|
( $msgid ) = ( $pofile[$i] =~ m/^msgid "(.*)"/ );
|
|
|
|
$i++;
|
|
|
|
next unless $msgid;
|
2011-04-06 00:38:55 +00:00
|
|
|
if ($ignore_fuzzy) {
|
|
|
|
my $previous = $pofile[$i - 2];
|
|
|
|
next if $previous =~ m/#,.*fuzzy/;
|
|
|
|
}
|
2011-04-05 21:48:55 +00:00
|
|
|
|
|
|
|
# some msgid's are more than one line long, so add those.
|
|
|
|
while ( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) {
|
|
|
|
$msgid = $msgid . $more;
|
2003-03-10 12:57:57 +00:00
|
|
|
$i++;
|
2011-04-05 21:48:55 +00:00
|
|
|
}
|
2011-04-05 22:11:06 +00:00
|
|
|
|
2011-04-05 21:48:55 +00:00
|
|
|
# now look for the associated msgstr.
|
|
|
|
until ( ( $msgstr ) = ( $pofile[$i] =~ m/^msgstr "(.*)"/ ) ) { $i++; };
|
|
|
|
$i++;
|
|
|
|
# again collect any extra lines.
|
|
|
|
while ( ( $i <= $noOfLines ) &&
|
|
|
|
( ( $more ) = $pofile[$i] =~ m/^"(.*)"/ ) ) {
|
|
|
|
$msgstr = $msgstr . $more;
|
2003-03-10 12:57:57 +00:00
|
|
|
$i++;
|
2011-04-05 21:48:55 +00:00
|
|
|
}
|
2003-03-10 12:57:57 +00:00
|
|
|
|
2011-04-05 21:48:55 +00:00
|
|
|
# nothing to do if one of them is empty.
|
|
|
|
# (surely that is always $msgstr?)
|
|
|
|
next if ($msgid eq "" or $msgstr eq "");
|
|
|
|
|
2012-07-03 12:36:44 +00:00
|
|
|
# discard [[...]] from the end of msgid, this is used only as hint to translation
|
|
|
|
$msgid =~ s/\[\[.*\]\]$//;
|
|
|
|
|
2011-04-05 22:11:06 +00:00
|
|
|
# Check for matching %1$s, etc.
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_args) {
|
|
|
|
my @argstrs = ( $msgid =~ m/%(\d)\$s/g );
|
|
|
|
if (@argstrs) {
|
|
|
|
my $n = 0;
|
|
|
|
foreach my $arg (@argstrs) { $n = $arg if $arg > $n; }
|
|
|
|
if ($n <= 0) {
|
2011-04-06 10:17:51 +00:00
|
|
|
print "$pofilename, line $linenum: Problem finding arguments in:\n $msgid!\n"
|
2011-04-05 22:58:08 +00:00
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Missing arguments"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
} else {
|
|
|
|
foreach my $i (1..$n) {
|
|
|
|
my $arg = "%$i\\\$s";
|
|
|
|
if ( $msgstr !~ m/$arg/ ) {
|
2011-04-06 10:17:51 +00:00
|
|
|
print "$pofilename, line $linenum: Missing argument `$arg'\n '$msgid' ==> '$msgstr'\n"
|
2011-04-05 22:58:08 +00:00
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Missing arguments"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
2011-04-05 22:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_colons) {
|
|
|
|
# Check colon at the end of a message
|
|
|
|
if ( ( $msgid =~ m/: *(\|.*)?$/ ) != ( $msgstr =~ m/: *(\|.*)?$/ ) ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
print "Line $linenum: Missing or unexpected colon:\n '$msgid' => '$msgstr'\n"
|
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Bad colons"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
2011-04-05 21:48:55 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_periods) {
|
|
|
|
# Check period at the end of a message; uncomment code if you are paranoid
|
|
|
|
if ( ( $msgid =~ m/\. *(\|.*)?$/ ) != ( $msgstr =~ m/\. *(\|.*)?$/ ) ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
print "Line $linenum: Missing or unexpected period:\n '$msgid' => '$msgstr'\n"
|
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Bad periods"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
2011-04-05 21:48:55 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_spaces) {
|
|
|
|
# Check space at the end of a message
|
|
|
|
if ( ( $msgid =~ m/ *?(\|.*)?$/ ) != ( $msgstr =~ m/ *?(\|.*)?$/ ) ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
print "Line $linenum: Missing or unexpected space:\n '$msgid' => '$msgstr'\n"
|
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Bad spaces"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
2003-03-10 12:57:57 +00:00
|
|
|
}
|
2011-04-05 21:48:55 +00:00
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_qt) {
|
|
|
|
# Check for "&" shortcuts
|
|
|
|
if ( ( $msgid =~ m/&[^ ]/ ) != ( $msgstr =~ m/&[^ ]/ ) ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
print "Line $linenum: Missing or unexpected Qt shortcut:\n '$msgid' => '$msgstr'\n"
|
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Bad Qt shortcuts"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
2011-04-05 21:48:55 +00:00
|
|
|
}
|
2011-04-05 22:32:32 +00:00
|
|
|
|
|
|
|
if ($check_menu) {
|
|
|
|
# Check for "|..." shortcuts
|
|
|
|
if ( ( $msgid =~ m/\|[^ ]/ ) != ( $msgstr =~ m/\|[^ ]/ ) ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
print "Line $linenum: Missing or unexpected menu shortcut:\n '$msgid' => '$msgstr'\n"
|
|
|
|
unless $only_total;
|
|
|
|
++$bad{"Bad menu shortcuts"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next unless $check_trans;
|
2011-04-05 22:11:06 +00:00
|
|
|
|
2011-04-05 21:48:55 +00:00
|
|
|
# we now collect these translations in a hash.
|
|
|
|
# this will allow us to check below if we have translated
|
|
|
|
# anything more than one way.
|
2011-04-05 22:13:48 +00:00
|
|
|
my $msgid_clean = lc($msgid);
|
|
|
|
my $msgstr_clean = lc($msgstr);
|
2011-04-05 21:48:55 +00:00
|
|
|
|
|
|
|
$msgid_clean =~ s/(.*)\|.*?$/$1/; # strip menu shortcuts
|
|
|
|
$msgstr_clean =~ s/(.*)\|.*?$/$1/;
|
|
|
|
$msgid_clean =~ s/&([^ ])/$1/; # strip Qt shortcuts
|
|
|
|
$msgstr_clean =~ s/&([^ ])/$1/;
|
|
|
|
|
|
|
|
# this is a hash of hashes. the keys of the outer hash are
|
|
|
|
# cleaned versions of ORIGINAL strings. the keys of the inner hash
|
|
|
|
# are the cleaned versions of their TRANSLATIONS. The value for the
|
|
|
|
# inner hash is an array of the orignal string and translation.
|
2011-04-05 22:37:44 +00:00
|
|
|
$trans{$msgid_clean}{$msgstr_clean} = [ $msgid, $msgstr, $linenum ];
|
2003-03-10 12:57:57 +00:00
|
|
|
}
|
2003-04-28 14:03:28 +00:00
|
|
|
|
2011-04-05 22:32:32 +00:00
|
|
|
if ($check_trans) {
|
|
|
|
foreach $msgid ( keys %trans ) {
|
|
|
|
# so $ref is a reference to the inner hash.
|
|
|
|
my $ref = $trans{$msgid};
|
|
|
|
# @msgstrkeys is an array of the keys of that inner hash.
|
|
|
|
my @msgstrkeys = keys %$ref;
|
|
|
|
|
|
|
|
# do we have more than one such key?
|
|
|
|
if ( $#msgstrkeys > 0 ) {
|
2011-04-05 22:58:08 +00:00
|
|
|
if (!$only_total) {
|
|
|
|
print "Different translations for '$msgid':\n";
|
|
|
|
foreach $msgstr ( @msgstrkeys ) {
|
|
|
|
print "Line $ref->{$msgstr}[2]: '" .
|
|
|
|
$ref->{$msgstr}[0] . "' => '" .
|
|
|
|
$ref->{$msgstr}[1] . "'\n";
|
|
|
|
}
|
2011-04-05 22:32:32 +00:00
|
|
|
}
|
2011-04-05 22:58:08 +00:00
|
|
|
++$bad{"Inconsistent translations"};
|
2011-04-05 22:32:32 +00:00
|
|
|
$warn++;
|
2003-04-28 14:03:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 10:17:51 +00:00
|
|
|
if (!$silent_mode) {
|
|
|
|
if ($warn) {
|
|
|
|
while (my ($k, $v) = each %bad) { print "$k: $v\n"; }
|
|
|
|
if (scalar(keys %bad) > 1) {
|
|
|
|
print "Total warnings: $warn\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print "No warnings!\n";
|
2011-04-05 23:02:12 +00:00
|
|
|
}
|
2011-04-06 10:17:51 +00:00
|
|
|
print "\n";
|
2011-04-05 23:02:12 +00:00
|
|
|
}
|
2013-10-12 00:35:33 +00:00
|
|
|
$total_warn += $warn;
|
2003-03-10 12:57:57 +00:00
|
|
|
}
|
2013-10-12 00:35:33 +00:00
|
|
|
|
|
|
|
exit ($total_warn > 0);
|
|
|
|
|