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