summaryrefslogtreecommitdiffhomepage
path: root/build/i18n-lua2po.pl
blob: a6eb26cafdc91711134eb770dbfb343051f1eec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl

@ARGV >= 2 || die "Usage: $0 <source-dir> <dest-dir> [<target-language>]\n";

my $source_dir  = shift @ARGV;
my $target_dir  = shift @ARGV;
my $target_lang = shift @ARGV;
my $master_lang = "en";


if( ! -d "$target_dir/" . ( $target_lang || 'templates' ) )
{
	system('mkdir', '-p', "$target_dir/" . ( $target_lang || 'templates' ));
}


my %target_strings;

if( $target_lang && open F, "find $source_dir -path '*/luasrc/i18n/*' -name '*.$target_lang.lua' |" )
{
	while( chomp( my $file = readline F ) )
	{
		if( open L, "< $file" )
		{
			my ( $basename ) = $file =~ m{.+/([^/]+)\.[\w\-]+\.lua$};
			$target_strings{$basename} = { };

			while( chomp( my $entry = readline L ) )
			{
				my ( $k, $v );
				if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
				{
					( $k, $v ) = ( $1, $2 );
				}
				elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
				{
					( $k, $v ) = ( $1, $2 );
				}
				elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
				{
					( $k, $v ) = ( $1, $2 );
				}
				
				if( $k && $v )
				{
					$v =~ s/"/\\"/g;
					$v =~ s/\\\\"/\\"/g;
					$target_strings{$basename}{$k} = $v;
				}
			}

			close L;
		}
	}

	close F;
}


if( open F, "find . -path '*/luasrc/i18n/*' -name '*.$master_lang.lua' |" )
{
	my $destfile = sprintf '%s/%s/%%s.%s',
		$target_dir,
		$target_lang || 'templates',
		$target_lang ? 'po' : 'pot'
	;

	while( chomp( my $file = readline F ) )
	{
		if( open L, "< $file" )
		{
			my ( $basename ) = $file =~ m{.+/([^/]+)\.\w+\.lua$};
			my $filename = sprintf $destfile, $basename;

			if( open T, "> $filename" )
			{
				printf "Generating %-40s ", $filename;

				printf T "#  %s.%s\n#  generated from %s\n\nmsgid \"\"\n" .
				         "msgstr \"Content-Type: text/plain; charset=UTF-8\"\n\n",
					$basename, $target_lang ? 'po' : 'pot', $file;
		
				while( chomp( my $entry = readline L ) )
				{
					my ( $k, $v );
					if( $entry =~ /^\s*(\w+)\s*=\s*\[\[(.+)\]\]/ )
					{
						( $k, $v ) = ( $1, $2 );
					}
					elsif( $entry =~ /^\s*(\w+)\s*=\s*'(.+)'/ )
					{
						( $k, $v ) = ( $1, $2 );
					}
					elsif( $entry =~ /^\s*(\w+)\s*=\s*"(.+)"/ )
					{
						( $k, $v ) = ( $1, $2 );
					}
				
					if( $k && $v )
					{
						$v =~ s/"/\\"/g;
						$v =~ s/\\\\"/\\"/g;
						printf T "#: %s:%d\n#. %s\nmsgid \"%s\"\nmsgstr \"%s\"\n\n",
							$file, $., $v, $k,
							( $target_strings{$basename} && $target_strings{$basename}{$k} )
								? $target_strings{$basename}{$k} : $v;
					}
				}
				
				close T;
				
				print "done\n";
			}

			close L;
		}
	}

	close F;
}