Merge pull request #2442 from TDT-AG/pr/20190109-luci-mod-system-pollinterval
[project/luci.git] / build / i18n-scan.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Text::Balanced qw(extract_bracketed extract_delimited extract_tagged);
6 use POSIX;
7
8 POSIX::setlocale(POSIX::LC_ALL, "C");
9
10 @ARGV >= 1 || die "Usage: $0 <source directory>\n";
11
12
13 my %stringtable;
14
15 sub dec_lua_str
16 {
17 my $s = shift;
18 $s =~ s/[\s\n]+/ /g;
19 $s =~ s/\\n/\n/g;
20 $s =~ s/\\t/\t/g;
21 $s =~ s/\\(.)/$1/g;
22 $s =~ s/^ //;
23 $s =~ s/ $//;
24 return $s;
25 }
26
27 sub dec_tpl_str
28 {
29 my $s = shift;
30 $s =~ s/-$//;
31 $s =~ s/[\s\n]+/ /g;
32 $s =~ s/^ //;
33 $s =~ s/ $//;
34 $s =~ s/\\/\\\\/g;
35 return $s;
36 }
37
38
39 if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' -o -name '*.js' ')' | sort |" )
40 {
41 while( defined( my $file = readline F ) )
42 {
43 chomp $file;
44
45 if( open S, "< $file" )
46 {
47 local $/ = undef;
48 my $raw = <S>;
49 close S;
50
51
52 my $text = $raw;
53 my $line = 1;
54
55 while( $text =~ s/ ^ (.*?) (?:translate|translatef|i18n|_) ([\n\s]*) \( /(/sgx )
56 {
57 my ($prefix, $suffix) = ($1, $2);
58
59 ( my $code, $text ) = extract_bracketed($text, q{('")});
60
61 $line += () = $prefix =~ /\n/g;
62
63 my $position = "$file:$line";
64
65 $line += () = $suffix =~ /\n/g;
66 $line += () = $code =~ /\n/g;
67
68 $code =~ s/\\\n/ /g;
69 $code =~ s/^\([\n\s]*//;
70 $code =~ s/[\n\s]*\)$//;
71
72 my $res = "";
73 my $sub = "";
74
75 if( $code =~ /^['"]/ )
76 {
77 while( defined $sub )
78 {
79 ( $sub, $code ) = extract_delimited($code, q{'"}, q{\s*(?:\.\.\s*)?});
80
81 if( defined $sub && length($sub) > 2 )
82 {
83 $res .= substr $sub, 1, length($sub) - 2;
84 }
85 else
86 {
87 undef $sub;
88 }
89 }
90 }
91 elsif( $code =~ /^(\[=*\[)/ )
92 {
93 my $stag = quotemeta $1;
94 my $etag = $stag;
95 $etag =~ s/\[/]/g;
96
97 ( $res ) = extract_tagged($code, $stag, $etag);
98
99 $res =~ s/^$stag//;
100 $res =~ s/$etag$//;
101 }
102
103 $res = dec_lua_str($res);
104
105 if ($res) {
106 $stringtable{$res} ||= [ ];
107 push @{$stringtable{$res}}, $position;
108 }
109 }
110
111
112 $text = $raw;
113 $line = 1;
114
115 while( $text =~ s/ ^ (.*?) <% -? [:_] /<%/sgx )
116 {
117 $line += () = $1 =~ /\n/g;
118
119 ( my $code, $text ) = extract_tagged($text, '<%', '%>');
120
121 if( defined $code )
122 {
123 my $position = "$file:$line";
124
125 $line += () = $code =~ /\n/g;
126
127 $code = dec_tpl_str(substr $code, 2, length($code) - 4);
128
129 $stringtable{$code} ||= [];
130 push @{$stringtable{$code}}, $position;
131 }
132 }
133 }
134 }
135
136 close F;
137 }
138
139
140 if( open C, "| msgcat -" )
141 {
142 printf C "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n";
143
144 foreach my $key ( sort keys %stringtable )
145 {
146 if( length $key )
147 {
148 my @positions = @{$stringtable{$key}};
149
150 $key =~ s/\\/\\\\/g;
151 $key =~ s/\n/\\n/g;
152 $key =~ s/\t/\\t/g;
153 $key =~ s/"/\\"/g;
154
155 printf C "#: %s\nmsgid \"%s\"\nmsgstr \"\"\n\n",
156 join(' ', @positions), $key;
157 }
158 }
159
160 close C;
161 }