Merge pull request #608 from sairon/relative-luci-mk-paths
[project/luci.git] / build / i18n-update.pl
1 #!/usr/bin/perl
2
3 @ARGV <= 2 || 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 my @dirs;
52
53 if( ! $source )
54 {
55         @dirs = glob("./*/*/po/");
56 }
57 else
58 {
59         @dirs = ( $source );
60 }
61
62 foreach my $dir (@dirs)
63 {
64         if( open F, "find $dir -type f -name '$pattern' |" )
65         {
66                 while( chomp( my $file = readline F ) )
67                 {
68                         my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
69                 
70                         if( -f "$dir/templates/$basename.pot" )
71                         {
72                                 my $head = read_header($file);
73
74                                 printf "Updating %-40s", $file;
75                                 system("msgmerge", "-U", "-N", $file, "$dir/templates/$basename.pot");
76
77                                 write_header($file, $head);
78                         }
79                 }
80
81                 close F;
82         }
83 }