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});
150 @MsgKeys = sort keys %Messages, keys %newMessages;
151 for my $k (@MsgKeys) {
152 if (defined($Messages{$k})) {
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;
160 if (defined($newMessages{$k})) {
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;
171 @MsgKeys = &getLineSortedKeys(\%Messages);
172 for my $k (@MsgKeys) {
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;
181 @MsgKeys = &getLineSortedKeys(\%newMessages);
182 for my $k (@MsgKeys) {
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;
191 &printExtraMessages("fuzzy", \%Fuzzy);
192 &printExtraMessages("untranslated", \%Untranslated);
197 my ($spec, $filename) = @_;
199 if (! -e $filename ) {
200 die("$spec po file does not exist");
202 if ( ! -f $filename ) {
203 die("$spec po file is not regular");
205 if ( ! -r $filename ) {
206 die("$spec po file is not readable");
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);
219 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
221 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
223 print GREEN "> msgid = \"$nk\"\n", RESET;
225 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
226 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
228 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
234 my ($k, $rM, $rnM) = @_;
236 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
237 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
240 &printDiff($k, $k, $rM, $rnM);
244 sub parse_po_file($$)
246 my ($file, $rMessages) = @_;
247 if (open(FI, '<', $file)) {
251 while (my $line = <FI>) {
253 &parse_po_line($line, $lineno, $rMessages);
255 &parse_po_line("", $lineno + 1, $rMessages);
260 sub parse_po_line($$$)
262 my ($line, $lineno, $rMessages) = @_;
265 if ($status eq "normal") {
266 if ($line =~ /^#, fuzzy/) {
269 elsif ($line =~ s/^msgid\s+//) {
270 $foundline = $lineno;
273 &parse_po_line($line);
276 elsif ($status eq "msgid") {
277 if ($line =~ /^\s*"(.*)"\s*/) {
280 elsif ($line =~ s/^msgstr\s+//) {
283 &parse_po_line($line);
286 elsif ($status eq "msgstr") {
287 if ($line =~ /^\s*"(.*)"\s*/) {
292 $rMessages->{$msgid}->{line} = $foundline;
293 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
294 $rMessages->{$msgid}->{msgstr} = $msgstr;
301 die("invalid status");
305 sub getLineSortedKeys($)
307 my ($rMessages) = @_;
309 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
312 sub printExtraMessages($$)
314 my ($type, $rExtra) = @_;
315 my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
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";