| [df126137] | 1 | #!/usr/bin/perl -w | 
|---|
 | 2 | require 5.008; | 
|---|
 | 3 | use bytes; | 
|---|
 | 4 | use strict; | 
|---|
 | 5 | use POSIX; | 
|---|
 | 6 | use Locale::PO; | 
|---|
 | 7 |  | 
|---|
 | 8 | use integer; | 
|---|
 | 9 |  | 
|---|
 | 10 | if (@ARGV == 0 || $ARGV[0] eq '--help') { | 
|---|
 | 11 |   print STDERR "Syntax: $0 PO_FILE...\n"; | 
|---|
 | 12 |   exit (@ARGV != 0); | 
|---|
 | 13 | } | 
|---|
 | 14 |  | 
|---|
| [ffe31e2] | 15 | my %dropped; | 
|---|
 | 16 |  | 
|---|
| [df126137] | 17 | my $pot = Locale::PO->load_file_asarray("survex.pot"); | 
|---|
 | 18 | for my $po_file (@ARGV) { | 
|---|
| [ffe31e2] | 19 |   my ($lang) = ($po_file =~ m!([^/]+)\.po$!); | 
|---|
| [df126137] | 20 |   my $po = Locale::PO->load_file_ashash($po_file); | 
|---|
 | 21 |   if (!defined $po) { | 
|---|
 | 22 |     print STDERR "$po_file: Bad!\n"; | 
|---|
 | 23 |     next; | 
|---|
 | 24 |   } | 
|---|
| [f54af4f] | 25 |  | 
|---|
 | 26 |   # Build a map from msgno to po msgid, so we can find the old translation when | 
|---|
 | 27 |   # a msgid has since changed in the pot file. | 
|---|
 | 28 |   my %msgno_to_po; | 
|---|
 | 29 |   foreach my $msgid (keys %{$po}) { | 
|---|
 | 30 |     my $ref = $po->{$msgid}->reference; | 
|---|
 | 31 |     if (defined $ref) { | 
|---|
 | 32 |       my ($msgno) = $ref =~ /\bn:(\d+)/; | 
|---|
 | 33 |       if (defined $msgno) { | 
|---|
 | 34 |         $msgno_to_po{$msgno} = [ $msgid, $po->{$msgid}->msgstr ]; | 
|---|
 | 35 |       } | 
|---|
 | 36 |     } | 
|---|
 | 37 |   } | 
|---|
 | 38 |  | 
|---|
 | 39 |   my @fuzzy; | 
|---|
| [df126137] | 40 |   open NEW, '>', "$po_file.tmp" or die $!; | 
|---|
 | 41 |   foreach my $pot_entry (@{$pot}) { | 
|---|
 | 42 |     my $msgid = $pot_entry->msgid; | 
|---|
 | 43 |     my $msgstr = '""'; | 
|---|
 | 44 |     my $po_entry; | 
|---|
 | 45 |     if (exists $$po{$msgid}) { | 
|---|
 | 46 |         $po_entry = $$po{$msgid}; | 
|---|
 | 47 |         $msgstr = $po_entry->msgstr; | 
|---|
 | 48 |         delete $$po{$msgid}; | 
|---|
 | 49 |         if ($msgid eq '""') { | 
|---|
 | 50 |             $msgstr =~ s/\\n(..)/\\n"\n"$1/g; | 
|---|
 | 51 |             print NEW "msgid $msgid\nmsgstr \"\"\n$msgstr\n"; | 
|---|
 | 52 |             next; | 
|---|
 | 53 |         } | 
|---|
 | 54 |     } else { | 
|---|
 | 55 |         $po_entry = $pot_entry; | 
|---|
 | 56 |     } | 
|---|
 | 57 |     if (defined $pot_entry->automatic) { | 
|---|
 | 58 |         my $automatic = "\n" . $pot_entry->automatic; | 
|---|
 | 59 |         $automatic =~ s/\n/\n#. /g; | 
|---|
 | 60 |         while ($automatic =~ s/\n#. \n/\n#.\n/g) { } | 
|---|
 | 61 |         print NEW $automatic; | 
|---|
 | 62 |     } | 
|---|
| [f54af4f] | 63 |     my $fuzzy = $po_entry->fuzzy; | 
|---|
| [df126137] | 64 |     my $ref = $pot_entry->reference; | 
|---|
 | 65 |     if (defined $ref) { | 
|---|
| [f54af4f] | 66 |         if ($msgstr eq '""') { | 
|---|
 | 67 |             my ($msgno) = $ref =~ /\bn:(\d+)/; | 
|---|
 | 68 |             if (exists $msgno_to_po{$msgno}) { | 
|---|
 | 69 |                 my $old_msgid; | 
|---|
 | 70 |                 ($old_msgid, $msgstr) = @{$msgno_to_po{$msgno}}; | 
|---|
 | 71 |                 if ($msgstr ne '""') { | 
|---|
 | 72 |                     $fuzzy = 1; | 
|---|
 | 73 |                     print "$po_file: Fuzzifying translation $old_msgid / $msgid -> $msgstr\n"; | 
|---|
 | 74 |                     push @fuzzy, $old_msgid; | 
|---|
 | 75 |                 } | 
|---|
 | 76 |             } | 
|---|
 | 77 |         } | 
|---|
| [df126137] | 78 |         $ref = "\n" . $ref; | 
|---|
 | 79 |         $ref =~ s/\n/\n#: /mg; | 
|---|
 | 80 |         print NEW $ref; | 
|---|
 | 81 |     } | 
|---|
 | 82 |     my $c_format = $pot_entry->c_format; | 
|---|
 | 83 |     if ($fuzzy || $c_format) { | 
|---|
 | 84 |         print NEW "\n#"; | 
|---|
 | 85 |         print NEW ", fuzzy" if $fuzzy; | 
|---|
 | 86 |         print NEW ", c-format" if $c_format; | 
|---|
 | 87 |     } | 
|---|
 | 88 |     print NEW "\n"; | 
|---|
 | 89 |     print NEW "#~ " if $pot_entry->obsolete; | 
|---|
 | 90 |     print NEW "msgid $msgid\n"; | 
|---|
 | 91 |     print NEW "#~ " if $pot_entry->obsolete; | 
|---|
 | 92 |     print NEW "msgstr $msgstr\n"; | 
|---|
 | 93 |   } | 
|---|
 | 94 |   close NEW or die $!; | 
|---|
 | 95 |   unlink "$po_file.old"; | 
|---|
| [f54af4f] | 96 |   delete @$po{@fuzzy}; | 
|---|
| [df126137] | 97 |   for (sort keys %{$po}) { | 
|---|
 | 98 |     my $msgstr = $$po{$_}->msgstr; | 
|---|
 | 99 |     if ($msgstr ne '""') { | 
|---|
 | 100 |       print "$po_file: Dropping [$_] -> [$msgstr]\n"; | 
|---|
| [ffe31e2] | 101 |       $dropped{$_}->{$lang} = $msgstr; | 
|---|
| [df126137] | 102 |     } | 
|---|
 | 103 |   } | 
|---|
 | 104 |   link $po_file, "$po_file.old" or die $!; | 
|---|
 | 105 |   rename "$po_file.tmp", $po_file or die $!; | 
|---|
 | 106 | } | 
|---|
| [ffe31e2] | 107 |  | 
|---|
 | 108 | if (%dropped) { | 
|---|
 | 109 |     print "Saving dropped messages to deadmessages.txt\n"; | 
|---|
 | 110 |     open DEAD, '>>deadmessages.txt' or die "Failed to open deadmessages.txt\n"; | 
|---|
 | 111 |     for (sort keys %dropped) { | 
|---|
 | 112 |         print DEAD "\nmsgid \"$_\"\n"; | 
|---|
 | 113 |         my $h = $dropped{$_}; | 
|---|
 | 114 |         for my $lang (sort keys %$h) { | 
|---|
 | 115 |             print DEAD "$lang $$h{$lang}\n"; | 
|---|
 | 116 |         } | 
|---|
 | 117 |     } | 
|---|
 | 118 |     close DEAD; | 
|---|
 | 119 | } | 
|---|