forked from zonemaster/zonemaster-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-msg-args
executable file
·110 lines (83 loc) · 2.63 KB
/
check-msg-args
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use open qw(:std :utf8);
use feature 'say';
use Locale::PO;
use Try::Tiny;
use Getopt::Long qw( GetOptions );
use Pod::Usage qw( pod2usage );
sub extract_perl_braces {
my $string = shift;
# Strip text before the first and after the last brace
$string =~ s/^[^{]*{?|}?[^}]*$//g;
# Extract the fields
my @fields = split /}[^{]*{/, $string;
return @fields;
}
sub diff_perl_braces {
my $got_string = shift // die 'undefined value for $got_string';
my $expected_string = shift // die 'undefined value for $expected_string';
my %got_fields = map { $_ => 1 } extract_perl_braces( $got_string );
my %expected_fields = map { $_ => 1 } extract_perl_braces( $expected_string );
my %missing = %expected_fields;
delete @missing{ keys %got_fields };
my %extra = %got_fields;
delete @extra{ keys %expected_fields };
return [ sort keys %missing ], [ sort keys %extra ];
}
sub check_perl_braces {
my $po = shift;
my $filename = shift;
my $msgid = $po->dequote( $po->msgid );
my $msgstr = $po->dequote( $po->msgstr );
my ( $missing, $extra ) = diff_perl_braces( $msgstr, $msgid );
if ( @{$missing} or @{$extra} ) {
say "";
say '# ' . $filename . ' line ' . $po->loaded_line_number;
print map { "#. $_\n" } split /\n/, $po->automatic // '';
say "msgid ", $po->msgid;
say "msgstr ", $po->msgstr;
for my $field ( @{$missing} ) {
say " Only in msgid: {$field}";
}
for my $field ( @{$extra} ) {
say " Only in msgstr: {$field}";
}
}
return;
}
my $opt_help = 0;
my $opt_man = 0;
GetOptions(
"help|h" => \$opt_help,
"man" => \$opt_man,
) or pod2usage( 2 );
pod2usage( 1 ) if $opt_help;
pod2usage( -verbose => 2 ) if $opt_man;
for my $filename ( @ARGV ) {
my $aref = Locale::PO->load_file_asarray( $filename, 'UTF-8' ) // die "Failed to load PO file '$filename'";
for my $po ( @$aref ) {
try {
if ( $po->has_flag( 'perl-brace-format' ) ) {
check_perl_braces( $po, $filename );
}
}
catch {
say STDERR "error processing $filename at line " . $po->loaded_line_number . ": " . $_;
exit 1;
};
}
}
=head1 NAME
check-msg-args - Verify that PO files have the same args in the msgid and msgstr of their perl-brace-format messages
=head1 SYNOPSIS
check-msg-args [options] [file ...]
=head1 OPTIONS
=over 4
=item B<--help>
Print a brief help message and exit.
=item B<--man>
Print the manual page and exit.
=back