10 if (@ARGV == 0 || $ARGV[0] eq '--help') {
11 print STDERR
"Syntax: $0 PO_FILE...\n";
17 my $pot = Locale
::PO
->load_file_asarray("survex.pot");
18 for my $po_file (@ARGV) {
19 my ($lang) = ($po_file =~ m!([^/]+)\.po$!);
20 my $po = Locale
::PO
->load_file_ashash($po_file);
22 print STDERR
"$po_file: Bad!\n";
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.
29 foreach my $msgid (keys %{$po}) {
30 my $ref = $po->{$msgid}->reference;
32 my ($msgno) = $ref =~ /\bn:(\d+)/;
34 $msgno_to_po{$msgno} = [ $msgid, $po->{$msgid}->msgstr ];
40 open NEW
, '>', "$po_file.tmp" or die $!;
41 foreach my $pot_entry (@
{$pot}) {
42 my $msgid = $pot_entry->msgid;
45 if (exists $$po{$msgid}) {
46 $po_entry = $$po{$msgid};
47 $msgstr = $po_entry->msgstr;
50 $msgstr =~ s/\\n(..)/\\n"\n"$1/g;
51 print NEW
"msgid $msgid\nmsgstr \"\"\n$msgstr\n";
55 $po_entry = $pot_entry;
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) { }
63 my $fuzzy = $po_entry->fuzzy;
64 my $ref = $pot_entry->reference;
66 if ($msgstr eq '""') {
67 my ($msgno) = $ref =~ /\bn:(\d+)/;
68 if (exists $msgno_to_po{$msgno}) {
70 ($old_msgid, $msgstr) = @
{$msgno_to_po{$msgno}};
71 if ($msgstr ne '""') {
73 print "$po_file: Fuzzifying translation $old_msgid / $msgid -> $msgstr\n";
74 push @fuzzy, $old_msgid;
79 $ref =~ s/\n/\n#: /mg;
82 my $c_format = $pot_entry->c_format;
83 if ($fuzzy || $c_format) {
85 print NEW
", fuzzy" if $fuzzy;
86 print NEW
", c-format" if $c_format;
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";
95 unlink "$po_file.old";
97 for (sort keys %{$po}) {
98 my $msgstr = $$po{$_}->msgstr;
99 if ($msgstr ne '""') {
100 print "$po_file: Dropping [$_] -> [$msgstr]\n";
101 $dropped{$_}->{$lang} = $msgstr;
104 link $po_file, "$po_file.old" or die $!;
105 rename "$po_file.tmp", $po_file or die $!;
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";