1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # gettexttomsg.pl |
---|
4 | # |
---|
5 | # Copyright (C) 2001,2002,2005,2011,2012 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 | while (<>) { |
---|
41 | if (!/^\s*#/) { |
---|
42 | while (/\\\n$/) { |
---|
43 | $_ .= <>; |
---|
44 | } |
---|
45 | # very crude - doesn't know about comments, etc |
---|
46 | s!\b_\("(.*?)"\)!replacement($1)!gse; |
---|
47 | } elsif (/\s*#\s*define\s+_\(/) { |
---|
48 | $_ = "#include \"message.h\"\n"; |
---|
49 | } |
---|
50 | print; |
---|
51 | } |
---|
52 | |
---|
53 | if ($die) { |
---|
54 | die "Not all messages found!\n"; |
---|
55 | } |
---|
56 | |
---|
57 | sub replacement { |
---|
58 | my $msg = shift; |
---|
59 | $msg =~ s/\\\n//g; |
---|
60 | my $msgno = ""; |
---|
61 | if (exists $revmsgs{$msg}) { |
---|
62 | $msgno = $revmsgs{$msg}; |
---|
63 | } else { |
---|
64 | my $tmp = $msg; |
---|
65 | $tmp =~ s/`(.*?)'/“$1”/g; |
---|
66 | $tmp =~ s/(\w)'(\w)/$1’$2/g; |
---|
67 | if (exists $revmsgs{$tmp}) { |
---|
68 | $msg = $tmp; |
---|
69 | $msgno = $revmsgs{$msg}; |
---|
70 | } else { |
---|
71 | if (!$die) { |
---|
72 | print STDERR "Message(s) not found in message file:\n"; |
---|
73 | $die = 1; |
---|
74 | } |
---|
75 | print STDERR "'$msg'\n"; |
---|
76 | } |
---|
77 | } |
---|
78 | return "msg(/*$msg*/$msgno)"; |
---|
79 | } |
---|