1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
#!/usr/bin/perl
#
# po2c - Converts .po files to C code
#
# Copyright (C) 2004 Angel Ortega <angel@triptico.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# http://www.triptico.com
#
$VERSION = "1.0.2-scummvm";
if(scalar(@ARGV) == 0)
{
print "Usage: po2c {po file[s]}\n";
exit 1;
}
%msgs = ();
%msgids = ();
# stage 1: loading
# arguments are .po files
foreach my $f (@ARGV)
{
my ($lang);
my ($langDesc);
next unless(($lang) = ($f =~ /([^\/]+)\.po$/));
if(open F, $f)
{
my ($msgid, $val, %a);
while(<F>)
{
chomp;
# ignore blank lines or comments
next if /^$/ or /^#/;
if(/^msgid\s+\"(.*)\"\s*$/)
{
# store previous msgid
if(defined($msgid))
{
$a{$msgid} = $val;
$msgids{$msgid} ++;
}
# start of msgid
$val = $1;
}
elsif(/^msgstr\s+\"(.*)\"\s*$/)
{
# store previous msgid
$msgid = $val;
# start of msgstr
$val = $1;
}
elsif(/^\"(.*)\"\s*$/)
{
# add to current value
$val .= $1;
}
}
# store previous msgid
if(defined($msgid))
{
$a{$msgid} = $val;
$msgids{$msgid} ++;
}
close F;
# add to the global message pool
$msgs{$lang} = \%a;
}
}
# stage 2: convert the data
# stores all sorted msgids into @msgids
@msgids = sort(keys(%msgids));
# travels again, storing indexes into %msgids
for(my $n = 0;$n < scalar(@msgids);$n++)
{
$msgids{$msgids[$n]} = $n;
}
# stage 3: dump as C++ code
print "// generated by po2c $VERSION - Do not modify\n\n";
# dump first the msgid array
print "static const char * const _messageIds[] = {\n";
for(my $n = 0;$n < scalar(@msgids);$n++)
{
print "\t/* $n */ \"" . $msgids[$n] . "\",\n";
}
print "\tNULL\n};\n\n";
# dump the lang structure
print "struct PoMessageEntry {\n";
print "\tint msgid;\n";
print "\tconst char *msgstr;\n";
print "};\n\n";
# dump now each language
foreach my $l (keys(%msgs))
{
print "static const PoMessageEntry _translation_${l}\[\] = {\n";
# get the translation table for the language $l
my ($m) = $msgs{$l};
# while (my ($msgstr, $msgid) = each (%$m))
foreach my $msgid (sort(keys(%$m)))
{
my ($msgstr) = "";
# make it 7-bit safe
foreach $c (split(//, $m->{$msgid})) {
if (ord($c) > 0x7f) {
$msgstr .= sprintf("\\%o", ord($c));
} else {
$msgstr .= $c;
}
}
print "\t{ " . $msgids{$msgid} . ", \"" . $msgstr . "\" },\n"
if $msgstr;
}
print "\t{ -1, NULL }\n};\n\n";
}
# finally, dump the languages
print "struct PoLangEntry {\n";
print "\tconst char *lang;\n";
print "\tconst char *charset;\n";
print "\tconst PoMessageEntry *msgs;\n";
print "};\n\n";
print "const PoLangEntry _translations[] = {\n";
foreach my $l (keys(%msgs))
{
$header = $msgs{$l}->{""};
$header =~ /charset=([^\\]+)/;
$charset = $1;
print "\t{ \"" . $l . "\", \"" . $charset . "\", _translation_${l} },\n";
}
print "\t{ NULL, NULL, NULL }\n};\n\n";
print "// code\n";
print << 'EOF';
static const PoMessageEntry *_currentTranslation = NULL;
static int _currentTranslationMessageEntryCount = 0;
static const char *_currentTranslationCharset = NULL;
void po2c_setlang(const char *lang) {
_currentTranslation = NULL;
_currentTranslationMessageEntryCount = 0;
_currentTranslationCharset = NULL;
// if lang is NULL or "", deactivate it
if (lang == NULL || *lang == '\0')
return;
// searches for a valid language array
for (int i = 0; _currentTranslation == NULL && _translations[i].lang != NULL; ++i) {
if (strcmp(lang, _translations[i].lang) == 0) {
_currentTranslation = _translations[i].msgs;
_currentTranslationCharset = _translations[i].charset;
}
}
// try partial searches
for (int i = 0; _currentTranslation == NULL && _translations[i].lang != NULL; ++i) {
if (strncmp(lang, _translations[i].lang, 2) == 0) {
_currentTranslation = _translations[i].msgs;
_currentTranslationCharset = _translations[i].charset;
}
}
// if found, count entries
if (_currentTranslation != NULL) {
for (const PoMessageEntry *m = _currentTranslation; m->msgid != -1; ++m)
++_currentTranslationMessageEntryCount;
}
}
const char *po2c_gettext(const char *msgid) {
// if no language is set or msgid is empty, return msgid as is
if (_currentTranslation == NULL || *msgid == '\0')
return msgid;
// binary-search for the msgid
int leftIndex = 0;
int rightIndex = _currentTranslationMessageEntryCount - 1;
while (rightIndex >= leftIndex) {
const int midIndex = (leftIndex + rightIndex) / 2;
const PoMessageEntry * const m = &_currentTranslation[midIndex];
const int compareResult = strcmp(msgid, _messageIds[m->msgid]);
if (compareResult == 0)
return m->msgstr;
else if (compareResult < 0)
rightIndex = midIndex - 1;
else
leftIndex = midIndex + 1;
}
return msgid;
}
const char *po2c_getcharset(void) {
if (_currentTranslationCharset)
return _currentTranslationCharset;
else
return "ASCII";
}
int po2c_getnumlangs(void) {
return ARRAYSIZE(_translations) - 1;
}
const char *po2c_getlang(const int num) {
assert(num < ARRAYSIZE(_translations));
return _translations[num].lang;
}
EOF
exit 0;
|