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