| 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 |  | 
|---|
| 15 | my $pot = Locale::PO->load_file_asarray("survex.pot"); | 
|---|
| 16 | for my $po_file (@ARGV) { | 
|---|
| 17 |   my $po = Locale::PO->load_file_ashash($po_file); | 
|---|
| 18 |   if (!defined $po) { | 
|---|
| 19 |     print STDERR "$po_file: Bad!\n"; | 
|---|
| 20 |     next; | 
|---|
| 21 |   } | 
|---|
| 22 |  | 
|---|
| 23 |   # Build a map from msgno to po msgid, so we can find the old translation when | 
|---|
| 24 |   # a msgid has since changed in the pot file. | 
|---|
| 25 |   my %msgno_to_po; | 
|---|
| 26 |   foreach my $msgid (keys %{$po}) { | 
|---|
| 27 |     my $ref = $po->{$msgid}->reference; | 
|---|
| 28 |     if (defined $ref) { | 
|---|
| 29 |       my ($msgno) = $ref =~ /\bn:(\d+)/; | 
|---|
| 30 |       if (defined $msgno) { | 
|---|
| 31 |         $msgno_to_po{$msgno} = [ $msgid, $po->{$msgid}->msgstr ]; | 
|---|
| 32 |       } | 
|---|
| 33 |     } | 
|---|
| 34 |   } | 
|---|
| 35 |  | 
|---|
| 36 |   my @fuzzy; | 
|---|
| 37 |   open NEW, '>', "$po_file.tmp" or die $!; | 
|---|
| 38 |   foreach my $pot_entry (@{$pot}) { | 
|---|
| 39 |     my $msgid = $pot_entry->msgid; | 
|---|
| 40 |     my $msgstr = '""'; | 
|---|
| 41 |     my $po_entry; | 
|---|
| 42 |     if (exists $$po{$msgid}) { | 
|---|
| 43 |         $po_entry = $$po{$msgid}; | 
|---|
| 44 |         $msgstr = $po_entry->msgstr; | 
|---|
| 45 |         delete $$po{$msgid}; | 
|---|
| 46 |         if ($msgid eq '""') { | 
|---|
| 47 |             $msgstr =~ s/\\n(..)/\\n"\n"$1/g; | 
|---|
| 48 |             print NEW "msgid $msgid\nmsgstr \"\"\n$msgstr\n"; | 
|---|
| 49 |             next; | 
|---|
| 50 |         } | 
|---|
| 51 |     } else { | 
|---|
| 52 |         $po_entry = $pot_entry; | 
|---|
| 53 |     } | 
|---|
| 54 |     if (defined $pot_entry->automatic) { | 
|---|
| 55 |         my $automatic = "\n" . $pot_entry->automatic; | 
|---|
| 56 |         $automatic =~ s/\n/\n#. /g; | 
|---|
| 57 |         while ($automatic =~ s/\n#. \n/\n#.\n/g) { } | 
|---|
| 58 |         print NEW $automatic; | 
|---|
| 59 |     } | 
|---|
| 60 |     my $fuzzy = $po_entry->fuzzy; | 
|---|
| 61 |     my $ref = $pot_entry->reference; | 
|---|
| 62 |     if (defined $ref) { | 
|---|
| 63 |         if ($msgstr eq '""') { | 
|---|
| 64 |             my ($msgno) = $ref =~ /\bn:(\d+)/; | 
|---|
| 65 |             if (exists $msgno_to_po{$msgno}) { | 
|---|
| 66 |                 my $old_msgid; | 
|---|
| 67 |                 ($old_msgid, $msgstr) = @{$msgno_to_po{$msgno}}; | 
|---|
| 68 |                 if ($msgstr ne '""') { | 
|---|
| 69 |                     $fuzzy = 1; | 
|---|
| 70 |                     print "$po_file: Fuzzifying translation $old_msgid / $msgid -> $msgstr\n"; | 
|---|
| 71 |                     push @fuzzy, $old_msgid; | 
|---|
| 72 |                 } | 
|---|
| 73 |             } | 
|---|
| 74 |         } | 
|---|
| 75 |         $ref = "\n" . $ref; | 
|---|
| 76 |         $ref =~ s/\n/\n#: /mg; | 
|---|
| 77 |         print NEW $ref; | 
|---|
| 78 |     } | 
|---|
| 79 |     my $c_format = $pot_entry->c_format; | 
|---|
| 80 |     if ($fuzzy || $c_format) { | 
|---|
| 81 |         print NEW "\n#"; | 
|---|
| 82 |         print NEW ", fuzzy" if $fuzzy; | 
|---|
| 83 |         print NEW ", c-format" if $c_format; | 
|---|
| 84 |     } | 
|---|
| 85 |     print NEW "\n"; | 
|---|
| 86 |     print NEW "#~ " if $pot_entry->obsolete; | 
|---|
| 87 |     print NEW "msgid $msgid\n"; | 
|---|
| 88 |     print NEW "#~ " if $pot_entry->obsolete; | 
|---|
| 89 |     print NEW "msgstr $msgstr\n"; | 
|---|
| 90 |   } | 
|---|
| 91 |   close NEW or die $!; | 
|---|
| 92 |   unlink "$po_file.old"; | 
|---|
| 93 |   delete @$po{@fuzzy}; | 
|---|
| 94 |   for (sort keys %{$po}) { | 
|---|
| 95 |     my $msgstr = $$po{$_}->msgstr; | 
|---|
| 96 |     if ($msgstr ne '""') { | 
|---|
| 97 |       print "$po_file: Dropping [$_] -> [$msgstr]\n"; | 
|---|
| 98 |     } | 
|---|
| 99 |   } | 
|---|
| 100 |   link $po_file, "$po_file.old" or die $!; | 
|---|
| 101 |   rename "$po_file.tmp", $po_file or die $!; | 
|---|
| 102 | } | 
|---|