luci/build/i18n-lua2po.pl

121 lines
2.5 KiB
Perl
Raw Normal View History

2009-05-14 19:44:04 +00:00
#!/usr/bin/perl
@ARGV >= 2 || die "Usage: $0 <source-dir> <dest-dir> [<target-language>]\n";
2009-05-14 19:44:04 +00:00
my $source_dir = shift @ARGV;
my $target_dir = shift @ARGV;
my $target_lang = shift @ARGV;
my $master_lang = "en";
if( ! -d "$target_dir/" . ( $target_lang || 'templates' ) )
2009-05-14 19:44:04 +00:00
{
system('mkdir', '-p', "$target_dir/" . ( $target_lang || 'templates' ));
2009-05-14 19:44:04 +00:00
}
my %target_strings;
if( $target_lang && open F, "find $source_dir -path '*/luasrc/i18n/*' -name '*.$target_lang.lua' |" )
2009-05-14 19:44:04 +00:00
{
while( chomp( my $file = readline F ) )
{
if( open L, "< $file" )
{
my ( $basename ) = $file =~ m{.+/([^/]+)\.[\w\-]+\.lua$};
2009-05-14 19:44:04 +00:00
$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;
2009-05-14 19:44:04 +00:00
$target_strings{$basename}{$k} = $v;
}
}
close L;
}
}
close F;
}
if( open F, "find . -path '*/luasrc/i18n/*' -name '*.$master_lang.lua' |" )
{
my $destfile = sprintf '%s/%s/%%s.%s',
$target_dir,
$target_lang || 'templates',
$target_lang ? 'po' : 'pot'
;
2009-05-14 19:44:04 +00:00
while( chomp( my $file = readline F ) )
{
if( open L, "< $file" )
{
my ( $basename ) = $file =~ m{.+/([^/]+)\.\w+\.lua$};
my $filename = sprintf $destfile, $basename;
2009-05-14 19:44:04 +00:00
if( open T, "> $filename" )
2009-05-14 19:44:04 +00:00
{
printf "Generating %-40s ", $filename;
2009-05-14 21:10:40 +00:00
printf T "# %s.%s\n# generated from %s\n\nmsgid \"\"\n" .
2009-05-14 19:44:04 +00:00
"msgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n",
$basename, $target_lang ? 'po' : 'pot', $file;
2009-05-14 19:44:04 +00:00
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",
2009-05-14 19:44:04 +00:00
$file, $., $v, $k,
( $target_strings{$basename} && $target_strings{$basename}{$k} )
2009-05-18 20:54:32 +00:00
? $target_strings{$basename}{$k} : "";
2009-05-14 19:44:04 +00:00
}
}
close T;
print "done\n";
}
close L;
}
}
close F;
}