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
27 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
29 my %Messages = (); # Used for original po-file
30 my %newMessages = (); # new po-file
31 my %Untranslated = (); # inside new po-file
32 my %Fuzzy = (); # inside new po-file
33 my $result = 0; # exit value
36 die("Expected exactly 2 parameters");
39 &check("original", $ARGV[0]);
40 &check("new", $ARGV[1]);
42 &parse_po_file($ARGV[0], \%Messages);
43 &parse_po_file($ARGV[1], \%newMessages);
45 my @MsgKeys = &getLineSortedKeys(\%newMessages);
47 for my $k (@MsgKeys) {
48 if ($newMessages{$k}->{msgstr} eq "") {
49 # this is still untranslated string
50 $Untranslated{$newMessages{$k}->{line}} = $k;
52 elsif ($newMessages{$k}->{fuzzy}) {
54 $Fuzzy{$newMessages{$k}->{line}} = $k;
56 if (exists($Messages{$k})) {
57 &printIfDiff($k, $Messages{$k}, $newMessages{$k});
58 delete($Messages{$k});
59 delete($newMessages{$k});
63 @MsgKeys = &getLineSortedKeys(\%Messages);
64 for my $k (@MsgKeys) {
66 print "deleted message\n";
67 print "< line = " . $Messages{$k}->{line} . "\n";
68 print "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n";
69 print "< msgid = \"$k\"\n";
70 print "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n";
73 @MsgKeys = &getLineSortedKeys(\%newMessages);
74 for my $k (@MsgKeys) {
76 print "new message\n";
77 print "> line = " . $newMessages{$k}->{line} . "\n";
78 print "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n";
79 print "> msgid = \"$k\"\n";
80 print "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n";
83 &printExtraMessages("fuzzy", \%Fuzzy);
84 &printExtraMessages("untranslated", \%Untranslated);
90 my ($spec, $filename) = @_;
92 if (! -e $filename ) {
93 die("$spec po file does not exist");
95 if ( ! -f $filename ) {
96 die("$spec po file is not regular");
98 if ( ! -r $filename ) {
99 die("$spec po file is not readable");
105 my ($k, $nk, $rM, $rnM) = @_;
106 print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n";
107 print " msgid = \"$k\"\n";
108 if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
109 print " fuzzy = " . $rM->{fuzzy} . "\n";
112 print "< fuzzy = " . $rM->{fuzzy} . "\n";
114 print "< msgstr = " . $rM->{msgstr} . "\n";
116 print "> msgid = \"$nk\"\n";
118 if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
119 print "> fuzzy = " . $rnM->{fuzzy} . "\n";
121 print "> msgstr = " . $rnM->{msgstr} . "\n";
127 my ($k, $rM, $rnM) = @_;
129 $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
130 $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
133 &printDiff($k, $k, $rM, $rnM);
137 sub parse_po_file($$)
139 my ($file, $rMessages) = @_;
140 if (open(FI, '<', $file)) {
144 while (my $line = <FI>) {
146 &parse_po_line($line, $lineno, $rMessages);
148 &parse_po_line("", $lineno + 1, $rMessages);
153 sub parse_po_line($$$)
155 my ($line, $lineno, $rMessages) = @_;
158 if ($status eq "normal") {
159 if ($line =~ /^#, fuzzy/) {
162 elsif ($line =~ s/^msgid\s+//) {
163 $foundline = $lineno;
166 &parse_po_line($line);
169 elsif ($status eq "msgid") {
170 if ($line =~ /^\s*"(.*)"\s*/) {
173 elsif ($line =~ s/^msgstr\s+//) {
176 &parse_po_line($line);
179 elsif ($status eq "msgstr") {
180 if ($line =~ /^\s*"(.*)"\s*/) {
185 $rMessages->{$msgid}->{line} = $foundline;
186 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
187 $rMessages->{$msgid}->{msgstr} = $msgstr;
194 die("invalid status");
198 sub getLineSortedKeys($)
200 my ($rMessages) = @_;
202 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
205 sub printExtraMessages($$)
207 my ($type, $rExtra) = @_;
208 my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
210 if (@UntranslatedKeys > 0) {
211 print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
212 for my $l (@UntranslatedKeys) {
213 print "> line $l: \"" . $rExtra->{$l} . "\"\n";