4106c6da4c1c5a1cb06294ef87129c2b8497c3c5
[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 read_header
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 =~ /
18 ^ (
19 msgid \s "" \n
20 msgstr \s "" \n
21 (?: " [^\n]+ " \n )+
22 \n )
23 /mx;
24
25 return $1;
26 }
27
28 sub write_header
29 {
30 my $file = shift || return;
31 my $head = shift || return;
32 local $/;
33
34 open P, "< $file" || die "open(): $!";
35 my $data = readline P;
36 close P;
37
38 $data =~ s/
39 ^ (
40 msgid \s "" \n
41 msgstr \s "" \n
42 (?: " [^\n]+ " \n )+
43 \n )
44 /$head/mx;
45
46 open P, "> $file" || die "open(): $!";
47 print P $data;
48 close P;
49 }
50
51 if( open F, "find $source -type f -name '$pattern' |" )
52 {
53 while( chomp( my $file = readline F ) )
54 {
55 my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
56
57 if( -f "$source/templates/$basename.pot" )
58 {
59 my $head = read_header($file);
60
61 printf "Updating %-40s", $file;
62 system("msgmerge", "-U", "-N", $file, "$source/templates/$basename.pot");
63
64 write_header($file, $head);
65 }
66 }
67
68 close F;
69 }