Merge pull request #1787 from TDT-AG/pr/20180509-net-mwan3-fixes
[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
7 @ARGV >= 1 || die "Usage: $0 <source direcory>\n";
8
9
10 my %stringtable;
11
12 sub dec_lua_str
13 {
14         my $s = shift;
15         $s =~ s/[\s\n]+/ /g;
16         $s =~ s/\\n/\n/g;
17         $s =~ s/\\t/\t/g;
18         $s =~ s/\\(.)/$1/g;
19         $s =~ s/^ //;
20         $s =~ s/ $//;
21         return $s;
22 }
23
24 sub dec_tpl_str
25 {
26         my $s = shift;
27         $s =~ s/-$//;
28         $s =~ s/[\s\n]+/ /g;
29         $s =~ s/^ //;
30         $s =~ s/ $//;
31         $s =~ s/\\/\\\\/g;
32         return $s;
33 }
34
35
36 if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
37 {
38         while( defined( my $file = readline F ) )
39         {
40                 chomp $file;
41
42                 if( open S, "< $file" )
43                 {
44                         local $/ = undef;
45                         my $raw = <S>;
46                         close S;
47
48
49                         my $text = $raw;
50
51                         while( $text =~ s/ ^ .*? (?:translate|translatef|i18n|_) [\n\s]* \( /(/sgx )
52                         {
53                                 ( my $code, $text ) = extract_bracketed($text, q{('")});
54
55                                 $code =~ s/\\\n/ /g;
56                                 $code =~ s/^\([\n\s]*//;
57                                 $code =~ s/[\n\s]*\)$//;
58
59                                 my $res = "";
60                                 my $sub = "";
61
62                                 if( $code =~ /^['"]/ )
63                                 {
64                                         while( defined $sub )
65                                         {
66                                                 ( $sub, $code ) = extract_delimited($code, q{'"}, q{\s*(?:\.\.\s*)?});
67
68                                                 if( defined $sub && length($sub) > 2 )
69                                                 {
70                                                         $res .= substr $sub, 1, length($sub) - 2;
71                                                 }
72                                                 else
73                                                 {
74                                                         undef $sub;
75                                                 }
76                                         }
77                                 }
78                                 elsif( $code =~ /^(\[=*\[)/ )
79                                 {
80                                         my $stag = quotemeta $1;
81                                         my $etag = $stag;
82                                            $etag =~ s/\[/]/g;
83
84                                         ( $res ) = extract_tagged($code, $stag, $etag);
85
86                                         $res =~ s/^$stag//;
87                                         $res =~ s/$etag$//;
88                                 }
89
90                                 $res = dec_lua_str($res);
91                                 $stringtable{$res}++ if $res;
92                         }
93
94
95                         $text = $raw;
96
97                         while( $text =~ s/ ^ .*? <% -? [:_] /<%/sgx )
98                         {
99                                 ( my $code, $text ) = extract_tagged($text, '<%', '%>');
100
101                                 if( defined $code )
102                                 {
103                                         $code = dec_tpl_str(substr $code, 2, length($code) - 4);
104                                         $stringtable{$code}++;
105                                 }
106                         }
107                 }
108         }
109
110         close F;
111 }
112
113
114 if( open C, "| msgcat -" )
115 {
116         printf C "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n";
117
118         foreach my $key ( sort keys %stringtable )
119         {
120                 if( length $key )
121                 {
122                         $key =~ s/"/\\"/g;
123                         printf C "msgid \"%s\"\nmsgstr \"\"\n\n", $key;
124                 }
125         }
126
127         close C;
128 }