]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Tool for comparing po-files. Interesting only for translators.
[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
27 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
28
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
34
35 if (@ARGV != 2) {
36   die("Expected exactly 2 parameters");
37 }
38
39 &check("original", $ARGV[0]);
40 &check("new", $ARGV[1]);
41
42 &parse_po_file($ARGV[0], \%Messages);
43 &parse_po_file($ARGV[1], \%newMessages);
44
45 my @MsgKeys = &getLineSortedKeys(\%newMessages);
46
47 for my $k (@MsgKeys) {
48   if ($newMessages{$k}->{msgstr} eq "") {
49     # this is still untranslated string
50     $Untranslated{$newMessages{$k}->{line}} = $k;
51   }
52   elsif ($newMessages{$k}->{fuzzy}) {
53     #fuzzy string
54     $Fuzzy{$newMessages{$k}->{line}} = $k;
55   }
56   if (exists($Messages{$k})) {
57     &printIfDiff($k, $Messages{$k}, $newMessages{$k});
58     delete($Messages{$k});
59     delete($newMessages{$k});
60   }
61 }
62
63 @MsgKeys = &getLineSortedKeys(\%Messages);
64 for my $k (@MsgKeys) {
65   $result |= 8;
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";
71 }
72
73 @MsgKeys = &getLineSortedKeys(\%newMessages);
74 for my $k (@MsgKeys) {
75   $result |= 16;
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";
81 }
82
83 &printExtraMessages("fuzzy", \%Fuzzy);
84 &printExtraMessages("untranslated", \%Untranslated);
85
86 exit($result);
87
88 sub check($$)
89 {
90   my ($spec, $filename) = @_;
91
92   if (! -e $filename ) {
93     die("$spec po file does not exist");
94   }
95   if ( ! -f $filename ) {
96     die("$spec po file is not regular");
97   }
98   if ( ! -r $filename ) {
99     die("$spec po file is not readable");
100   }
101 }
102
103 sub printDiff($$$$)
104 {
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";
110   }
111   else {
112     print "< fuzzy = " . $rM->{fuzzy} . "\n";
113   }
114   print "< msgstr = " . $rM->{msgstr} . "\n";
115   if ($k ne $nk) {
116     print "> msgid = \"$nk\"\n";
117   }
118   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
119     print "> fuzzy = " . $rnM->{fuzzy} . "\n";
120   }
121   print "> msgstr = " . $rnM->{msgstr} . "\n";
122   print "\n";
123 }
124
125 sub printIfDiff($$$)
126 {
127   my ($k, $rM, $rnM) = @_;
128   my $doprint = 0;
129   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
130   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
131   if ($doprint) {
132     $result |= 4;
133     &printDiff($k, $k, $rM, $rnM);
134   }
135 }
136
137 sub parse_po_file($$)
138 {
139   my ($file, $rMessages) = @_;
140   if (open(FI, '<', $file)) {
141     $status = "normal";
142     $fuzzy = 0;
143     my $lineno = 0;
144     while (my $line = <FI>) {
145       $lineno++;
146       &parse_po_line($line, $lineno, $rMessages);
147     }
148     &parse_po_line("", $lineno + 1, $rMessages);
149     close(FI);
150   }
151 }
152
153 sub parse_po_line($$$)
154 {
155   my ($line, $lineno, $rMessages) = @_;
156   chomp($line);
157
158   if ($status eq "normal") {
159     if ($line =~ /^#, fuzzy/) {
160       $fuzzy = 1;
161     }
162     elsif ($line =~ s/^msgid\s+//) {
163       $foundline = $lineno;
164       $status = "msgid";
165       $msgid = "";
166       &parse_po_line($line);
167     }
168   }
169   elsif ($status eq "msgid") {
170     if ($line =~ /^\s*"(.*)"\s*/) {
171       $msgid .= $1;
172     }
173     elsif ($line =~ s/^msgstr\s+//) {
174       $status = "msgstr";
175       $msgstr = "";
176       &parse_po_line($line);
177     }
178   }
179   elsif ($status eq "msgstr") {
180     if ($line =~ /^\s*"(.*)"\s*/) {
181       $msgstr .= $1;
182     }
183     else {
184       if ($msgid ne "") {
185         $rMessages->{$msgid}->{line} = $foundline;
186         $rMessages->{$msgid}->{fuzzy} = $fuzzy;
187         $rMessages->{$msgid}->{msgstr} = $msgstr;
188       }
189       $fuzzy = 0;
190       $status = "normal";
191     }
192   }
193   else {
194     die("invalid status");
195   }
196 }
197
198 sub getLineSortedKeys($)
199 {
200   my ($rMessages) = @_;
201
202   return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
203 }
204
205 sub printExtraMessages($$)
206 {
207   my ($type, $rExtra) = @_;
208   my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
209
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"; 
214     }
215   }
216 }