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