7 use Text
::Balanced
qw(gen_extract_tagged);
10 POSIX
::setlocale
(POSIX
::LC_ALL
, 'C');
12 @ARGV >= 1 || die "Usage: $0 <source directory>\n";
16 '.js' => [ '_:1', '_:1,2c', 'N_:2,3', 'N_:2,3,4c' ],
17 '.ut' => [ '_:1', '_:1,2c', 'N_:2,3', 'N_:2,3,4c' ],
18 '.uc' => [ '_:1', '_:1,2c', 'translate:1', 'translate:1,2c', 'N_:2,3', 'N_:2,3,4c', 'ntranslate:2,3', 'ntranslate:2,3,4c' ],
19 '.lua' => [ '_:1', '_:1,2c', 'translate:1', 'translate:1,2c', 'translatef:1', 'N_:2,3', 'N_:2,3,4c', 'ntranslate:2,3', 'ntranslate:2,3,4c' ],
20 '.htm' => [ '_:1', '_:1,2c', 'translate:1', 'translate:1,2c', 'translatef:1', 'N_:2,3', 'N_:2,3,4c', 'ntranslate:2,3', 'ntranslate:2,3,4c' ],
21 '.json' => [ '_:1', '_:1,2c' ]
27 my ($ext) = $path =~ m!(\.\w+)$!;
28 my @cmd = qw(xgettext --from-code=UTF-8 --no-wrap);
30 if ($ext eq '.htm' || $ext eq '.lua') {
31 push @cmd, '--language=Lua';
33 elsif ($ext eq '.ut' || $ext eq '.uc' || $ext eq '.js' || $ext eq '.json') {
34 push @cmd, '--language=JavaScript';
37 push @cmd, map { "--keyword=$_" } (@
{$keywords{$ext}}, @keywords);
43 sub whitespace_collapse
($) {
45 my %r = ('n' => ' ', 't' => ' ');
47 # Translate \t and \n to plain spaces, leave all other escape
48 # sequences alone. Finally replace all consecutive spaces by
49 # single ones and trim leading and trailing space.
50 $s =~ s/\\(.)/$r{$1} || "\\$1"/eg;
58 sub postprocess_pot
($$) {
59 my ($path, $source) = @_;
63 $source =~ s/^#: (.+?)\n/join("\n", map { "#: $path:$_" } $1 =~ m!:(\d+)!g) . "\n"/emg;
65 my @lines = split /\n/, $source;
67 # Remove all header lines up to the first location comment
68 while (@lines > 0 && $lines[0] !~ m!^#: !) {
73 my $line = shift @lines;
75 # Concat multiline msgids and collapse whitespaces
76 if ($line =~ m!^(msg\w+) "(.*)"$!) {
80 while (@lines > 0 && $lines[0] =~ m!^"(.*)"$!) {
85 $kv = whitespace_collapse
($kv);
87 # Filter invalid empty msgids by popping all lines in @res
88 # leading to this point and skip all subsequent lines in
89 # @lines belonging to this faulty id.
90 if ($kw ne 'msgstr' && $kv eq '') {
91 while (@res > 0 && $res[-1] !~ m!^$!) {
95 while (@lines > 0 && $lines[0] =~ m!^(?:msg\w+ )?"(.*)"$!) {
102 push @res, sprintf '%s "%s"', $kw, $kv;
105 # Ignore any flags added by xgettext
106 elsif ($line =~ m!^#, !) {
110 # Pass through other lines unmodified
116 return @res ?
join("\n", '', @res, '') : '';
120 my %h = map { $_, 1 } @_;
124 sub preprocess_htm
($$) {
125 my ($path, $source) = @_;
128 '_' => 'translate([==[%s]==])',
129 ':' => 'translate([==[%s]==])',
130 '+' => 'include([==[%s]==])',
131 '#' => '--[==[%s]==]',
135 # Translate the .htm source into a valid Lua source using bracket quotes
136 # to avoid the need for complex escaping.
137 $source =~ s
!<%-?
([=_
:+#]?)(.*?)-?%>!
141 # Split translation expressions on first non-escaped pipe.
142 if ($t eq ':' || $t eq '_') {
143 $s =~ s/^((?:[^\|\\]|\\.)*)\|(.*)$/$1]==],[==[$2/;
146 sprintf "]==]; $sub->{$t}; [==[", $s
149 # Discover expressions like "lng.translate(...)" or "luci.i18n.translate(...)"
150 # and return them as extra keyword so that xgettext recognizes such expressions
151 # as translate(...) calls.
152 my @extra_function_keywords =
153 map { ("$_:1", "$_:1,2c") }
154 uniq
($source =~ m!((?:\w+\.)+translatef?)[ \t\n]*\(!g);
156 return ("[==[$source]==]", @extra_function_keywords);
159 sub preprocess_ut
($$) {
160 my ($path, $source) = @_;
162 # Translate the .ut source into valid JavaScript code by enclosing template text
163 # in multiline comments and extracting blocks as plain code.
164 my $comt = gen_extract_tagged
('{#', '#}', '(?s).*?(?=\{[#{%])');
165 my $expr = gen_extract_tagged
('{{', '}}', '(?s).*?(?=\{[#{%])');
166 my $stmt = gen_extract_tagged
('{%', '%}', '(?s).*?(?=\{[#{%])');
170 while (length($source)) {
171 my ($block, $remain, $prefix);
173 ($block, $remain, $prefix) = $comt->($source);
174 ($block, $remain, $prefix) = $expr->($source) unless defined $block;
175 ($block, $remain, $prefix) = $stmt->($source) unless defined $block;
177 last unless defined $block;
181 $prefix =~ s!\*/!*\\/!g;
182 $res .= '/*' . $prefix . '*/';
184 if ($block =~ s!^\{#(.*)#}$!$1!s) {
185 $block =~ s!\*/!*\\/!g;
186 $res .= '/*' . $block . '*/';
188 elsif ($block =~ s!^\{\{(.*)}}$!$1!s) {
191 $res .= '(' . $block . ')';
193 elsif ($block =~ s!^\{%(.*)%}$!$1!s) {
196 $res .= '{' . $block . '}';
200 if ($source =~ m!^(.*)\{%[+-]?(.*)$!s) {
204 $prefix =~ s!\*/!*\\/!g;
205 $res .= '/*' . $prefix . '*/';
206 $res .= '{' . $block . '}';
212 sub preprocess_lua
($$) {
213 my ($path, $source) = @_;
215 # Discover expressions like "lng.translate(...)" or "luci.i18n.translate(...)"
216 # and return them as extra keyword so that xgettext recognizes such expressions
217 # as translate(...) calls.
218 my @extra_function_keywords =
219 map { ("$_:1", "$_:1,2c") }
220 uniq
($source =~ m!((?:\w+\.)+translatef?)[ \t\n]*\(!g);
222 return ($source, @extra_function_keywords);
225 sub preprocess_json
($$) {
226 my ($path, $source) = @_;
227 my ($file) = $path =~ m!([^/]+)$!;
229 $source =~ s/("(?:title|description)")\s*:\s*("(?:[^"\\]|\\.)*")/$1: _($2)/sg;
235 my ($msguniq_in, $msguniq_out);
236 my $msguniq_pid = open2
($msguniq_out, $msguniq_in, 'msguniq', '-s');
238 print $msguniq_in "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n";
240 if (open F
, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' -o -name '*.js' -o -name '*.uc' -o -name '*.ut' -o -path '*/menu.d/*.json' -o -path '*/acl.d/*.json' -o -path '*/statistics/plugins/*.json' -o -path '*/https-dns-proxy/providers/*.json' ')' |")
242 while (defined( my $file = readline F
))
246 if (open S
, '<', $file)
250 my @extra_function_keywords;
252 if ($file =~ m!\.htm$!)
254 ($source, @extra_function_keywords) = preprocess_htm
($file, $source);
256 elsif ($file =~ m!\.ut$!)
258 ($source, @extra_function_keywords) = preprocess_ut
($file, $source);
260 elsif ($file =~ m!\.lua$!)
262 ($source, @extra_function_keywords) = preprocess_lua
($file, $source);
264 elsif ($file =~ m!\.json$!)
266 ($source, @extra_function_keywords) = preprocess_json
($file, $source);
269 my ($xgettext_in, $xgettext_out);
270 my $pid = open2
($xgettext_out, $xgettext_in, xgettext
($file, @extra_function_keywords), '-');
272 print $xgettext_in $source;
275 my $pot = readline $xgettext_out;
280 print $msguniq_in postprocess_pot
($file, $pot);
289 my @pot = <$msguniq_out>;
292 waitpid $msguniq_pid, 0;
295 my $line = shift @pot;
297 # Reorder the location comments in a detemrinistic way to
298 # reduce SCM noise when frequently updating templates.
299 if ($line =~ m!^#: !) {
302 while (@pot > 0 && $pot[0] =~ m!^#: !) {
303 push @locs, shift @pot;
307 map { join(':', @
$_) . "\n" }
308 sort { ($a->[0] cmp $b->[0]) || ($a->[1] <=> $b->[1]) }
309 map { [ /^(.+):(\d+)$/ ] }