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

@ARGV <= 2 || die "Usage: $0 [<po directory>] [<file pattern>]\n";

my $source  = shift @ARGV;
my $pattern = shift @ARGV || '*.po';

sub read_header
{
	my $file = shift || return;
	local $/;

	open P, "< $file" || die "open(): $!";
	my $data = readline P;
	close P;

	$data =~ /
		^ (
		msgid \s "" \n
		msgstr \s "" \n
		(?: " [^\n]+ " \n )+
		\n )
	/mx;

	return $1;
}

sub write_header
{
	my $file = shift || return;
	my $head = shift || return;
	local $/;

	open P, "< $file" || die "open(): $!";
	my $data = readline P;
	close P;

	$data =~ s/
		^ (
		msgid \s "" \n
		msgstr \s "" \n
		(?: " [^\n]+ " \n )+
		\n )
	/$head/mx;

	open P, "> $file" || die "open(): $!";
	print P $data;
	close P;
}

my @dirs;

if( ! $source )
{
	@dirs = glob("./*/*/po");
}
else
{
	@dirs = ( $source );
}

foreach my $dir (@dirs)
{
	if( open F, "find $dir -type f -name '$pattern' |" )
	{
		while( chomp( my $file = readline F ) )
		{
			my ( $basename ) = $file =~ m{.+/([^/]+)\.po$};
		
			if( -f "$dir/templates/$basename.pot" )
			{
				my $head = read_header($file);

				printf "Updating %-40s", $file;
				system("msgmerge", "-U", "-N", $file, "$dir/templates/$basename.pot");

				write_header($file, $head);
			}
		}

		close F;
	}
}