luci-0.8: merge r4521-r4525
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 14 May 2009 21:20:46 +0000 (21:20 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 14 May 2009 21:20:46 +0000 (21:20 +0000)
Makefile
build/i18n-lua2po.pl [new file with mode: 0755]
build/i18n-po2lua.pl [new file with mode: 0755]

index cbebd453576c97b9a7c948bd8cfd2610679e9eae..98fd0e82bd5d502474c184b0aca8b04568b033fd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -67,6 +67,12 @@ uvldocs: hostenv
        build/hostenv.sh $(realpath host) $(LUA_MODULEDIR) $(LUA_LIBRARYDIR) \
        "build/uvldoc $(realpath host) $(UVL_SCHEMEDIR) uvldocs $(DOCS)"
 
+po: host
+       for L in $${LANGUAGE:-$$(find i18n/ -path 'i18n/*/luasrc/i18n/*' -name 'default.*.lua' | \
+         sed -e 's!.*/default\.\(.*\)\.lua!\1!')}; do \
+           build/i18n-lua2po.pl . $(realpath host)/po $$L; \
+       done
+
 run:
        #       make run is deprecated                          #
        #       Please use:                                     #
diff --git a/build/i18n-lua2po.pl b/build/i18n-lua2po.pl
new file mode 100755 (executable)
index 0000000..4905bb4
--- /dev/null
@@ -0,0 +1,117 @@
+#!/usr/bin/perl
+
+@ARGV >= 2 || die "Usage: $0 <source-dir> <dest-dir> [<target-language>]\n";
+
+my $source_dir  = shift @ARGV;
+my $target_dir  = shift @ARGV;
+my $target_lang = shift @ARGV;
+my $master_lang = "en";
+
+
+if( ! -d $target_dir )
+{
+       system('mkdir', '-p', $target_dir);
+}
+
+
+my %target_strings;
+
+
+if( $target_lang && open F, "find $source_dir -path '*/luasrc/i18n/*' -name '*.$target_lang.lua' |" )
+{
+       while( chomp( my $file = readline F ) )
+       {
+               if( open L, "< $file" )
+               {
+                       my ( $basename ) = $file =~ m{.+/([^/]+)\.[\w\-]+\.lua$};
+                       $target_strings{$basename} = { };
+
+                       while( chomp( my $entry = readline L ) )
+                       {
+                               my ( $k, $v );
+                               if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
+                               {
+                                       ( $k, $v ) = ( $1, $2 );
+                               }
+                               elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
+                               {
+                                       ( $k, $v ) = ( $1, $2 );
+                               }
+                               elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
+                               {
+                                       ( $k, $v ) = ( $1, $2 );
+                               }
+                               
+                               if( $k && $v )
+                               {
+                                       $v =~ s/"/\\"/g;
+                                       $v =~ s/\\\\"/\\"/g;
+                                       $target_strings{$basename}{$k} = $v;
+                               }
+                       }
+
+                       close L;
+               }
+       }
+
+       close F;
+}
+
+
+if( open F, "find . -path '*/luasrc/i18n/*' -name '*.$master_lang.lua' |" )
+{
+       my $ext = $target_lang ? "$target_lang.po" : "pot";
+
+       while( chomp( my $file = readline F ) )
+       {
+               if( open L, "< $file" )
+               {
+                       my ( $basename ) = $file =~ m{.+/([^/]+)\.\w+\.lua$};
+
+                       if( open T, "> $target_dir/$basename.$ext" )
+                       {
+                               printf "Generating %-40s ",
+                                       "$target_dir/$basename.$ext";
+               
+                               printf T "#  %s.%s\n#  generated from %s\n\nmsgid \"\"\n" .
+                                        "msgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n",
+                                       $basename, $ext, $file;
+               
+                               while( chomp( my $entry = readline L ) )
+                               {
+                                       my ( $k, $v );
+                                       if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
+                                       {
+                                               ( $k, $v ) = ( $1, $2 );
+                                       }
+                                       elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
+                                       {
+                                               ( $k, $v ) = ( $1, $2 );
+                                       }
+                                       elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
+                                       {
+                                               ( $k, $v ) = ( $1, $2 );
+                                       }
+                               
+                                       if( $k && $v )
+                                       {
+                                               $v =~ s/"/\\"/g;
+                                               $v =~ s/\\\\"/\\"/g;
+                                               printf T "#: %s:%d\n#. \"%s\"\nmsgid \"%s\"\nmsgstr \"%s\"\n\n",
+                                                       $file, $., $v, $k,
+                                                       ( $target_strings{$basename} && $target_strings{$basename}{$k} )
+                                                               ? $target_strings{$basename}{$k} : "";
+                                       }
+                               }
+                               
+                               close T;
+                               
+                               print "done\n";
+                       }
+
+                       close L;
+               }
+       }
+
+       close F;
+}
diff --git a/build/i18n-po2lua.pl b/build/i18n-po2lua.pl
new file mode 100755 (executable)
index 0000000..38e6529
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+
+@ARGV == 2 || die "Usage: $0 <source-dir> <dest-dir>\n";
+
+my $source_dir  = shift @ARGV;
+my $target_dir  = shift @ARGV;
+
+if( ! -d $target_dir )
+{
+       system('mkdir', '-p', $target_dir);
+}
+
+
+my %target_strings;
+
+
+if( open F, "find $source_dir -type f -name '*.po' |" )
+{
+       while( chomp( my $file = readline F ) )
+       {
+               if( open L, "< $file" )
+               {
+                       my ( $basename ) = $file =~ m{.+/([^/]+\.[\w\-]+)\.po$};
+
+                       if( open D, "> $target_dir/$basename.lua" )
+                       {
+                               printf "Generating %-40s ", "$target_dir/$basename.lua";
+
+                               my ( $k, $v );
+
+                               while( chomp( my $line = readline L ) )
+                               {
+                                       if( $line =~ /^msgid "(.+)"/ )
+                                       {
+                                               $k = $1;
+                                       }
+                                       elsif( $k && $line =~ /^msgstr "(.*)"/ )
+                                       {
+                                               $v = $1;
+                                       }
+                                       elsif( $k && defined($v) && $line =~ /^"(.+)"/ )
+                                       {
+                                               $v .= $1;
+                                       }
+                                       else
+                                       {
+                                               if( $k && defined($v) )
+                                               {
+                                                       $v =~ s/\\(['"\\])/$1/g;
+                                                       $v =~ s/(['\\])/\\$1/g;
+
+                                                       printf D "%s%s='%s'\n", $v ? '' : '--', $k, $v;
+                                               }
+               
+                                               $k = $v = undef;
+                                       }
+                               }
+
+                               print "done\n";
+
+                               close D;
+                       }
+
+                       close L;
+               }
+       }
+
+       close F;
+}