summaryrefslogtreecommitdiffhomepage
path: root/build/i18n-scan.pl
blob: 03ab0292eaba69fcfb1a6d2d59a0fe4f16725846 (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
#!/usr/bin/perl

use strict;
use warnings;
use Text::Balanced qw(extract_codeblock);

@ARGV == 1 || die "Usage: $0 <source direcory>\n";


sub _parse
{
	my ( $code ) = @_;
	my ( $k, $v );
	
	if( $code =~ s/^<%:-?\s*(.+)\s*%>/$1/s )
	{
		my ( $key, @text ) = split /[\n\s]+/, $code;

		$k = $key;
		$v = join ' ', @text;
	}
	elsif( $code =~ s/^\(\s*(.+)\s*\)/$1/s )
	{
		if( $code =~ /^(?:"(\w+)"|'(\w+)')\s*,\s*(?:"(.+?)"|'(.+?)')/s )
		{
			$k = $1 || $2;
			$v = $3 || $4 || '';
			$v =~ s/\s+/ /sg;
		}
		elsif( $code =~ /^(?:"(\w+)"|'(\w+)')/ )
		{
			$k = $1 || $2;
			$v = '';
		}
		else
		{
			return ();
		}
	}
	else
	{
		return ();
	}

	$v =~ s/\\"/"/g;
	$v =~ s/"/\\"/g;
	
	return ( $k, $v );
}


if( open F, "find $ARGV[0] -type f -name '*.htm' -or -name '*.lua' |" )
{
	while( defined( my $file = readline F ) )
	{
		chomp $file;

		if( open S, "< $file" )
		{
			my $text = '';
			$text .= $_ foreach( readline S );

			while(
				$text =~ s/
					^ .*?
					(?:
						(?: translate f? | i18n )
						[\s\n]* ( \( )
					|
						( \<%: -? )
					)
				/$1 || $2/segx
			) {
				my $code;

				( $code, $text ) = extract_codeblock( $text, '', '^', '()' );
				if( ! $code ) {
					( $code, $text ) = extract_codeblock( $text, '', '^', '<>' );
				}

				if( ! $code ) {
					# Corner case:
					$text =~ s/(#[^\n]*)%>/$1\n%>/;
					( $code, $text ) = extract_codeblock( $text, '<>', '^' );
					if( ! $code ) {
						last;
					}
				}

				my ( $k, $v ) = _parse( $code );
				if( $k && defined($v) )
				{
					if( $v )
					{
						printf "#. %s\n", $v || $k;
					}

					printf "msgid \"%s\"\nmsgstr \"%s\"\n\n",
						$k, $v;
				}
			}

			close S;
		}
	}

	close F;
}