4 # script to compare changes between translation files before merging them
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
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.
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.
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
27 # Copyright (c) 1010-2011 Kornel Benko, kornel@lyx.org
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
35 use Term::ANSIColor qw(:constants);
38 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
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
48 # Check first, if called as standalone program for git
49 if ($ARGV[0] =~ /^-r(.*)/) {
55 for my $argf (@ARGV) {
58 if ($argf =~ /^(.*)\/([^\/]+)$/) {
66 if (-d "$filedir/../.git") {
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 );
77 push(@args, $tmpfile->filename, $argf);
78 print "===================================================================\n";
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";
94 #########################################################
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);
112 if (! defined($names[0])) {
113 push(@names, "original");
115 if (! defined($names[1])) {
120 die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
123 &check($names[0], $args[0]);
124 &check($names[1], $args[1]);
126 &parse_po_file($args[0], \%Messages);
127 &parse_po_file($args[1], \%newMessages);
129 my @MsgKeys = &getLineSortedKeys(\%newMessages);
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;
138 elsif ($newMessages{$k}->{fuzzy}) {
140 $Fuzzy{$newMessages{$k}->{line}} = $k;
142 if (exists($Messages{$k})) {
143 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
144 delete($Messages{$k});
145 delete($newMessages{$k});
149 @MsgKeys = &getLineSortedKeys(\%Messages);
150 for my $k (@MsgKeys) {
152 print "deleted message\n";
153 print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
154 print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
155 print RED "< msgid = \"$k\"\n", RESET;
156 print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
159 @MsgKeys = &getLineSortedKeys(\%newMessages);
160 for my $k (@MsgKeys) {
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;
169 &printExtraMessages("fuzzy", \%Fuzzy);
170 &printExtraMessages("untranslated", \%Untranslated);
175 my ($spec, $filename) = @_;
177 if (! -e $filename ) {
178 die("$spec po file does not exist");
180 if ( ! -f $filename ) {
181 die("$spec po file is not regular");
183 if ( ! -r $filename ) {
184 die("$spec po file is not readable");
190 my ($k, $nk, $rM, $rnM) = @_;
191 print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
192 print " msgid = \"$k\"\n";
193 if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
194 print " fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
197 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
199 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
201 print GREEN "> msgid = \"$nk\"\n", RESET;
203 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
204 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
206 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
212 my ($k, $rM, $rnM) = @_;
214 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
215 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
218 &printDiff($k, $k, $rM, $rnM);
222 sub parse_po_file($$)
224 my ($file, $rMessages) = @_;
225 if (open(FI, '<', $file)) {
229 while (my $line = <FI>) {
231 &parse_po_line($line, $lineno, $rMessages);
233 &parse_po_line("", $lineno + 1, $rMessages);
238 sub parse_po_line($$$)
240 my ($line, $lineno, $rMessages) = @_;
243 if ($status eq "normal") {
244 if ($line =~ /^#, fuzzy/) {
247 elsif ($line =~ s/^msgid\s+//) {
248 $foundline = $lineno;
251 &parse_po_line($line);
254 elsif ($status eq "msgid") {
255 if ($line =~ /^\s*"(.*)"\s*/) {
258 elsif ($line =~ s/^msgstr\s+//) {
261 &parse_po_line($line);
264 elsif ($status eq "msgstr") {
265 if ($line =~ /^\s*"(.*)"\s*/) {
270 $rMessages->{$msgid}->{line} = $foundline;
271 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
272 $rMessages->{$msgid}->{msgstr} = $msgstr;
279 die("invalid status");
283 sub getLineSortedKeys($)
285 my ($rMessages) = @_;
287 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
290 sub printExtraMessages($$)
292 my ($type, $rExtra) = @_;
293 my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
295 if (@UntranslatedKeys > 0) {
296 print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
297 for my $l (@UntranslatedKeys) {
298 print "> line $l: \"" . $rExtra->{$l} . "\"\n";