| 1 | #!/usr/bin/perl -w |
|---|
| 2 | require 5.008; |
|---|
| 3 | use bytes; |
|---|
| 4 | use strict; |
|---|
| 5 | use POSIX; |
|---|
| 6 | use Locale::PO; |
|---|
| 7 | |
|---|
| 8 | my $pot_creation_date = strftime "%Y-%m-%d %H:%M:%S +0000", gmtime(); |
|---|
| 9 | |
|---|
| 10 | use integer; |
|---|
| 11 | |
|---|
| 12 | my (%msgs, @uses, %comment); |
|---|
| 13 | my $translator_comment; |
|---|
| 14 | while (<ARGV>) { |
|---|
| 15 | if (m!(/[/*])\s*(TRANSLATORS:.*?)\s*\z!) { |
|---|
| 16 | my ($comment_type, $comment) = ($1, $2); |
|---|
| 17 | if ($comment_type eq '/*') { |
|---|
| 18 | while ($comment !~ s!\s*\*/\z!! && defined($_ = <ARGV>)) { |
|---|
| 19 | if (m!^\s*\*?\s*(.*?)\s*\z!) { |
|---|
| 20 | # */ on a line by itself results in '/' for $1. |
|---|
| 21 | last if $1 eq '/'; |
|---|
| 22 | $comment .= "\n$1"; |
|---|
| 23 | } |
|---|
| 24 | } |
|---|
| 25 | $comment =~ s/\n+$//; |
|---|
| 26 | } |
|---|
| 27 | if (defined $translator_comment) { |
|---|
| 28 | print STDERR "Ignored TRANSLATORS comment: $translator_comment\n"; |
|---|
| 29 | } |
|---|
| 30 | $translator_comment = $comment; |
|---|
| 31 | last if !defined $_; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | while (m!/\*(.*?)\*/(\d+)\b!g) { |
|---|
| 35 | my ($msg, $msgno) = ($1, $2); |
|---|
| 36 | # Handle there being a comment before the comment with the message in. |
|---|
| 37 | $msg =~ s!.*/\*!!; |
|---|
| 38 | if (exists $msgs{$msgno}) { |
|---|
| 39 | if ($msgs{$msgno} ne $msg) { |
|---|
| 40 | print STDERR "Mismatch for message number $msgno:\n"; |
|---|
| 41 | print STDERR "$msgs{$msgno}\n$msg\n"; |
|---|
| 42 | } |
|---|
| 43 | } else { |
|---|
| 44 | $msgs{$msgno} = $msg; |
|---|
| 45 | } |
|---|
| 46 | if (defined $translator_comment) { |
|---|
| 47 | if (exists $comment{$msgno} && $comment{$msgno} ne $translator_comment) { |
|---|
| 48 | print STDERR "Different TRANSLATOR comemnts for message #$msgno\n"; |
|---|
| 49 | } |
|---|
| 50 | $comment{$msgno} = $translator_comment; |
|---|
| 51 | undef $translator_comment; |
|---|
| 52 | } |
|---|
| 53 | push @{$uses[$msgno]}, "$ARGV:$."; |
|---|
| 54 | } |
|---|
| 55 | } continue { |
|---|
| 56 | # Reset $. for each input file. |
|---|
| 57 | close ARGV if eof; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | print << "END"; |
|---|
| 61 | # Survex translation template. |
|---|
| 62 | # Copyright (C) YEAR COPYRIGHT HOLDERS |
|---|
| 63 | # This file is distributed under the same licence as Survex. |
|---|
| 64 | # |
|---|
| 65 | msgid "" |
|---|
| 66 | msgstr "" |
|---|
| 67 | "Project-Id-Version: survex\\n" |
|---|
| 68 | "Report-Msgid-Bugs-To: olly\@survex.com\\n" |
|---|
| 69 | "POT-Creation-Date: $pot_creation_date\\n" |
|---|
| 70 | "PO-Revision-Date: YEAR-MO-DA HO:MI:SE +ZONE\\n" |
|---|
| 71 | "Language-Team: LANGUAGE <LL\@li.org>\\n" |
|---|
| 72 | "MIME-Version: 1.0\\n" |
|---|
| 73 | "Content-Type: text/plain; charset=utf-8\\n" |
|---|
| 74 | "Content-Transfer-Encoding: 8bit\\n" |
|---|
| 75 | END |
|---|
| 76 | |
|---|
| 77 | my $num_list = Locale::PO->load_file_asarray("survex.pot"); |
|---|
| 78 | my $first = 1; |
|---|
| 79 | foreach my $po_entry (@{$num_list}) { |
|---|
| 80 | my $msgno = ''; |
|---|
| 81 | my $ref = $po_entry->reference; |
|---|
| 82 | if (defined $ref && $ref =~ /^n:(\d+)$/m) { |
|---|
| 83 | $msgno = $1; |
|---|
| 84 | } |
|---|
| 85 | if ($first) { |
|---|
| 86 | $first = 0; |
|---|
| 87 | next if ($po_entry->msgid eq '""'); |
|---|
| 88 | } |
|---|
| 89 | my $msg; |
|---|
| 90 | if (exists $msgs{$msgno}) { |
|---|
| 91 | $msg = $msgs{$msgno}; |
|---|
| 92 | delete $msgs{$msgno}; |
|---|
| 93 | } else { |
|---|
| 94 | print STDERR "Message number $msgno is in survex.pot but not found in source - preserving\n" unless $po_entry->obsolete; |
|---|
| 95 | $msg = $po_entry->dequote($po_entry->msgid); |
|---|
| 96 | } |
|---|
| 97 | if (exists $comment{$msgno}) { |
|---|
| 98 | my $new = $comment{$msgno}; |
|---|
| 99 | my $old = $po_entry->automatic; |
|---|
| 100 | $po_entry->automatic($new); |
|---|
| 101 | if (defined $old) { |
|---|
| 102 | $old =~ s/\s+/ /g; |
|---|
| 103 | $new =~ s/\s+/ /g; |
|---|
| 104 | if ($old ne $new) { |
|---|
| 105 | print STDERR "Comment for message #$msgno changed from\n[$old]\nto\n[$new]\n"; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | if (defined $po_entry->automatic) { |
|---|
| 110 | if (!exists $comment{$msgno}) { |
|---|
| 111 | my $fake_err = ": Comment for message #$msgno not in source code\n"; |
|---|
| 112 | if ($msgno ne '' && exists($uses[$msgno])) { |
|---|
| 113 | print STDERR join($fake_err, @{$uses[$msgno]}), $fake_err if exists($uses[$msgno]); |
|---|
| 114 | } else { |
|---|
| 115 | print STDERR $fake_err; |
|---|
| 116 | } |
|---|
| 117 | my $x = $po_entry->automatic; |
|---|
| 118 | $x =~ s/\n/\n * /g; |
|---|
| 119 | print STDERR " /* $x */\n"; |
|---|
| 120 | } |
|---|
| 121 | my $automatic = "\n" . $po_entry->automatic; |
|---|
| 122 | $automatic =~ s/\n/\n#. /g; |
|---|
| 123 | while ($automatic =~ s/\n#. \n/\n#.\n/g) { } |
|---|
| 124 | print $automatic; |
|---|
| 125 | } |
|---|
| 126 | if ($msgno =~ /^\d+$/) { |
|---|
| 127 | for (@{$uses[$msgno]}) { |
|---|
| 128 | print "\n#: ", $_; |
|---|
| 129 | } |
|---|
| 130 | print "\n#: n:$msgno"; |
|---|
| 131 | } |
|---|
| 132 | print "\n#, c-format" if $msg =~ /\%[a-z0-9]/; |
|---|
| 133 | if ($msg =~ s/(?:^|[^\\])"/\\"/g) { |
|---|
| 134 | print STDERR "Escaping unescaped \" in message number $msgno\n"; |
|---|
| 135 | } |
|---|
| 136 | print "\n"; |
|---|
| 137 | print "#~ " if $po_entry->obsolete; |
|---|
| 138 | print "msgid \"$msg\"\n"; |
|---|
| 139 | print "#~ " if $po_entry->obsolete; |
|---|
| 140 | print "msgstr \"\"\n"; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | for my $msgno (sort keys %msgs) { |
|---|
| 144 | next if ($msgno == 0 || $msgno >= 1000); |
|---|
| 145 | print STDERR "New message number $msgno\n"; |
|---|
| 146 | for (@{$uses[$msgno]}) { |
|---|
| 147 | print "\n#: ", $_; |
|---|
| 148 | } |
|---|
| 149 | my $msg = $msgs{$msgno}; |
|---|
| 150 | print "\n#: n:$msgno"; |
|---|
| 151 | print "\n#, c-format" if $msg =~ /\%[a-z0-9]/; |
|---|
| 152 | if ($msg =~ s/(?:^|[^\\])"/\\"/g) { |
|---|
| 153 | print STDERR "Escaping unescaped \" in message number $msgno\n"; |
|---|
| 154 | } |
|---|
| 155 | print "\nmsgid \"$msg\"\n"; |
|---|
| 156 | print "msgstr \"\"\n"; |
|---|
| 157 | } |
|---|