/
extract_i18n_strings_portal.pl
294 lines (221 loc) · 6.46 KB
/
extract_i18n_strings_portal.pl
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/perl -w
=head1 NAME
extract_i18n_strings.pl - extract localizable strings
=head1 SYNOPSIS
=head1 DESCRIPTION
The script extracts the strings from the source code and the HTML templates that
can be localized.
=cut
use File::Find;
use lib qw(/usr/local/pf/lib /usr/local/pf/html/pfappserver/lib);
use pf::web::constants;
use constant {
CONF => 'conf',
PORTAL => 'html/captive-portal/templates'
};
my %strings = ();
my %translations = ();
=head1 SUBROUTINES
=head2 add_string
Add a localizable sring to the list.
=cut
sub add_string {
my ($string, $source) = @_;
$string =~ s/(?<!\\)\"/\\\"/g; # escape double-quotes
unless ($strings{$string}) {
$strings{$string} = [];
}
unless (grep(/\Q$source\E/, @{$strings{$string}})) {
push(@{$strings{$string}}, $source);
}
}
=head2 add_translation
Add an existing English translation.
=cut
sub add_translation {
my ($key, $value) = @_;
$translations{$key} = $value;
}
=head2 parse_po
Parse the English PO file to extract existing translations because some keys are
translated even for English.
=cut
sub parse_po {
my $file = 'conf/locale/en/LC_MESSAGES/packetfence.po';
my ($key, %msg);
open (PO, $file);
my $line;
while (defined($line = <PO>)) {
chomp $line;
if ($line =~ m/^\s*\"(.+)\"$/) {
if ($key) {
$msg{$key} .= $1;
}
}
elsif ($line =~ m/^(msgid|msgstr) \"(.*)\"$/) {
if ($msg{msgid} && $msg{msgstr}) {
add_translation($msg{msgid}, $msg{msgstr});
delete $msg{msgid};
delete $msg{msgstr};
}
elsif ($1 eq 'msgid') {
delete $msg{msgstr};
}
$key = $1;
$msg{$key} = $2;
}
}
if ($msg{msgid} && $msg{msgstr}) {
add_translation($msg{msgid}, $msg{msgstr});
}
}
=head2 parse_tt
Extract localizable strings from TT templates.
=cut
sub parse_tt {
my $dir = PORTAL;
my @templates = ();
my $tt = sub {
return unless -f && m/\.(html)$/;
push(@templates, $File::Find::name);
};
find($tt, $dir);
my $line;
foreach my $template (@templates) {
open(TT, $template);
while (defined($line = <TT>)) {
chomp $line;
while ($line =~ m/i18n(_format)?\(['"](.+?(?!\\))['"](,.*)?\)/g) {
add_string($2, $template) unless ($2 =~ m/\${/);
}
}
close(TT);
}
}
=head2 parse_mc
Extract localizable strings from Models and Controllers classes.
=cut
sub parse_mc {
my $base = 'lib/pf/web/';
my @modules = ('lib/pf/web.pm');
my $pm = sub {
return unless -f && m/\.pm$/;
push(@modules, $File::Find::name);
};
find($pm, $base);
my $line;
foreach my $module (@modules) {
open(PM, $module);
while (defined($line = <PM>)) {
chomp $line;
if ($line =~ m/i18n(_format)?\(['"]([^\$].+?[^'"\\])["']\)/) {
my $string = $2;
$string =~ s/\\'/'/g;
add_string($string, $module);
}
}
close(PM);
}
}
=head2 extract_modules
Extract various localizable strings from PacketFence modules.
=cut
sub extract_modules {
my %strings = ();
sub const {
my ($module, $name, $arrayref) = @_;
foreach (@$arrayref) {
add_string($_, "$module ($name)");
}
}
const('pf::web::constants', 'Locales', \@WEB::LOCALES);
}
=head2 print_po
Print the PO file constructed from the extracted localizable strings.
=cut
sub print_po {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
my $now = sprintf("%d-%02d-%02d %02d:%02d-0400", $year+1900, $mon+1, $mday, $hour, $min);
open(RELEASE, CONF.'/pf-release');
my $content = <RELEASE>;
chomp $content;
my ($package, $version) = $content =~ m/(\S+) ([\d\.]+)/;
close(RELEASE);
print <<EOT;
# English translations for $package package.
# Copyright (C) 2005-2016 Inverse inc.
# This file is distributed under the same license as the $package package.
#
msgid ""
msgstr ""
"Project-Id-Version: $version\\n"
"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"PO-Revision-Date: $now\\n"
"Last-Translator: Inverse inc. <info\@inverse.ca>\\n"
"Language-Team: English\\n"
"Language: en\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=ASCII\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"
EOT
foreach my $string (sort keys %strings) {
foreach my $file (sort @{$strings{$string}}) {
print "# $file\n";
}
my @lines = split("\n", $string);
if (@lines > 1) {
print "msgid \"\"\n";
print join("\n", map { " \"$_\"" } @lines), "\n";
}
else {
print "msgid \"$string\"\n";
}
print "msgstr \"" . ($translations{$string} || '') . "\"\n\n";
}
}
=head2 verify
Check if any translated string was not extracted. In this case, we need to
manually check if the string is still used.
=cut
sub verify {
my @translated_keys = keys %translations;
my @extracted_keys = keys %strings;
my %seen;
@seen {@translated_keys} = ( );
delete @seen {@extracted_keys};
my @translated_not_extracted = keys %seen;
if (scalar @translated_not_extracted) {
warn "The following keys were not extracted:\n\t" .
join("\n\t", sort @translated_not_extracted) .
"\n";
}
}
#### MAIN ####
&parse_po;
&parse_tt;
&parse_mc;
&extract_modules;
&print_po;
&verify;
=head1 AUTHOR
Inverse inc. <info@inverse.ca>
=head1 COPYRIGHT
Copyright (C) 2005-2016 Inverse inc.
=head1 LICENSE
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
=cut
# vim: set shiftwidth=4:
# vim: set expandtab:
# vim: set backspace=indent,eol,start: