[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 | |
---|
| 12 | my (%msgs, @uses); |
---|
| 13 | while (<ARGV>) { |
---|
| 14 | while (m!/\*(.*?)\*/(\d+)\b!g) { |
---|
| 15 | my ($msg, $msgno) = ($1, $2); |
---|
| 16 | if (exists $msgs{$msgno}) { |
---|
| 17 | if ($msgs{$msgno} ne $msg) { |
---|
| 18 | print STDERR "Mismatch for message number $msgno:\n"; |
---|
| 19 | print STDERR "$msgs{$msgno}\n$msg\n"; |
---|
| 20 | } |
---|
| 21 | } else { |
---|
| 22 | $msgs{$msgno} = $msg; |
---|
| 23 | } |
---|
| 24 | push @{$uses[$msgno]}, "$ARGV:$."; |
---|
| 25 | } |
---|
| 26 | } continue { |
---|
| 27 | # Reset $. for each input file. |
---|
| 28 | close ARGV if eof; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | print << "END"; |
---|
| 32 | # Survex translation template. |
---|
| 33 | # Copyright (C) YEAR COPYRIGHT HOLDERS |
---|
| 34 | # This file is distributed under the same licence as Survex. |
---|
| 35 | # |
---|
| 36 | msgid "" |
---|
| 37 | msgstr "" |
---|
[f8fa253] | 38 | "Project-Id-Version: survex\\n" |
---|
[f63bd56] | 39 | "Report-Msgid-Bugs-To: olly\@survex.com\\n" |
---|
| 40 | "POT-Creation-Date: $pot_creation_date\\n" |
---|
| 41 | "PO-Revision-Date: YEAR-MO-DA HO:MI:SE +ZONE\\n" |
---|
| 42 | "Language-Team: LANGUAGE <LL\@li.org>\\n" |
---|
| 43 | "MIME-Version: 1.0\\n" |
---|
| 44 | "Content-Type: text/plain; charset=utf-8\\n" |
---|
| 45 | "Content-Transfer-Encoding: 8bit\\n" |
---|
| 46 | END |
---|
| 47 | |
---|
[0dc5829] | 48 | my $num_list = Locale::PO->load_file_asarray("survex.pot"); |
---|
[f63bd56] | 49 | my $first = 1; |
---|
| 50 | foreach my $po_entry (@{$num_list}) { |
---|
[0dc5829] | 51 | my $msgno = ''; |
---|
| 52 | my $ref = $po_entry->reference; |
---|
| 53 | if (defined $ref && $ref =~ /^n:(\d+)$/m) { |
---|
| 54 | $msgno = $1; |
---|
| 55 | } |
---|
[f63bd56] | 56 | if ($first) { |
---|
| 57 | $first = 0; |
---|
| 58 | next if ($po_entry->msgid eq '""'); |
---|
| 59 | } |
---|
| 60 | my $msg; |
---|
| 61 | if (exists $msgs{$msgno}) { |
---|
| 62 | $msg = $msgs{$msgno}; |
---|
| 63 | delete $msgs{$msgno}; |
---|
| 64 | } else { |
---|
[0dc5829] | 65 | print STDERR "Message number $msgno is in survex.pot but not found in source - preserving\n" unless $po_entry->obsolete; |
---|
[f63bd56] | 66 | $msg = $po_entry->dequote($po_entry->msgid); |
---|
| 67 | } |
---|
| 68 | if (defined $po_entry->automatic) { |
---|
| 69 | my $automatic = "\n" . $po_entry->automatic; |
---|
| 70 | $automatic =~ s/\n/\n#. /g; |
---|
| 71 | while ($automatic =~ s/\n#. \n/\n#.\n/g) { } |
---|
| 72 | print $automatic; |
---|
| 73 | } |
---|
| 74 | if ($msgno =~ /^\d+$/) { |
---|
| 75 | for (@{$uses[$msgno]}) { |
---|
| 76 | print "\n#: ", $_; |
---|
| 77 | } |
---|
[0dc5829] | 78 | print "\n#: n:$msgno"; |
---|
[f63bd56] | 79 | } |
---|
| 80 | print "\n#, c-format" if $msg =~ /\%[a-z0-9]/; |
---|
| 81 | if ($msg =~ s/(?:^|[^\\])"/\\"/g) { |
---|
| 82 | print STDERR "Escaping unescaped \" in message number $msgno\n"; |
---|
| 83 | } |
---|
| 84 | print "\n"; |
---|
| 85 | print "#~ " if $po_entry->obsolete; |
---|
| 86 | print "msgid \"$msg\"\n"; |
---|
| 87 | print "#~ " if $po_entry->obsolete; |
---|
[0dc5829] | 88 | print "msgstr \"\"\n"; |
---|
[f63bd56] | 89 | } |
---|
| 90 | |
---|
| 91 | for my $msgno (sort keys %msgs) { |
---|
| 92 | next if ($msgno == 0 || $msgno >= 1000); |
---|
| 93 | print STDERR "New message number $msgno\n"; |
---|
| 94 | for (@{$uses[$msgno]}) { |
---|
| 95 | print "\n#: ", $_; |
---|
| 96 | } |
---|
| 97 | my $msg = $msgs{$msgno}; |
---|
[0dc5829] | 98 | print "\n#: n:$msgno"; |
---|
[f63bd56] | 99 | print "\n#, c-format" if $msg =~ /\%[a-z0-9]/; |
---|
| 100 | if ($msg =~ s/(?:^|[^\\])"/\\"/g) { |
---|
| 101 | print STDERR "Escaping unescaped \" in message number $msgno\n"; |
---|
| 102 | } |
---|
| 103 | print "\nmsgid \"$msg\"\n"; |
---|
[0dc5829] | 104 | print "msgstr \"\"\n"; |
---|
[f63bd56] | 105 | } |
---|