102ae5f7aaff9db39e556ecb61cf562211f71249
[project/luci.git] / build / i18n-scan.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Text::Balanced qw(extract_codeblock);
6
7 @ARGV == 1 || die "Usage: $0 <source direcory>\n";
8
9
10 sub _parse
11 {
12 my ( $code ) = @_;
13 my ( $k, $v );
14
15 if( $code =~ s/^<%:-?\s*(.+)\s*%>/$1/s )
16 {
17 my ( $key, @text ) = split /[\n\s]+/, $code;
18
19 $k = $key;
20 $v = join ' ', @text;
21 }
22 elsif( $code =~ s/^\(\s*(.+)\s*\)/$1/s )
23 {
24 if( $code =~ /^(?:"(\w+)"|'(\w+)')\s*,\s*(?:"(.+?)"|'(.+?)')/s )
25 {
26 $k = $1 || $2;
27 $v = $3 || $4 || '';
28 $v =~ s/\s+/ /sg;
29 }
30 elsif( $code =~ /^(?:"(\w+)"|'(\w+)')/ )
31 {
32 $k = $1 || $2;
33 $v = '';
34 }
35 else
36 {
37 return ();
38 }
39 }
40 else
41 {
42 return ();
43 }
44
45 $v =~ s/\\"/"/g;
46 $v =~ s/"/\\"/g;
47
48 return ( $k, $v );
49 }
50
51
52 if( open F, "find $ARGV[0] -type f -name '*.htm' -or -name '*.lua' |" )
53 {
54 while( defined( my $file = readline F ) )
55 {
56 chomp $file;
57
58 if( open S, "< $file" )
59 {
60 my $text = '';
61 $text .= $_ foreach( readline S );
62
63 while(
64 $text =~ s/
65 ^ .*?
66 (?:
67 (?: translate f? | i18n )
68 [\s\n]* ( \( )
69 |
70 ( \<%: -? )
71 )
72 /$1 || $2/segx
73 ) {
74 #$text =~ s/^ .*? (?: (?: translate f? | i18n ) [\s\n]* ( \( ) | ( <%: -? ) ) / $1 || $2 /sex;
75 #warn "T[$text]";
76 my $code;
77
78 reparse:
79 ( $code, $text ) = extract_codeblock( $text, '', '^', '()' );
80 if( ! $code ) {
81 ( $code, $text ) = extract_codeblock( $text, '', '^', '<>' );
82 }
83
84 if( ! $code ) {
85 # Corner case:
86 $text =~ s/(#[^\n]*)%>/$1\n%>/;
87 ( $code, $text ) = extract_codeblock( $text, '<>', '^' );
88 if( ! $code ) {
89 last;
90 }
91 }
92
93 my ( $k, $v ) = _parse( $code );
94 #warn "M[$code]";
95 #last;
96
97 if( $k && defined($v) )
98 {
99 if( $v )
100 {
101 printf "#. %s\n", $v || $k;
102 }
103
104 printf "msgid \"%s\"\nmsgstr \"%s\"\n\n",
105 $k, $v;
106 }
107 }
108
109 close S;
110 }
111 }
112
113 close F;
114 }