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