]> pere.pagekite.me Git - homepage.git/blob - linux/check_po_consistency
Generated.
[homepage.git] / linux / check_po_consistency
1 #!/usr/bin/perl -w
2 #
3 # Author: Petter Reinholdtsen <pere@td.org.uit.no>
4 # Date: 2001-08-04
5 # Source: http://www.student.uit.no/~pere/linux/
6 #
7 # Check .po files for consistency. Write two files msg-all and
8 # msg-inconsistent.
9 #
10 # Usage: $0 <po-files>
11
12 #use strict;
13 use Getopt::Std;
14
15 getopt('a');
16
17 my ($pofile, %translation, @problem, %msgstrs, %files);
18
19 my $showfile = 1;
20
21 for $pofile ( @ARGV ) {
22 open(POFILE, "<$pofile") || next;
23
24 my ($next, $msgid, $msgstr);
25
26 while (1) {
27 $_ = $next || <POFILE>;
28 last unless $_;
29 $next = "";
30
31 chomp;
32
33 s/^\#.+$//; # Remove comments
34
35 next if (/^\s*$/);
36
37 if (/^msgid (\".+)/) {
38 $msgid = $1;
39 while (<POFILE>) {
40 chomp;
41 unless (/^\"/) {
42 $next = $_;
43 last;
44 }
45 $msgid .= $_;
46 $msgid =~ s/\"\"//;
47 }
48
49 $msgstr = "";
50 next;
51 }
52
53 if (/^msgstr (\".+)/) {
54 $msgstr = $1;
55 while (<POFILE>) {
56 chomp;
57 unless (/^\"/) {
58 $next = $_;
59 last;
60 }
61 $msgstr .= $_;
62 $msgstr =~ s/\"\"//;
63 }
64
65 add_translation($pofile, $msgid, $msgstr);
66
67 $msgid = "";
68 $msgstr = "";
69 }
70
71 }
72 close(POFILE);
73 }
74
75 sub add_translation {
76 my ($pofile, $msgid, $msgstr) = @_;
77 # print " Translation: $msgid = $msgstr\n";
78 $msgstr = $1;
79 $msgstrs{"$msgid\t$msgstr"}++;
80 $files{"$msgid\t$msgstr"} = $pofile;
81 if ( !defined($translation{$msgid}) ) {
82 push(@{$translation{$msgid}}, $msgstr);
83 } else {
84 if ( ! in_array($msgstr, $translation{$msgid}) ) {
85 push(@{$translation{$msgid}}, $msgstr);
86 push(@problem, $msgid) unless grep($msgid, @problem);
87 }
88 }
89 $msgid = "";
90 }
91
92 sub in_array ($$) {
93 my ($str, $aref) = @_;
94 my $test;
95 for $test (@$aref) {
96 return 1 if ($test eq $str);
97 }
98 return 0;
99 }
100
101 sub output_transl {
102 my ($fp, $msgid) = @_;
103 print $fp "\nmsgid $msgid\n";
104 my $msgstr;
105 for $msgstr (sort @{$translation{$msgid}}) {
106 print $fp "msgstr $msgstr # (" . $msgstrs{"$msgid\t$msgstr"}. ") " or die;
107 print $fp "[" . $files{"$msgid\t$msgstr"} . "]" or die if $showfile;
108 print $fp "\n" or die;
109 }
110 }
111
112 output();
113
114 # Remove leading space and '&' before comparing
115 sub kde_magic_sort {
116 my $aa = $a;
117 my $bb = $b;
118
119 $aa =~ s/\&|^\s+//;
120 $bb =~ s/\&|^\s+//;
121 return lc($aa) cmp lc($bb);
122 }
123
124 sub output {
125 my ($msgid);
126 my ($count, $icount) = (0,0);
127 if ($opt_a) {
128 open (ALL, ">msg-all");
129 print "Creating msg-all\n";
130 }
131 open (INCONS, ">msg-inconsistent");
132 print "Creating msg-inconsistent\n";
133 for $msgid (sort kde_magic_sort keys %translation ) {
134 if ($opt_a) {
135 output_transl(ALL, $msgid);
136 }
137 $count++;
138 if ( 1 < scalar(@{$translation{$msgid}}) ) {
139 $icount++;
140 output_transl(INCONS, $msgid);
141 }
142 }
143 print ALL "\n#Total # of messages: $count\n" if ($opt_a);
144 print INCONS "\n#Total inconsistent: $icount of $count (",
145 int(100*$icount/$count), "%)\n";
146
147 close(INCONS);
148 close(ALL) if ($opt_a);
149 }