ddec094caab48c25c9d7a2affdc2157a44246aad
9 POSIX
::setlocale
(POSIX
::LC_ALL
, 'C');
11 @ARGV >= 1 || die "Usage: $0 <source directory>\n";
15 '.js' => [ '_:1', '_:1,2c', 'N_:2,3', 'N_:2,3,4c' ],
16 '.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' ],
17 '.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' ],
18 '.json' => [ '_:1', '_:1,2c' ]
24 my ($ext) = $path =~ m!(\.\w+)$!;
25 my @cmd = qw(xgettext --from-code=UTF-8 --no-wrap);
27 if ($ext eq '.htm' || $ext eq '.lua') {
28 push @cmd, '--language=Lua';
30 elsif ($ext eq '.js' || $ext eq '.json') {
31 push @cmd, '--language=JavaScript';
34 push @cmd, map { "--keyword=$_" } (@
{$keywords{$ext}}, @keywords);
40 sub whitespace_collapse
($) {
42 my %r = ('n' => ' ', 't' => ' ');
44 # Translate \t and \n to plain spaces, leave all other escape
45 # sequences alone. Finally replace all consecutive spaces by
46 # single ones and trim leading and trailing space.
47 $s =~ s/\\(.)/$r{$1} || "\\$1"/eg;
55 sub postprocess_pot
($$) {
56 my ($path, $source) = @_;
60 $source =~ s/^#: (.+?)\n/join("\n", map { "#: $path:$_" } $1 =~ m!:(\d+)!g) . "\n"/emg;
62 my @lines = split /\n/, $source;
64 # Remove all header lines up to the first location comment
65 while (@lines > 0 && $lines[0] !~ m!^#: !) {
70 my $line = shift @lines;
72 # Concat multiline msgids and collapse whitespaces
73 if ($line =~ m!^(msg\w+) "(.*)"$!) {
77 while (@lines > 0 && $lines[0] =~ m!^"(.*)"$!) {
82 $kv = whitespace_collapse
($kv);
84 # Filter invalid empty msgids by popping all lines in @res
85 # leading to this point and skip all subsequent lines in
86 # @lines belonging to this faulty id.
87 if ($kw ne 'msgstr' && $kv eq '') {
88 while (@res > 0 && $res[-1] !~ m!^$!) {
92 while (@lines > 0 && $lines[0] =~ m!^(?:msg\w+ )?"(.*)"$!) {
99 push @res, sprintf '%s "%s"', $kw, $kv;
102 # Ignore any flags added by xgettext
103 elsif ($line =~ m!^#, !) {
107 # Pass through other lines unmodified
113 return @res ?
join("\n", '', @res, '') : '';
117 my %h = map { $_, 1 } @_;
121 sub preprocess_htm
($$) {
122 my ($path, $source) = @_;
125 '_' => 'translate([==[%s]==])',
126 ':' => 'translate([==[%s]==])',
127 '+' => 'include([==[%s]==)',
128 '#' => '--[==[%s]==]',
132 # Translate the .htm source into a valid Lua source using bracket quotes
133 # to avoid the need for complex escaping.
134 $source =~ s
|<%-?
([=_
:+#]?)(.*?)-?%>|sprintf "]==]; $sub->{$1}; [==[", $2|sge;
136 # Discover expressions like "lng.translate(...)" or "luci.i18n.translate(...)"
137 # and return them as extra keyword so that xgettext recognizes such expressions
138 # as translate(...) calls.
139 my @extra_function_keywords =
140 map { ("$_:1", "$_:1,2c") }
141 uniq
($source =~ m!((?:\w+\.)+translatef?)[ \t\n]*\(!g);
143 return ("[==[$source]==]", @extra_function_keywords);
146 sub preprocess_lua
($$) {
147 my ($path, $source) = @_;
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_json
($$) {
160 my ($path, $source) = @_;
161 my ($file) = $path =~ m!([^/]+)$!;
163 $source =~ s/("(?:title)")\s*:\s*("(?:[^"\\]|\\.)*")/$1: _($2)/sg;
169 my ($msguniq_in, $msguniq_out);
170 my $msguniq_pid = open2
($msguniq_out, $msguniq_in, 'msguniq', '-s');
172 print $msguniq_in "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n";
174 if (open F
, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' -o -name '*.js' -o -path '*/menu.d/*.json' ')' |")
176 while (defined( my $file = readline F
))
180 if (open S
, '<', $file)
184 my @extra_function_keywords;
186 if ($file =~ m!\.htm$!)
188 ($source, @extra_function_keywords) = preprocess_htm
($file, $source);
190 elsif ($file =~ m!\.lua$!)
192 ($source, @extra_function_keywords) = preprocess_lua
($file, $source);
194 elsif ($file =~ m!\.json$!)
196 ($source, @extra_function_keywords) = preprocess_json
($file, $source);
199 my ($xgettext_in, $xgettext_out);
200 my $pid = open2
($xgettext_out, $xgettext_in, xgettext
($file, @extra_function_keywords), '-');
202 print $xgettext_in $source;
205 my $pot = readline $xgettext_out;
210 print $msguniq_in postprocess_pot
($file, $pot);
219 my @pot = <$msguniq_out>;
222 waitpid $msguniq_pid, 0;
225 my $line = shift @pot;
227 # Reorder the location comments in a detemrinistic way to
228 # reduce SCM noise when frequently updating templates.
229 if ($line =~ m!^#: !) {
232 while (@pot > 0 && $pot[0] =~ m!^#: !) {
233 push @locs, shift @pot;
237 map { join(':', @
$_) . "\n" }
238 sort { ($a->[0] cmp $b->[0]) || ($a->[1] <=> $b->[1]) }
239 map { [ /^(.+):(\d+)$/ ] }