build: add script for po to lua conversation
[project/luci.git] / build / i18n-po2lua.pl
1 #!/usr/bin/perl
2
3 @ARGV == 2 || die "Usage: $0 <source-dir> <dest-dir>\n";
4
5 my $source_dir = shift @ARGV;
6 my $target_dir = shift @ARGV;
7
8 if( ! -d $target_dir )
9 {
10 system('mkdir', '-p', $target_dir);
11 }
12
13
14 my %target_strings;
15
16
17 if( open F, "find $source_dir -type f -name '*.po' |" )
18 {
19 while( chomp( my $file = readline F ) )
20 {
21 if( open L, "< $file" )
22 {
23 my ( $basename ) = $file =~ m{.+/([^/]+\.[\w\-]+)\.po$};
24
25 if( open D, "> $target_dir/$basename.lua" )
26 {
27 printf "Generating %-40s ", "$target_dir/$basename.lua";
28
29 my ( $k, $v );
30
31 while( chomp( my $line = readline L ) )
32 {
33 if( $line =~ /^msgid "(.+)"/ )
34 {
35 $k = $1;
36 }
37 elsif( $k && $line =~ /^msgstr "(.*)"/ )
38 {
39 $v = $1;
40 }
41 elsif( $k && defined($v) && $line =~ /^"(.+)"/ )
42 {
43 $v .= $1;
44 }
45 else
46 {
47 if( $k && defined($v) )
48 {
49 $v =~ s/\\(['"\\])/$1/g;
50 $v =~ s/(['\\])/\\$1/g;
51
52 printf D "%s%s='%s'\n", $v ? '' : '--', $k, $v;
53 }
54
55 $k = $v = undef;
56 }
57 }
58
59 print "done\n";
60
61 close D;
62 }
63
64 close L;
65 }
66 }
67
68 close F;
69 }