Commit from LuCI Translation Portal by user jow.: 155 of 156 messages translated...
[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         return $s;
32 }
33
34
35 if( open F, "find @ARGV -type f '(' -name '*.htm' -o -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
54                                 $code =~ s/\\\n/ /g;
55                                 $code =~ s/^\([\n\s]*//;
56                                 $code =~ s/[\n\s]*\)$//;
57
58                                 my $res = "";
59                                 my $sub = "";
60
61                                 if( $code =~ /^['"]/ )
62                                 {
63                                         while( defined $sub )
64                                         {
65                                                 ( $sub, $code ) = extract_delimited($code, q{'"}, q{\s*(?:\.\.\s*)?});
66
67                                                 if( length($sub) < 1 ) {
68                                                         undef $sub;
69                                                 }
70
71                                                 if( defined $sub )
72                                                 {
73                                                         $res .= substr $sub, 1, length($sub) - 2;
74                                                 }
75                                         }
76                                 }
77                                 elsif( $code =~ /^(\[=*\[)/ )
78                                 {
79                                         my $stag = quotemeta $1;
80                                         my $etag = $stag;
81                                            $etag =~ s/\[/]/g;
82
83                                         ( $res ) = extract_tagged($code, $stag, $etag);
84
85                                         $res =~ s/^$stag//;
86                                         $res =~ s/$etag$//;
87                                 }
88
89                                 $res = dec_lua_str($res);
90                                 $stringtable{$res}++ if $res;
91                         }
92
93
94                         $text = $raw;
95
96                         while( $text =~ s/ ^ .*? <% -? [:_] /<%/sgx )
97                         {
98                                 ( my $code, $text ) = extract_tagged($text, '<%', '%>');
99
100                                 if( defined $code )
101                                 {
102                                         $code = dec_tpl_str(substr $code, 2, length($code) - 4);
103                                         $stringtable{$code}++;
104                                 }
105                         }
106                 }
107         }
108
109         close F;
110 }
111
112
113 if( open C, "| msgcat -" )
114 {
115         printf C "msgid \"\"\nmsgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n";
116
117         foreach my $key ( sort keys %stringtable )
118         {
119                 if( length $key )
120                 {
121                         $key =~ s/"/\\"/g;
122                         printf C "msgid \"%s\"\nmsgstr \"\"\n\n", $key;
123                 }
124         }
125
126         close C;
127 }