]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Update tex2lyx test cases to format 471
[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) 2010-2013 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 BEGIN {
35     use File::Spec;
36     my $p = File::Spec->rel2abs( __FILE__ );
37     $p =~ s/[\/\\]?diff_po\.pl$//;
38     unshift(@INC, "$p");
39 }
40
41 use strict;
42 use parsePoLine;
43 use Term::ANSIColor qw(:constants);
44 use File::Temp;
45
46 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
47
48 my %Messages = ();              # Used for original po-file
49 my %newMessages = ();           # new po-file
50 my %Untranslated = ();          # inside new po-file
51 my %Fuzzy = ();                 # inside new po-file
52 my $result = 0;                 # exit value
53 my $printlines = 1;
54 my @names = ();
55
56 # Check first, if called as standalone program for git
57 if ($ARGV[0] =~ /^-r(.*)/) {
58   my $rev = $1;
59   shift(@ARGV);
60   if ($rev eq "") {
61     $rev = shift(@ARGV);
62   }
63   for my $argf (@ARGV) {
64     my $baseargf;
65     my $filedir;
66     if ($argf =~ /^(.*)\/([^\/]+)$/) {
67       $baseargf = $2;
68       $filedir = $1;
69     }
70     else {
71       $baseargf = $argf;
72       $filedir = ".";
73     }
74     if (-d "$filedir/../.git") {
75       my @args = ();
76       my $tmpfile = File::Temp->new();
77       push(@args, "-L", $argf . "    (" . $rev . ")");
78       push(@args, "-L", $argf . "    (local copy)");
79       open(FI, "git show $rev:po/$baseargf|");
80       $tmpfile->unlink_on_destroy( 1 );
81       while(my $l = <FI>) {
82         print $tmpfile $l;
83       }
84       close(FI);
85       $tmpfile->seek( 0, SEEK_END );            # Flush()
86       push(@args, $tmpfile->filename, $argf);
87       print "===================================================================\n";
88       &diff_po(@args);
89     }
90     elsif (-d "$filedir/.svn") {
91       # call it again indirectly
92       my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $argf);
93       print "cmd = " . join(' ', @cmd) . "\n";
94       system(@cmd);
95     }
96   }
97 }
98 else {
99   &diff_po(@ARGV);
100 }
101
102 exit($result);
103 #########################################################
104
105 sub diff_po($$)
106 {
107   my @args = @_;
108   %Messages = ();
109   %newMessages = ();
110   %Untranslated = ();
111   %Fuzzy = ();
112   @names = ();
113   while(defined($args[0])) {
114     last if ($args[0] !~ /^\-/);
115     my $param = shift(@args);
116     if ($param eq "-L") {
117       my $name = shift(@args);
118       push(@names, $name);
119     }
120   }
121   if (! defined($names[0])) {
122     push(@names, "original");
123   }
124   if (! defined($names[1])) {
125     push(@names, "new");
126   }
127
128   if (@args != 2) {
129     die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
130   }
131
132   &check($names[0], $args[0]);
133   &check($names[1], $args[1]);
134
135   &parse_po_file($args[0], \%Messages);
136   &parse_po_file($args[1], \%newMessages);
137
138   my @MsgKeys = &getLineSortedKeys(\%newMessages);
139
140   print RED "<<< \"$names[0]\"\n", RESET;
141   print GREEN ">>> \"$names[1]\"\n", RESET;
142   for my $k (@MsgKeys) {
143     if ($newMessages{$k}->{msgstr} eq "") {
144       # this is still untranslated string
145       $Untranslated{$newMessages{$k}->{line}} = $k;
146     }
147     elsif ($newMessages{$k}->{fuzzy}) {
148       #fuzzy string
149       $Fuzzy{$newMessages{$k}->{line}} = $k;
150     }
151     if (exists($Messages{$k})) {
152       &printIfDiff($k, $Messages{$k}, $newMessages{$k});
153       delete($Messages{$k});
154       delete($newMessages{$k});
155     }
156   }
157
158   if (0) {
159     @MsgKeys = sort keys %Messages, keys %newMessages;
160     for my $k (@MsgKeys) {
161       if (defined($Messages{$k})) {
162         $result |= 8;
163         print "deleted message\n";
164         print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
165         print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
166         print RED "< msgid = \"$k\"\n", RESET;
167         print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
168       }
169       if (defined($newMessages{$k})) {
170         $result |= 16;
171         print "new message\n";
172         print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
173         print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
174         print GREEN "> msgid = \"$k\"\n", RESET;
175         print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
176       }
177     }
178   }
179   else {
180     @MsgKeys = &getLineSortedKeys(\%Messages);
181     for my $k (@MsgKeys) {
182       $result |= 8;
183       print "deleted message\n";
184       print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
185       print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
186       print RED "< msgid = \"$k\"\n", RESET;
187       print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
188     }
189
190     @MsgKeys = &getLineSortedKeys(\%newMessages);
191     for my $k (@MsgKeys) {
192       $result |= 16;
193       print "new message\n";
194       print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
195       print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
196       print GREEN "> msgid = \"$k\"\n", RESET;
197       print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
198     }
199   }
200   &printExtraMessages("fuzzy", \%Fuzzy);
201   &printExtraMessages("untranslated", \%Untranslated);
202 }
203
204 sub check($$)
205 {
206   my ($spec, $filename) = @_;
207
208   if (! -e $filename ) {
209     die("$spec po file does not exist");
210   }
211   if ( ! -f $filename ) {
212     die("$spec po file is not regular");
213   }
214   if ( ! -r $filename ) {
215     die("$spec po file is not readable");
216   }
217 }
218
219 sub printDiff($$$$)
220 {
221   my ($k, $nk, $rM, $rnM) = @_;
222   print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
223   print "  msgid = \"$k\"\n";
224   if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
225     print "  fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
226   }
227   else {
228     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
229   }
230   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
231   if ($k ne $nk) {
232     print GREEN "> msgid = \"$nk\"\n", RESET;
233   }
234   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
235     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
236   }
237   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
238   print "\n";
239 }
240
241 sub printIfDiff($$$)
242 {
243   my ($k, $rM, $rnM) = @_;
244   my $doprint = 0;
245   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
246   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
247   if ($doprint) {
248     $result |= 4;
249     &printDiff($k, $k, $rM, $rnM);
250   }
251 }
252
253 sub printExtraMessages($$)
254 {
255   my ($type, $rExtra) = @_;
256   my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
257
258   if (@UntranslatedKeys > 0) {
259     print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
260     for my $l (@UntranslatedKeys) {
261       print "> line $l: \"" . $rExtra->{$l} . "\"\n"; 
262     }
263   }
264 }