luci-base: zh_CN: update Simplified Chinese translation
[project/luci.git] / build / i18n-update.pl
index c41b039..c82b4fe 100755 (executable)
@@ -1,22 +1,83 @@
 #!/usr/bin/perl
 
 #!/usr/bin/perl
 
-@ARGV >= 1 || die "Usage: $0 <po directory> [<file pattern>]\n";
+@ARGV <= 2 || die "Usage: $0 [<po directory>] [<file pattern>]\n";
 
 my $source  = shift @ARGV;
 my $pattern = shift @ARGV || '*.po';
 
 
 my $source  = shift @ARGV;
 my $pattern = shift @ARGV || '*.po';
 
-if( open F, "find $source -type f -name '$pattern' |" )
+sub read_header
 {
 {
-       while( chomp( my $file = readline F ) )
+       my $file = shift || return;
+       local $/;
+
+       open P, "< $file" || die "open(): $!";
+       my $data = readline P;
+       close P;
+
+       $data =~ /
+               ^ (
+               msgid \s "" \n
+               msgstr \s "" \n
+               (?: " [^\n]+ " \n )+
+               \n )
+       /mx;
+
+       return $1;
+}
+
+sub write_header
+{
+       my $file = shift || return;
+       my $head = shift || return;
+       local $/;
+
+       open P, "< $file" || die "open(): $!";
+       my $data = readline P;
+       close P;
+
+       $data =~ s/
+               ^ (
+               msgid \s "" \n
+               msgstr \s "" \n
+               (?: " [^\n]+ " \n )+
+               \n )
+       /$head/mx;
+
+       open P, "> $file" || die "open(): $!";
+       print P $data;
+       close P;
+}
+
+my @dirs;
+
+if( ! $source )
+{
+       @dirs = glob("./*/*/po/");
+}
+else
+{
+       @dirs = ( $source );
+}
+
+foreach my $dir (@dirs)
+{
+       if( open F, "find $dir -type f -name '$pattern' |" )
        {
        {
-               my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
-               
-               if( -f "$source/templates/$basename.pot" )
+               while( chomp( my $file = readline F ) )
                {
                {
-                       printf "Updating %-40s", $file;
-                       system("msgmerge", "-U", $file, "$source/templates/$basename.pot");
+                       my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
+               
+                       if( -f "$dir/templates/$basename.pot" )
+                       {
+                               my $head = read_header($file);
+
+                               printf "Updating %-40s", $file;
+                               system("msgmerge", "-U", "-N", $file, "$dir/templates/$basename.pot");
+
+                               write_header($file, $head);
+                       }
                }
                }
-       }
 
 
-       close F;
+               close F;
+       }
 }
 }