File: check_consistency

package info (click to toggle)
kde-i18n 4%3A2.2.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 341,220 kB
  • ctags: 327
  • sloc: sh: 10,878; perl: 2,910; cpp: 1,319; makefile: 1,016; python: 258
file content (149 lines) | stat: -rwxr-xr-x 3,031 bytes parent folder | download | duplicates (4)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl -w
#
# Author: Petter Reinholdtsen <pere@td.org.uit.no>
# Date:   2001-08-04
# Source: http://www.student.uit.no/~pere/linux/
#
# Check .po files for consistency.  Write two files msg-all and
# msg-inconsistent.
#
# Usage: $0 <po-files>

#use strict;
use Getopt::Std;

getopt('a');

my ($pofile, %translation, @problem, %msgstrs, %files);

my $showfile = 1;

for $pofile ( @ARGV ) {
  open(POFILE, "<$pofile") || next;

  my ($next, $msgid, $msgstr);

  while (1) {
    $_ = $next || <POFILE>;
    last unless $_;
    $next = "";

    chomp;

    s/^\#.+$//; # Remove comments

    next if (/^\s*$/);

    if (/^msgid (\".+)/) {
      $msgid = $1;
      while (<POFILE>) {
        chomp;
        unless (/^\"/) {
          $next = $_;
          last;
        }
        $msgid .= $_;
        $msgid =~ s/\"\"//;
      }

      $msgstr = "";
      next;
    }

    if (/^msgstr (\".+)/) {
      $msgstr = $1;
      while (<POFILE>) {
        chomp;
        unless (/^\"/) {
          $next = $_;
          last;
        }
        $msgstr .= $_;
        $msgstr =~ s/\"\"//;
      }

      add_translation($pofile, $msgid, $msgstr);

      $msgid = "";
      $msgstr = "";
    }

  }
  close(POFILE);
}

sub add_translation {
  my ($pofile, $msgid, $msgstr) = @_;
#  print " Translation: $msgid = $msgstr\n";
  $msgstr = $1;
  $msgstrs{"$msgid\t$msgstr"}++;
  $files{"$msgid\t$msgstr"} = $pofile;
  if ( !defined($translation{$msgid}) ) {
    push(@{$translation{$msgid}}, $msgstr);
  } else {
    if ( ! in_array($msgstr, $translation{$msgid}) ) {
      push(@{$translation{$msgid}}, $msgstr);
      push(@problem, $msgid) unless grep($msgid, @problem);
    }
  }
  $msgid = "";
}

sub in_array ($$) {
  my ($str, $aref) = @_;
  my $test;
  for $test (@$aref) {
    return 1 if ($test eq $str);
  }
  return 0;
}

sub output_transl {
  my ($fp, $msgid) = @_;
  print $fp "\nmsgid $msgid\n";
  my $msgstr;
  for $msgstr (sort @{$translation{$msgid}}) {
    print $fp "msgstr $msgstr # (" . $msgstrs{"$msgid\t$msgstr"}. ") " or die;
    print $fp "[" . $files{"$msgid\t$msgstr"} . "]" or die if $showfile;
    print $fp "\n" or die;
  }
}

output();

# Remove leading space and '&' before comparing
sub kde_magic_sort {
  my $aa = $a;
  my $bb = $b;

  $aa =~ s/\&|^\s+//;
  $bb =~ s/\&|^\s+//;
  return lc($aa) cmp lc($bb);
}

sub output {
  my ($msgid);
  my ($count, $icount) = (0,0);
  if ($opt_a) {
      open (ALL, ">msg-all");
      print "Creating msg-all\n";
  }
  open (INCONS, ">msg-inconsistent");
  print "Creating msg-inconsistent\n";
  for $msgid (sort kde_magic_sort keys %translation ) {
      if ($opt_a) {
          output_transl(ALL, $msgid);
      }
      $count++;
      if ( 1 < scalar(@{$translation{$msgid}}) ) {
          $icount++;
          output_transl(INCONS, $msgid);
      }
  }
  print ALL "\n#Total # of messages: $count\n" if ($opt_a);
  print INCONS "\n#Total inconsistent: $icount of $count (",
      int(100*$icount/$count), "%)\n";

  close(INCONS);
  close(ALL)  if ($opt_a);
}