modules/base: ltn12: source.file() should terminate when an empty chunk is read
[project/luci.git] / build / i18n-update.pl
index c41b039..4106c6d 100755 (executable)
@@ -5,6 +5,49 @@
 my $source  = shift @ARGV;
 my $pattern = shift @ARGV || '*.po';
 
+sub read_header
+{
+       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;
+}
+
 if( open F, "find $source -type f -name '$pattern' |" )
 {
        while( chomp( my $file = readline F ) )
@@ -13,8 +56,12 @@ if( open F, "find $source -type f -name '$pattern' |" )
                
                if( -f "$source/templates/$basename.pot" )
                {
+                       my $head = read_header($file);
+
                        printf "Updating %-40s", $file;
-                       system("msgmerge", "-U", $file, "$source/templates/$basename.pot");
+                       system("msgmerge", "-U", "-N", $file, "$source/templates/$basename.pot");
+
+                       write_header($file, $head);
                }
        }