]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Sort deleted and inserted messages, so that adjacent may possibly be
[lyx.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 # Examples of usage:
7 # ./diff_po.pl cs.po.old cs.po
8 # svn diff -r38367 --diff-cmd ./diff_po.pl cs.po
9 # git difftool --extcmd=./diff_po.pl sk.po
10 # ./diff_po.pl -r HEAD~100 cs.po        #fetch git revision and compare
11 # ./diff_po.pl -r39229 cs.po            #fetch svn revision and compare
12 #
13 # This file is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public
15 # License as published by the Free Software Foundation; either
16 # version 2 of the License, or (at your option) any later version.
17 #
18 # This software is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public
24 # License along with this software; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 #
27 # Copyright (c) 1010-2011 Kornel Benko, kornel@lyx.org
28 #
29 # TODO:
30 # 1.) Check for ".git" or ".svn" to decide about revisioning
31 # 2.) Search for good correlations of deleted <==> inserted string
32 #     using Text::Levenshtein or Algorithm::Diff
33
34 use strict;
35 use Term::ANSIColor qw(:constants);
36 use File::Temp;
37
38 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
39
40 my %Messages = ();              # Used for original po-file
41 my %newMessages = ();           # new po-file
42 my %Untranslated = ();          # inside new po-file
43 my %Fuzzy = ();                 # inside new po-file
44 my $result = 0;                 # exit value
45 my $printlines = 0;
46 my @names = ();
47
48 # Check first, if called as standalone program for git
49 if ($ARGV[0] =~ /^-r(.*)/) {
50   my $rev = $1;
51   shift(@ARGV);
52   if ($rev eq "") {
53     $rev = shift(@ARGV);
54   }
55   for my $argf (@ARGV) {
56     my $baseargf;
57     my $filedir;
58     if ($argf =~ /^(.*)\/([^\/]+)$/) {
59       $baseargf = $2;
60       $filedir = $1;
61     }
62     else {
63       $baseargf = $argf;
64       $filedir = ".";
65     }
66     if (-d "$filedir/../.git") {
67       my @args = ();
68       my $tmpfile = File::Temp->new();
69       push(@args, "-L", $argf . "    (" . $rev . ")");
70       push(@args, "-L", $argf . "    (local copy)");
71       open(FI, "git show $rev:po/$baseargf|");
72       $tmpfile->unlink_on_destroy( 1 );
73       while(my $l = <FI>) {
74         print $tmpfile $l;
75       }
76       close(FI);
77       push(@args, $tmpfile->filename, $argf);
78       print "===================================================================\n";
79       &diff_po(@args);
80     }
81     elsif (-d "$filedir/.svn") {
82       # call it again indirectly
83       my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $argf);
84       print "cmd = " . join(' ', @cmd) . "\n";
85       system(@cmd);
86     }
87   }
88 }
89 else {
90   &diff_po(@ARGV);
91 }
92
93 exit($result);
94 #########################################################
95
96 sub diff_po($$)
97 {
98   my @args = @_;
99   %Messages = ();
100   %newMessages = ();
101   %Untranslated = ();
102   %Fuzzy = ();
103   @names = ();
104   while(defined($args[0])) {
105     last if ($args[0] !~ /^\-/);
106     my $param = shift(@args);
107     if ($param eq "-L") {
108       my $name = shift(@args);
109       push(@names, $name);
110     }
111   }
112   if (! defined($names[0])) {
113     push(@names, "original");
114   }
115   if (! defined($names[1])) {
116     push(@names, "new");
117   }
118
119   if (@args != 2) {
120     die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
121   }
122
123   &check($names[0], $args[0]);
124   &check($names[1], $args[1]);
125
126   &parse_po_file($args[0], \%Messages);
127   &parse_po_file($args[1], \%newMessages);
128
129   my @MsgKeys = &getLineSortedKeys(\%newMessages);
130
131   print RED "<<< \"$names[0]\"\n", RESET;
132   print GREEN ">>> \"$names[1]\"\n", RESET;
133   for my $k (@MsgKeys) {
134     if ($newMessages{$k}->{msgstr} eq "") {
135       # this is still untranslated string
136       $Untranslated{$newMessages{$k}->{line}} = $k;
137     }
138     elsif ($newMessages{$k}->{fuzzy}) {
139       #fuzzy string
140       $Fuzzy{$newMessages{$k}->{line}} = $k;
141     }
142     if (exists($Messages{$k})) {
143       &printIfDiff($k, $Messages{$k}, $newMessages{$k});
144       delete($Messages{$k});
145       delete($newMessages{$k});
146     }
147   }
148
149   if (1) {
150     @MsgKeys = sort keys %Messages, keys %newMessages;
151     for my $k (@MsgKeys) {
152       if (defined($Messages{$k})) {
153         $result |= 8;
154         print "deleted message\n";
155         print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
156         print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
157         print RED "< msgid = \"$k\"\n", RESET;
158         print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
159       }
160       if (defined($newMessages{$k})) {
161         $result |= 16;
162         print "new message\n";
163         print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
164         print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
165         print GREEN "> msgid = \"$k\"\n", RESET;
166         print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
167       }
168     }
169   }
170   else {
171     @MsgKeys = &getLineSortedKeys(\%Messages);
172     for my $k (@MsgKeys) {
173       $result |= 8;
174       print "deleted message\n";
175       print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
176       print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
177       print RED "< msgid = \"$k\"\n", RESET;
178       print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
179     }
180
181     @MsgKeys = &getLineSortedKeys(\%newMessages);
182     for my $k (@MsgKeys) {
183       $result |= 16;
184       print "new message\n";
185       print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
186       print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
187       print GREEN "> msgid = \"$k\"\n", RESET;
188       print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
189     }
190   }
191   &printExtraMessages("fuzzy", \%Fuzzy);
192   &printExtraMessages("untranslated", \%Untranslated);
193 }
194
195 sub check($$)
196 {
197   my ($spec, $filename) = @_;
198
199   if (! -e $filename ) {
200     die("$spec po file does not exist");
201   }
202   if ( ! -f $filename ) {
203     die("$spec po file is not regular");
204   }
205   if ( ! -r $filename ) {
206     die("$spec po file is not readable");
207   }
208 }
209
210 sub printDiff($$$$)
211 {
212   my ($k, $nk, $rM, $rnM) = @_;
213   print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
214   print "  msgid = \"$k\"\n";
215   if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
216     print "  fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
217   }
218   else {
219     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
220   }
221   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
222   if ($k ne $nk) {
223     print GREEN "> msgid = \"$nk\"\n", RESET;
224   }
225   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
226     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
227   }
228   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
229   print "\n";
230 }
231
232 sub printIfDiff($$$)
233 {
234   my ($k, $rM, $rnM) = @_;
235   my $doprint = 0;
236   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
237   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
238   if ($doprint) {
239     $result |= 4;
240     &printDiff($k, $k, $rM, $rnM);
241   }
242 }
243
244 sub parse_po_file($$)
245 {
246   my ($file, $rMessages) = @_;
247   if (open(FI, '<', $file)) {
248     $status = "normal";
249     $fuzzy = 0;
250     my $lineno = 0;
251     while (my $line = <FI>) {
252       $lineno++;
253       &parse_po_line($line, $lineno, $rMessages);
254     }
255     &parse_po_line("", $lineno + 1, $rMessages);
256     close(FI);
257   }
258 }
259
260 sub parse_po_line($$$)
261 {
262   my ($line, $lineno, $rMessages) = @_;
263   chomp($line);
264
265   if ($status eq "normal") {
266     if ($line =~ /^#, fuzzy/) {
267       $fuzzy = 1;
268     }
269     elsif ($line =~ s/^msgid\s+//) {
270       $foundline = $lineno;
271       $status = "msgid";
272       $msgid = "";
273       &parse_po_line($line);
274     }
275   }
276   elsif ($status eq "msgid") {
277     if ($line =~ /^\s*"(.*)"\s*/) {
278       $msgid .= $1;
279     }
280     elsif ($line =~ s/^msgstr\s+//) {
281       $status = "msgstr";
282       $msgstr = "";
283       &parse_po_line($line);
284     }
285   }
286   elsif ($status eq "msgstr") {
287     if ($line =~ /^\s*"(.*)"\s*/) {
288       $msgstr .= $1;
289     }
290     else {
291       if ($msgid ne "") {
292         $rMessages->{$msgid}->{line} = $foundline;
293         $rMessages->{$msgid}->{fuzzy} = $fuzzy;
294         $rMessages->{$msgid}->{msgstr} = $msgstr;
295       }
296       $fuzzy = 0;
297       $status = "normal";
298     }
299   }
300   else {
301     die("invalid status");
302   }
303 }
304
305 sub getLineSortedKeys($)
306 {
307   my ($rMessages) = @_;
308
309   return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
310 }
311
312 sub printExtraMessages($$)
313 {
314   my ($type, $rExtra) = @_;
315   my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
316
317   if (@UntranslatedKeys > 0) {
318     print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
319     for my $l (@UntranslatedKeys) {
320       print "> line $l: \"" . $rExtra->{$l} . "\"\n"; 
321     }
322   }
323 }