]> pere.pagekite.me Git - homepage.git/blob - linux/auto_translate
Generated.
[homepage.git] / linux / auto_translate
1 #!/usr/bin/perl -w
2 #
3 # Author: Petter Reinholdtsen <pere@td.org.uit.no>
4 # Date: 2001-08-05
5 # Source: http://www.student.uit.no/~pere/linux/
6 #
7 # Read available translations from .po files, and allow translations
8 # already done in one file to be inserted into all the other places
9 # where the original text occurs.
10 #
11 # Usage: auto_translate <po-files>
12 #
13 # Changes:
14 # 2001-08-05 v0.2
15 # Fixed bug. Unable to handle multiline 'msgid'.
16 # 2001-08-05 v0.3
17 # Add 'fuzzy' tag when inserting text.
18 # 2001-08-05 v0.4
19 # Only add 'fuzzy' tag in the changed files.
20
21 #use strict;
22 use Getopt::Std;
23
24 my ($pofile, %translation, @problem, %msgstrs, %files);
25 my $showfile = 1;
26
27 #########################################################################
28
29 sub add_translation {
30 my ($pofile, $msgid, $msgstr) = @_;
31 # print " Translation: $msgid = $msgstr\n";
32 $msgstr = $1;
33 $msgstrs{"$msgid\t$msgstr"}++;
34 push(@{$files{"$msgid\t$msgstr"}}, $pofile);
35 if ( !defined($translation{$msgid}) ) {
36 push(@{$translation{$msgid}}, $msgstr);
37 } else {
38 if ( ! in_array($msgstr, $translation{$msgid}) ) {
39 push(@{$translation{$msgid}}, $msgstr);
40 push(@problem, $msgid) unless grep($msgid, @problem);
41 }
42 }
43 $msgid = "";
44 }
45
46 sub in_array ($$) {
47 my ($str, $aref) = @_;
48 my $test;
49 for $test (@$aref) {
50 return 1 if ($test eq $str);
51 }
52 return 0;
53 }
54
55 sub replace_translation {
56 my ($filename, $msgid, $msgstr) = @_;
57 my $count = 0;
58 open(POTMPFILE, ">$filename.new") || die "Unable to open $filename.new";
59
60 open(POFILE, "<$filename") || die "Unable to open $filename";
61 while (<POFILE>) {
62 chomp;
63 my @lines = ();
64 if (m/^msgid\s+\"(.*)\"\s*$/) {
65 my $thisid = $1;
66 my $thisstr = "";
67 push(@lines, $_);
68 while (<POFILE>) {
69 chomp;
70 push(@lines, $_);
71 if (m/^\s*\"(.+)\"\s*$/) {
72 print "Appending \'$thisid\' \n + \'$1\'\n" if ($debug);
73 $thisid .= $1;
74 }
75 if (m/^msgstr\s\"(.*)\"\s*$/) {
76 print "ID: $thisid\n" if ($debug);
77 $thisstr = $1;
78 while (<POFILE>) {
79 chomp;
80 push(@lines, $_);
81 last if (/^\s*$/);
82 if (m/^\s*\"(.+\")\s*$/) {
83 $thisstr .= $1;
84 }
85 }
86 print "Msg: $thisstr\n" if ($debug);
87 last;
88 }
89 }
90 if ("\"$thisid\"" eq $msgid &&
91 "\"$thisstr\"" ne $msgstr) {
92 print POTMPFILE "#, fuzzy\n";
93 print POTMPFILE "msgid $msgid\n";
94 print POTMPFILE "msgstr $msgstr\n";
95 print POTMPFILE "\n";
96 $count++;
97 } else {
98 print POTMPFILE join("\n", @lines), "\n";
99 }
100 } else {
101 print POTMPFILE "$_\n";
102 }
103 }
104 close(POFILE) || die "unable to close $filename";
105 close(POTMPFILE) || die "unable to close $filename.new";
106 rename "$filename.new", $filename;
107
108 if (0 == $count) {
109 print "Did not find anything to replace!\n";
110 }
111 }
112
113 sub choose_transl {
114 my ($msgid) = @_;
115 print "\n$msgid\n";
116 my $msgstr;
117 my $num = 0;
118 print " $num - skip this translation\n";
119 my @strings = sort { $msgstrs{"$msgid\t$b"} <=> $msgstrs{"$msgid\t$a"}; } @{$translation{$msgid}};
120 my %curfiles;
121 for $msgstr (@strings) {
122 $num++;
123 print " $num $msgstr # (" . $msgstrs{"$msgid\t$msgstr"}. ") " or die;
124 print "[" . join(",", @{$files{"$msgid\t$msgstr"}}) . "]" or die if $showfile;
125 print "\n" or die;
126 my $file;
127 for $file (@{$files{"$msgid\t$msgstr"}}) {
128 $curfiles{$file}++;
129 }
130 }
131 if ($num != 2)
132 {
133 print "** To many choices. **\n";
134 return;
135 }
136
137 print "Choice: ";
138 $choice = <STDIN>;
139
140 return if (0 == $choice);
141 $msgstr = $strings[$choice-1];
142 print "\n Translating $msgid as $msgstr in\n";
143 for $file (keys %curfiles) {
144 print " $file\n";
145 replace_translation($file, $msgid, $msgstr);
146 }
147 }
148
149 # Remove leading space and '&' before comparing
150 sub kde_magic_sort {
151 my $aa = $a;
152 my $bb = $b;
153
154 $aa =~ s/\&|^\s+//;
155 $bb =~ s/\&|^\s+//;
156 return lc($aa) cmp lc($bb);
157 }
158
159 sub translate {
160 my ($msgid);
161 my ($count, $icount) = (0,0);
162 for $msgid (sort kde_magic_sort keys %translation ) {
163 $count++;
164 if ( 1 < scalar(@{$translation{$msgid}}) ) {
165 $icount++;
166 choose_transl($msgid);
167 }
168 }
169 }
170
171 sub read_files
172 {
173 my @argv = @_;
174
175 for $pofile ( @argv ) {
176 open(POFILE, "<$pofile") || next;
177
178 my ($next, $msgid, $msgstr);
179
180 while (1) {
181 $_ = $next || <POFILE>;
182 last unless $_;
183 $next = "";
184
185 chomp;
186
187 s/^\#.+$//; # Remove comments
188
189 next if (/^\s*$/);
190
191 if (/^msgid (\".+)/) {
192 $msgid = $1;
193 while (<POFILE>) {
194 chomp;
195 unless (/^\"/) {
196 $next = $_;
197 last;
198 }
199 $msgid .= $_;
200 $msgid =~ s/\"\"//;
201 }
202
203 $msgstr = "";
204 next;
205 }
206
207 if (/^msgstr (\".+)/) {
208 $msgstr = $1;
209 while (<POFILE>) {
210 chomp;
211 unless (/^\"/) {
212 $next = $_;
213 last;
214 }
215 $msgstr .= $_;
216 $msgstr =~ s/\"\"//;
217 }
218
219 add_translation($pofile, $msgid, $msgstr);
220
221 $msgid = "";
222 $msgstr = "";
223 }
224
225 }
226 close(POFILE);
227 }
228 }
229 #########################################################################
230
231 getopt('f');
232
233 if (defined $opt_f) {
234 $showfile = 1;
235 print "Showing filenames\n";
236 }
237
238 print "Reading .po files...\n";
239 read_files(@ARGV);
240
241 print "Ready to autotranslate\n";
242 translate();
243