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