build: fixup header order in i18n-update.pl to minimize diffs to pootle
[project/luci.git] / build / i18n-update.pl
1 #!/usr/bin/perl
2
3 @ARGV >= 1 || die "Usage: $0 <po directory> [<file pattern>]\n";
4
5 my $source  = shift @ARGV;
6 my $pattern = shift @ARGV || '*.po';
7
8 sub fixup_header_order
9 {
10         my $file = shift || return;
11         local $/;
12
13         open P, "< $file" || die "open(): $!";
14         my $data = readline P;
15         close P;
16
17         $data =~ s/("Language-Team: .*?\\n"\n)(.+?)("Language: .*?\\n"\n)/$1$3$2/s;
18
19         open P, "> $file" || die "open(): $!";
20         print P $data;
21         close P;
22 }
23
24 if( open F, "find $source -type f -name '$pattern' |" )
25 {
26         while( chomp( my $file = readline F ) )
27         {
28                 my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
29                 
30                 if( -f "$source/templates/$basename.pot" )
31                 {
32                         printf "Updating %-40s", $file;
33                         system("msgmerge", "-U", "-N", $file, "$source/templates/$basename.pot");
34                         fixup_header_order($file);
35                 }
36         }
37
38         close F;
39 }