Commit from LuCI Translation Portal by user jow.: 127 of 127 messages translated...
[project/luci.git] / build / i18n-lua2po.pl
1 #!/usr/bin/perl
2
3 @ARGV >= 2 || die "Usage: $0 <source-dir> <dest-dir> [<target-language>]\n";
4
5 my $source_dir = shift @ARGV;
6 my $target_dir = shift @ARGV;
7 my $target_lang = shift @ARGV;
8 my $master_lang = "en";
9
10
11 if( ! -d "$target_dir/" . ( $target_lang || 'templates' ) )
12 {
13 system('mkdir', '-p', "$target_dir/" . ( $target_lang || 'templates' ));
14 }
15
16
17 my %target_strings;
18
19 if( $target_lang && open F, "find $source_dir -path '*/luasrc/i18n/*' -name '*.$target_lang.lua' |" )
20 {
21 while( chomp( my $file = readline F ) )
22 {
23 if( open L, "< $file" )
24 {
25 my ( $basename ) = $file =~ m{.+/([^/]+)\.[\w\-]+\.lua$};
26 $target_strings{$basename} = { };
27
28 while( chomp( my $entry = readline L ) )
29 {
30 my ( $k, $v );
31 if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
32 {
33 ( $k, $v ) = ( $1, $2 );
34 }
35 elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
36 {
37 ( $k, $v ) = ( $1, $2 );
38 }
39 elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
40 {
41 ( $k, $v ) = ( $1, $2 );
42 }
43
44 if( $k && $v )
45 {
46 $v =~ s/"/\\"/g;
47 $v =~ s/\\\\"/\\"/g;
48 $target_strings{$basename}{$k} = $v;
49 }
50 }
51
52 close L;
53 }
54 }
55
56 close F;
57 }
58
59
60 if( open F, "find . -path '*/luasrc/i18n/*' -name '*.$master_lang.lua' |" )
61 {
62 my $destfile = sprintf '%s/%s/%%s.%s',
63 $target_dir,
64 $target_lang || 'templates',
65 $target_lang ? 'po' : 'pot'
66 ;
67
68 while( chomp( my $file = readline F ) )
69 {
70 if( open L, "< $file" )
71 {
72 my ( $basename ) = $file =~ m{.+/([^/]+)\.\w+\.lua$};
73 my $filename = sprintf $destfile, $basename;
74
75 if( open T, "> $filename" )
76 {
77 printf "Generating %-40s ", $filename;
78
79 printf T "# %s.%s\n# generated from %s\n\nmsgid \"\"\n" .
80 "msgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n",
81 $basename, $target_lang ? 'po' : 'pot', $file;
82
83 while( chomp( my $entry = readline L ) )
84 {
85 my ( $k, $v );
86 if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
87 {
88 ( $k, $v ) = ( $1, $2 );
89 }
90 elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
91 {
92 ( $k, $v ) = ( $1, $2 );
93 }
94 elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
95 {
96 ( $k, $v ) = ( $1, $2 );
97 }
98
99 if( $k && $v )
100 {
101 $v =~ s/"/\\"/g;
102 $v =~ s/\\\\"/\\"/g;
103 printf T "#: %s:%d\n#. %s\nmsgid \"%s\"\nmsgstr \"%s\"\n\n",
104 $file, $., $v, $k,
105 ( $target_strings{$basename} && $target_strings{$basename}{$k} )
106 ? $target_strings{$basename}{$k} : "";
107 }
108 }
109
110 close T;
111
112 print "done\n";
113 }
114
115 close L;
116 }
117 }
118
119 close F;
120 }