]>
pere.pagekite.me Git - homepage.git/blob - linux/auto_translate
3 # Author: Petter Reinholdtsen <pere@td.org.uit.no>
5 # Source: http://www.student.uit.no/~pere/linux/
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.
11 # Usage: auto_translate <po-files>
15 # Fixed bug. Unable to handle multiline 'msgid'.
17 # Add 'fuzzy' tag when inserting text.
19 # Only add 'fuzzy' tag in the changed files.
24 my ($pofile, %translation, @problem, %msgstrs, %files);
27 #########################################################################
30 my ($pofile, $msgid, $msgstr) = @_;
31 # print " Translation: $msgid = $msgstr\n";
33 $msgstrs{"$msgid\t$msgstr"}++;
34 push(@{$files{"$msgid\t$msgstr"}}, $pofile);
35 if ( !defined($translation{$msgid}) ) {
36 push(@{$translation{$msgid}}, $msgstr);
38 if ( ! in_array
($msgstr, $translation{$msgid}) ) {
39 push(@{$translation{$msgid}}, $msgstr);
40 push(@problem, $msgid) unless grep($msgid, @problem);
47 my ($str, $aref) = @_;
50 return 1 if ($test eq $str);
55 sub replace_translation
{
56 my ($filename, $msgid, $msgstr) = @_;
58 open(POTMPFILE
, ">$filename.new") || die "Unable to open $filename.new";
60 open(POFILE
, "<$filename") || die "Unable to open $filename";
64 if (m/^msgid\s+\"(.*)\"\s*$/) {
71 if (m/^\s*\"(.+)\"\s*$/) {
72 print "Appending \'$thisid\' \n + \'$1\'\n" if ($debug);
75 if (m/^msgstr\s\"(.*)\"\s*$/) {
76 print "ID: $thisid\n" if ($debug);
82 if (m/^\s*\"(.+\")\s*$/) {
86 print "Msg: $thisstr\n" if ($debug);
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";
98 print POTMPFILE
join("\n", @lines), "\n";
101 print POTMPFILE
"$_\n";
104 close(POFILE
) || die "unable to close $filename";
105 close(POTMPFILE
) || die "unable to close $filename.new";
106 rename "$filename.new", $filename;
109 print "Did not find anything to replace!\n";
118 print " $num - skip this translation\n";
119 my @strings = sort { $msgstrs{"$msgid\t$b"} <=> $msgstrs{"$msgid\t$a"}; } @{$translation{$msgid}};
121 for $msgstr (@strings) {
123 print " $num $msgstr # (" . $msgstrs{"$msgid\t$msgstr"}. ") " or die;
124 print "[" . join(",", @{$files{"$msgid\t$msgstr"}}) . "]" or die if $showfile;
127 for $file (@{$files{"$msgid\t$msgstr"}}) {
133 print "** To many choices. **\n";
140 return if (0 == $choice);
141 $msgstr = $strings[$choice-1];
142 print "\n Translating $msgid as $msgstr in\n";
143 for $file (keys %curfiles) {
145 replace_translation
($file, $msgid, $msgstr);
149 # Remove leading space and '&' before comparing
156 return lc($aa) cmp lc($bb);
161 my ($count, $icount) = (0,0);
162 for $msgid (sort kde_magic_sort
keys %translation ) {
164 if ( 1 < scalar(@{$translation{$msgid}}) ) {
166 choose_transl
($msgid);
175 for $pofile ( @argv ) {
176 open(POFILE
, "<$pofile") || next;
178 my ($next, $msgid, $msgstr);
181 $_ = $next || <POFILE
>;
187 s/^\#.+$//; # Remove comments
191 if (/^msgid (\".+)/) {
207 if (/^msgstr (\".+)/) {
219 add_translation
($pofile, $msgid, $msgstr);
229 #########################################################################
233 if (defined $opt_f) {
235 print "Showing filenames\n";
238 print "Reading .po files...\n";
241 print "Ready to autotranslate\n";