]> git.lyx.org Git - features.git/blob - po/diff_po.pl
a7967bc7143f4e0a7731f377b9f12538e4ef91b2
[features.git] / po / diff_po.pl
1 #! /usr/bin/env perl
2
3 # file diff_po.pl
4 # script to compare changes between translation files before merging them
5 #
6 # This file is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
10 #
11 # This software is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public
17 # License along with this software; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 #
20 # author: Kornel Benko, kornel@lyx.org
21 #
22 # TODO: Search for good correlations of deleted and inserted string
23 # using Text::Levenshtein or Algorithm::Diff
24
25 use strict;
26 use Term::ANSIColor qw(:constants);
27
28 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
29
30 my %Messages = ();              # Used for original po-file
31 my %newMessages = ();           # new po-file
32 my %Untranslated = ();          # inside new po-file
33 my %Fuzzy = ();                 # inside new po-file
34 my $result = 0;                 # exit value
35 my $printlines = 0;
36 my @names = ();
37
38
39 while(defined($ARGV[0])) {
40   last if ($ARGV[0] !~ /^\-/);
41   my $param = shift(@ARGV);
42   if ($param eq "-L") {
43     my $name = shift(@ARGV);
44     push(@names, $name);
45   }
46 }
47 if (! defined($names[0])) {
48   push(@names, "original");
49 }
50 if (! defined($names[1])) {
51   push(@names, "new");
52 }
53
54 if (@ARGV != 2) {
55   die('"', join('" "', @names, @ARGV) . "\" Expected exactly 2 parameters");
56 }
57
58 &check($names[0], $ARGV[0]);
59 &check($names[1], $ARGV[1]);
60
61 &parse_po_file($ARGV[0], \%Messages);
62 &parse_po_file($ARGV[1], \%newMessages);
63
64 my @MsgKeys = &getLineSortedKeys(\%newMessages);
65
66 print RED "<<< \"$names[0]\"\n", RESET;
67 print GREEN ">>> \"$names[1]\"\n", RESET;
68 for my $k (@MsgKeys) {
69   if ($newMessages{$k}->{msgstr} eq "") {
70     # this is still untranslated string
71     $Untranslated{$newMessages{$k}->{line}} = $k;
72   }
73   elsif ($newMessages{$k}->{fuzzy}) {
74     #fuzzy string
75     $Fuzzy{$newMessages{$k}->{line}} = $k;
76   }
77   if (exists($Messages{$k})) {
78     &printIfDiff($k, $Messages{$k}, $newMessages{$k});
79     delete($Messages{$k});
80     delete($newMessages{$k});
81   }
82 }
83
84 @MsgKeys = &getLineSortedKeys(\%Messages);
85 for my $k (@MsgKeys) {
86   $result |= 8;
87   print "deleted message\n";
88   print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
89   print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
90   print RED "< msgid = \"$k\"\n", RESET;
91   print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
92 }
93
94 @MsgKeys = &getLineSortedKeys(\%newMessages);
95 for my $k (@MsgKeys) {
96   $result |= 16;
97   print "new message\n";
98   print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
99   print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
100   print GREEN "> msgid = \"$k\"\n", RESET;
101   print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
102 }
103
104 &printExtraMessages("fuzzy", \%Fuzzy);
105 &printExtraMessages("untranslated", \%Untranslated);
106
107 exit($result);
108
109 sub check($$)
110 {
111   my ($spec, $filename) = @_;
112
113   if (! -e $filename ) {
114     die("$spec po file does not exist");
115   }
116   if ( ! -f $filename ) {
117     die("$spec po file is not regular");
118   }
119   if ( ! -r $filename ) {
120     die("$spec po file is not readable");
121   }
122 }
123
124 sub printDiff($$$$)
125 {
126   my ($k, $nk, $rM, $rnM) = @_;
127   print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
128   print "  msgid = \"$k\"\n";
129   if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
130     print "  fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
131   }
132   else {
133     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
134   }
135   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
136   if ($k ne $nk) {
137     print GREEN "> msgid = \"$nk\"\n", RESET;
138   }
139   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
140     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
141   }
142   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
143   print "\n";
144 }
145
146 sub printIfDiff($$$)
147 {
148   my ($k, $rM, $rnM) = @_;
149   my $doprint = 0;
150   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
151   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
152   if ($doprint) {
153     $result |= 4;
154     &printDiff($k, $k, $rM, $rnM);
155   }
156 }
157
158 sub parse_po_file($$)
159 {
160   my ($file, $rMessages) = @_;
161   if (open(FI, '<', $file)) {
162     $status = "normal";
163     $fuzzy = 0;
164     my $lineno = 0;
165     while (my $line = <FI>) {
166       $lineno++;
167       &parse_po_line($line, $lineno, $rMessages);
168     }
169     &parse_po_line("", $lineno + 1, $rMessages);
170     close(FI);
171   }
172 }
173
174 sub parse_po_line($$$)
175 {
176   my ($line, $lineno, $rMessages) = @_;
177   chomp($line);
178
179   if ($status eq "normal") {
180     if ($line =~ /^#, fuzzy/) {
181       $fuzzy = 1;
182     }
183     elsif ($line =~ s/^msgid\s+//) {
184       $foundline = $lineno;
185       $status = "msgid";
186       $msgid = "";
187       &parse_po_line($line);
188     }
189   }
190   elsif ($status eq "msgid") {
191     if ($line =~ /^\s*"(.*)"\s*/) {
192       $msgid .= $1;
193     }
194     elsif ($line =~ s/^msgstr\s+//) {
195       $status = "msgstr";
196       $msgstr = "";
197       &parse_po_line($line);
198     }
199   }
200   elsif ($status eq "msgstr") {
201     if ($line =~ /^\s*"(.*)"\s*/) {
202       $msgstr .= $1;
203     }
204     else {
205       if ($msgid ne "") {
206         $rMessages->{$msgid}->{line} = $foundline;
207         $rMessages->{$msgid}->{fuzzy} = $fuzzy;
208         $rMessages->{$msgid}->{msgstr} = $msgstr;
209       }
210       $fuzzy = 0;
211       $status = "normal";
212     }
213   }
214   else {
215     die("invalid status");
216   }
217 }
218
219 sub getLineSortedKeys($)
220 {
221   my ($rMessages) = @_;
222
223   return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
224 }
225
226 sub printExtraMessages($$)
227 {
228   my ($type, $rExtra) = @_;
229   my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
230
231   if (@UntranslatedKeys > 0) {
232     print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
233     for my $l (@UntranslatedKeys) {
234       print "> line $l: \"" . $rExtra->{$l} . "\"\n"; 
235     }
236   }
237 }