contrib: remove asterisk-xip
[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/\\n/\n/g;
16         $s =~ s/\\t/\n/g;
17         $s =~ s/\\(.)/$1/g;
18         $s =~ s/[\s\n]+/ /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         return $s;
32 }
33
34
35 if( open F, "find @ARGV -type f '(' -name '*.htm' -or -name '*.lua' ')' |" )
36 {
37         while( defined( my $file = readline F ) )
38         {
39                 chomp $file;
40
41                 if( open S, "< $file" )
42                 {
43                         local $/ = undef;
44                         my $raw = <S>;
45                         close S;
46
47
48                         my $text = $raw;
49
50                         while( $text =~ s/ ^ .*? (?:translate|translatef|i18n|_) [\n\s]* \( /(/sgx )
51                         {
52                                 ( my $code, $text ) = extract_bracketed($text, q{('")});
53                                 $code =~ s/^\(//; $code =~ s/\)$//;
54
55                                 my $res = "";
56                                 my $sub = "";
57
58                                 while( defined $sub )
59                                 {
60                                         ( $sub, $code ) = extract_delimited($code, q{'"}, q{\s*(?:\.\.\s*)?});
61
62                                         if( defined $sub )
63                                         {
64                                                 $res .= substr $sub, 1, length($sub) - 2;
65                                         }
66                                 }
67
68                                 $res = dec_lua_str($res);
69                                 $stringtable{$res}++;
70                         }
71
72
73                         $text = $raw;
74
75                         while( $text =~ s/ ^ .*? <% [:_] -? /<%/sgx )
76                         {
77                                 ( my $code, $text ) = extract_tagged($text, '<%', '%>');
78
79                                 if( defined $code )
80                                 {
81                                         $code = dec_tpl_str(substr $code, 2, length($code) - 4);
82                                         $stringtable{$code}++;
83                                 }
84                         }
85                 }
86         }
87
88         close F;
89 }
90
91
92 if( open C, "| msgcat -" )
93 {
94         printf C "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n";
95
96         foreach my $key ( sort keys %stringtable )
97         {
98                 if( length $key )
99                 {
100                         $key =~ s/"/\\"/g;
101                         printf C "msgid \"%s\"\nmsgstr \"\"\n\n", $key;
102                 }
103         }
104
105         close C;
106 }