summaryrefslogtreecommitdiffstats
path: root/config-db/tools/extractTranslations.pl
blob: 57a819a57cc4fd9832a8fba2ad4a09eceaad924d (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /usr/bin/perl
#
# extractTranslations.pl - OpenSLX-script to extract translatable strings from
#                          other scripts and modules.
#
# (c) 2006 - OpenSLX.com
#
# Oliver Tappe <ot@openslx.com>
#
use strict;

use File::Find;
use Getopt::Long;
use Pod::Usage;

my (
	$helpReq,
	$update,
	$verbose,
	$versionReq,

	%translatableStrings,
);

GetOptions(
	'help|?' => \$helpReq,
	'update' => \$update,
	'verbose' => \$verbose,
	'version' => \$versionReq,
) or pod2usage(2);
pod2usage(1) if $helpReq;
if ($versionReq) {
	system('slxversion');
	exit 1;
}

use FindBin;
chdir("$FindBin::RealBin/../../config-db")
	or die "unable to find 'config-db'-folder (should be '..' from script)";
		# always start in 'config-db' - folder

find(\&ExtractTrStrings, '.');

if ($update) {
	find(\&UpdateTrModule, 'OpenSLX/Translations');
} else {
	foreach my $tr (sort {lc($a) cmp lc($b)} keys %translatableStrings) {
		print "\tqq{$tr}\n\t\t=> qq{$tr}\n";
	}
}

sub ExtractTrStrings
{
	$File::Find::prune = 1 if ($_ eq '.svn' || $_ eq 'Translations');
	return if -d;
	print "$File::Find::name...\n" if $verbose;
	open(F, "< $_")
		or die "could not open file $_ for reading!";
	$/ = undef;
	my $text = <F>;
	close(F);
	while($text =~ m[_tr\s*\(\s*('[^']+'|\"[^"]+\")\s*(?:,.+?)?\)\s*;]gos) {
		my $tr = substr($1, 1, -1);
		$translatableStrings{$tr} = $tr;
		print "\t$tr\n" if $verbose;
	}
}

sub UpdateTrModule
{
	$File::Find::prune = 1 if ($_ eq '.svn');
	return if -d;
	print "updating $File::Find::name...\n";
	my $trModule = $_;
	my $useKeyAsTranslation = ($trModule eq 'posix.pm');
	open(F, "< $trModule")
		or die "could not open file $trModule for reading!";
	$/ = undef;
	my $text = <F>;
	close(F);
	if ($text !~ m[%translations\s*=\s*\(\s*(.+)\s*\);]os) {
		print "\t*** No translations found (?!?) file will be skipped! ***\n";
		return;
	}
	my %translations;
	if (!eval "$&") {
		print "\t*** Something No translations found (?!?) file will be skipped! ***\n";
	my $updatedTranslations = "%translations = (\n";
	foreach my $tr (sort {lc($a) cmp lc($b)} keys %translatableStrings) {
		if (!exists $translations{$tr} && $useKeyAsTranslation) {
			# POSIX language: use key as translation:
			$updatedTranslations
				.= "\tqq{$tr}\n\t\t => qq{$tr},\n\n";
		} else {
			# use existing translation for key:
			$updatedTranslations
				.= "\tqq{$tr}\n\t\t => qq{$translations{$tr}},\n\n";
		}
	}
	$text =~ s[%translations\s*=\s*\(\s*(.+)\s*\);]
			  [$updatedTranslations);\n]os;
	chomp $text;
	open(F, "> $trModule")
		or die "could not open file $trModule for writing!";
	print F "$text\n";
	close(F);
}

__END__

=head1 NAME

extractTranslations.pl - OpenSLX-script to extract translatable strings from
other scripts and modules.

=head1 SYNOPSIS

extractTranslations.pl [options] [path]

  Options:
      --help              brief help message
      --update            update the OpenSLX locale modules
      --verbose           show what's going on
      --version           show version

=head1 OPTIONS

=over 8

=item B<--help>

Prints a brief help message and exits.

=item B<--update>

Integrates the found translatable strings into all OpenSLX locale modules, i.e.
every module will be updated with the found strings, existing translations
will not be changed (unless the corresponding key doesn't exist anymore, in
which case they will be removed).

=item B<--verbose>

Prints information about what's going on during execution of the script.

=item B<--version>

Prints the version and exits.

=back

=head1 DESCRIPTION

B<extractTranslations.pl> can be used to