luci-app-statistics: only include lua modules into local scope
[oweals/luci.git] / build / i18n-scan.pl
index 1243545ac5134b2ef371257264e904b92ef6c411..f47fc3553c7a7b7600150fc76f006d61e2b4ded3 100755 (executable)
@@ -3,8 +3,11 @@
 use strict;
 use warnings;
 use Text::Balanced qw(extract_bracketed extract_delimited extract_tagged);
+use POSIX;
 
-@ARGV >= 1 || die "Usage: $0 <source direcory>\n";
+POSIX::setlocale(POSIX::LC_ALL, "C");
+
+@ARGV >= 1 || die "Usage: $0 <source directory>\n";
 
 
 my %stringtable;
@@ -28,11 +31,12 @@ sub dec_tpl_str
        $s =~ s/[\s\n]+/ /g;
        $s =~ s/^ //;
        $s =~ s/ $//;
+       $s =~ s/\\/\\\\/g;
        return $s;
 }
 
 
-if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
+if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' -o -name '*.js' ')' | sort |" )
 {
        while( defined( my $file = readline F ) )
        {
@@ -46,11 +50,21 @@ if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
 
 
                        my $text = $raw;
+                       my $line = 1;
 
-                       while( $text =~ s/ ^ .*? (?:translate|translatef|i18n|_) [\n\s]* \( /(/sgx )
+                       while( $text =~ s/ ^ (.*?) (?:translate|translatef|i18n|_) ([\n\s]*) \( /(/sgx )
                        {
+                               my ($prefix, $suffix) = ($1, $2);
+
                                ( my $code, $text ) = extract_bracketed($text, q{('")});
 
+                               $line += () = $prefix =~ /\n/g;
+
+                               my $position = "$file:$line";
+
+                               $line += () = $suffix =~ /\n/g;
+                               $line += () = $code   =~ /\n/g;
+
                                $code =~ s/\\\n/ /g;
                                $code =~ s/^\([\n\s]*//;
                                $code =~ s/[\n\s]*\)$//;
@@ -60,7 +74,7 @@ if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
 
                                if( $code =~ /^['"]/ )
                                {
-                                       while( defined $sub && length($sub) > 0 )
+                                       while( defined $sub )
                                        {
                                                ( $sub, $code ) = extract_delimited($code, q{'"}, q{\s*(?:\.\.\s*)?});
 
@@ -68,6 +82,10 @@ if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
                                                {
                                                        $res .= substr $sub, 1, length($sub) - 2;
                                                }
+                                               else
+                                               {
+                                                       undef $sub;
+                                               }
                                        }
                                }
                                elsif( $code =~ /^(\[=*\[)/ )
@@ -83,20 +101,33 @@ if( open F, "find @ARGV -type f '(' -name '*.htm' -o -name '*.lua' ')' |" )
                                }
 
                                $res = dec_lua_str($res);
-                               $stringtable{$res}++ if $res;
+
+                               if ($res) {
+                                       $stringtable{$res} ||= [ ];
+                                       push @{$stringtable{$res}}, $position;
+                               }
                        }
 
 
                        $text = $raw;
+                       $line = 1;
 
-                       while( $text =~ s/ ^ .*? <% -? [:_] /<%/sgx )
+                       while( $text =~ s/ ^ (.*?) <% -? [:_] /<%/sgx )
                        {
+                               $line += () = $1 =~ /\n/g;
+
                                ( my $code, $text ) = extract_tagged($text, '<%', '%>');
 
                                if( defined $code )
                                {
+                                       my $position = "$file:$line";
+
+                                       $line += () = $code =~ /\n/g;
+
                                        $code = dec_tpl_str(substr $code, 2, length($code) - 4);
-                                       $stringtable{$code}++;
+
+                                       $stringtable{$code} ||= [];
+                                       push @{$stringtable{$code}}, $position;
                                }
                        }
                }
@@ -114,8 +145,15 @@ if( open C, "| msgcat -" )
        {
                if( length $key )
                {
+                       my @positions = @{$stringtable{$key}};
+
+                       $key =~ s/\\/\\\\/g;
+                       $key =~ s/\n/\\n/g;
+                       $key =~ s/\t/\\t/g;
                        $key =~ s/"/\\"/g;
-                       printf C "msgid \"%s\"\nmsgstr \"\"\n\n", $key;
+
+                       printf C "#: %s\nmsgid \"%s\"\nmsgstr \"\"\n\n",
+                               join(' ', @positions), $key;
                }
        }