1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # gettexttomsg.pl |
---|
4 | # |
---|
5 | # Copyright (C) 2001,2002,2005,2011,2012,2014 Olly Betts |
---|
6 | # |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | |
---|
21 | require 5.008; |
---|
22 | use strict; |
---|
23 | |
---|
24 | my %revmsgs = (); |
---|
25 | |
---|
26 | my $msgno; |
---|
27 | open MSG, "../lib/survex.pot" or die $!; |
---|
28 | while (<MSG>) { |
---|
29 | if (/^#: n:(\d+)$/) { |
---|
30 | $msgno = $1; |
---|
31 | } elsif (defined $msgno && /^msgid\s*"(.*)"/) { |
---|
32 | $revmsgs{$1} = $msgno; |
---|
33 | $msgno = undef; |
---|
34 | } |
---|
35 | } |
---|
36 | close MSG; |
---|
37 | |
---|
38 | my $die = 0; |
---|
39 | |
---|
40 | my $suppress_argc_argv_unused_warnings = 0; |
---|
41 | while (<>) { |
---|
42 | if ($suppress_argc_argv_unused_warnings && /^{/) { |
---|
43 | $suppress_argc_argv_unused_warnings = 0; |
---|
44 | print "$_ (void)argc;\n (void)argv;\n"; |
---|
45 | next; |
---|
46 | } |
---|
47 | |
---|
48 | if (/^_getopt_initialize\b/) { |
---|
49 | $suppress_argc_argv_unused_warnings = 1; |
---|
50 | } elsif (!/^\s*#/) { |
---|
51 | while (/\\\n$/) { |
---|
52 | $_ .= <>; |
---|
53 | } |
---|
54 | # very crude - doesn't know about comments, etc |
---|
55 | s!\b_\("(.*?)"\)!replacement($1)!gse; |
---|
56 | } elsif (/\s*#\s*define\s+_\(/) { |
---|
57 | $_ = "#include \"message.h\"\n"; |
---|
58 | } |
---|
59 | print; |
---|
60 | } |
---|
61 | |
---|
62 | if ($die) { |
---|
63 | die "Not all messages found!\n"; |
---|
64 | } |
---|
65 | |
---|
66 | sub replacement { |
---|
67 | my $msg = shift; |
---|
68 | $msg =~ s/\\\n//g; |
---|
69 | my $msgno = ""; |
---|
70 | if (exists $revmsgs{$msg}) { |
---|
71 | $msgno = $revmsgs{$msg}; |
---|
72 | } else { |
---|
73 | my $tmp = $msg; |
---|
74 | $tmp =~ s/`(.*?)'/“$1”/g; |
---|
75 | $tmp =~ s/(\w)'(\w)/$1’$2/g; |
---|
76 | if (exists $revmsgs{$tmp}) { |
---|
77 | $msg = $tmp; |
---|
78 | $msgno = $revmsgs{$msg}; |
---|
79 | } else { |
---|
80 | if (!$die) { |
---|
81 | print STDERR "Message(s) not found in message file:\n"; |
---|
82 | $die = 1; |
---|
83 | } |
---|
84 | print STDERR "'$msg'\n"; |
---|
85 | } |
---|
86 | } |
---|
87 | return "msg(/*$msg*/$msgno)"; |
---|
88 | } |
---|