4 # script to compare changes between translation files before merging them
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.
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.
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
20 # author: Kornel Benko, kornel@lyx.org
22 # TODO: Search for good correlations of deleted and inserted string
23 # using Text::Levenshtein or Algorithm::Diff
26 use Term::ANSIColor qw(:constants);
28 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
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
37 my $tmpfile = "/tmp/blax";
39 # Check first, if called as standalone program for git
40 if ($ARGV[0] =~ /^-r(.*)/) {
46 for my $argf (@ARGV) {
48 ($baseargf = $argf) =~ s/^.*\///;
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);
59 push(@args, $tmpfile, $argf);
68 #########################################################
78 print "========================================================\n";
79 while(defined($args[0])) {
80 last if ($args[0] !~ /^\-/);
81 my $param = shift(@args);
83 my $name = shift(@args);
87 if (! defined($names[0])) {
88 push(@names, "original");
90 if (! defined($names[1])) {
95 die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
98 &check($names[0], $args[0]);
99 &check($names[1], $args[1]);
101 &parse_po_file($args[0], \%Messages);
102 &parse_po_file($args[1], \%newMessages);
104 my @MsgKeys = &getLineSortedKeys(\%newMessages);
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;
113 elsif ($newMessages{$k}->{fuzzy}) {
115 $Fuzzy{$newMessages{$k}->{line}} = $k;
117 if (exists($Messages{$k})) {
118 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
119 delete($Messages{$k});
120 delete($newMessages{$k});
124 @MsgKeys = &getLineSortedKeys(\%Messages);
125 for my $k (@MsgKeys) {
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;
134 @MsgKeys = &getLineSortedKeys(\%newMessages);
135 for my $k (@MsgKeys) {
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;
144 &printExtraMessages("fuzzy", \%Fuzzy);
145 &printExtraMessages("untranslated", \%Untranslated);
150 my ($spec, $filename) = @_;
152 if (! -e $filename ) {
153 die("$spec po file does not exist");
155 if ( ! -f $filename ) {
156 die("$spec po file is not regular");
158 if ( ! -r $filename ) {
159 die("$spec po file is not readable");
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);
172 print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
174 print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
176 print GREEN "> msgid = \"$nk\"\n", RESET;
178 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
179 print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
181 print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
187 my ($k, $rM, $rnM) = @_;
189 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
190 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
193 &printDiff($k, $k, $rM, $rnM);
197 sub parse_po_file($$)
199 my ($file, $rMessages) = @_;
200 if (open(FI, '<', $file)) {
204 while (my $line = <FI>) {
206 &parse_po_line($line, $lineno, $rMessages);
208 &parse_po_line("", $lineno + 1, $rMessages);
213 sub parse_po_line($$$)
215 my ($line, $lineno, $rMessages) = @_;
218 if ($status eq "normal") {
219 if ($line =~ /^#, fuzzy/) {
222 elsif ($line =~ s/^msgid\s+//) {
223 $foundline = $lineno;
226 &parse_po_line($line);
229 elsif ($status eq "msgid") {
230 if ($line =~ /^\s*"(.*)"\s*/) {
233 elsif ($line =~ s/^msgstr\s+//) {
236 &parse_po_line($line);
239 elsif ($status eq "msgstr") {
240 if ($line =~ /^\s*"(.*)"\s*/) {
245 $rMessages->{$msgid}->{line} = $foundline;
246 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
247 $rMessages->{$msgid}->{msgstr} = $msgstr;
254 die("invalid status");
258 sub getLineSortedKeys($)
260 my ($rMessages) = @_;
262 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
265 sub printExtraMessages($$)
267 my ($type, $rExtra) = @_;
268 my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
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";