-
Notifications
You must be signed in to change notification settings - Fork 38
/
xgettext
executable file
·155 lines (128 loc) · 3.95 KB
/
xgettext
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
154
155
#! /usr/bin/env perl
#
# Script to extract strings from Foswiki sources and templates
use strict;
use v5.14;
# force the use from the root
-d 'core'
or die(
"You may only run this utility from the root directory of Foswiki SVN checkout!"
);
use File::Spec;
use Try::Tiny;
use Foswiki::App ();
use Foswiki::Exception ();
$ENV{PATH};
my $cwd = `pwd`;
chomp($cwd);
unshift( @INC, File::Spec->catfile( $cwd, 'core/lib' ) );
eval "use Foswiki::I18N::Extract";
die($@) if $@;
eval "use Locale::Maketext::Lexicon";
die($@) if $@;
die('You need at least version 0.61 of Locale::Maketext::Lexicon to run this utility') unless $Locale::Maketext::Lexicon::VERSION >= 0.61;
die(
'The msgmerge command is required, it must be in your path. Try installing GNU gettext tools.'
) unless `which msgmerge`;
# config
my $localesDir = "core/locale";
my $potfile = "${localesDir}/Foswiki.pot";
my $newpotfile = $potfile . '.new';
# extract strings from the source code and append to the new potfile
#####################################################################
# driven from MANIFEST (picks up only files that are included in the release)
sub read_MANIFEST {
my ( $manifest, $prefix ) = @_;
my @files;
open( R, '<', $manifest ) || die "Could not open $manifest: $!";
foreach my $source (<R>) {
chomp($source);
if ( $source =~ m#^!include ../((\w+)/.*)$# ) {
my $included = $2;
my $included_manifest = $1 . '/MANIFEST';
print "I: including: $included, from $included_manifest \n";
push( @files, read_MANIFEST( $included_manifest, "$included/" ) );
}
my @fields = split( /\s/, $source );
$source = $fields[0];
if (
$source =~ m/^bin\/[a-z]/
|| ( $source =~ m/^lib\/.*\.pm$/
&& ( !( $source =~ m/^lib\/CPAN\/.*\.pm$/ ) ) )
|| $source =~ m/^templates\/.*\.tmpl$/
|| $source =~ m/^data\/.*\.txt$/
)
{
my $file = $prefix . $source;
if ( -e $file ) {
push( @files, $prefix . $source );
}
else {
print "W: $file is listed in MANIFEST and does not exist!\n";
}
}
}
close(R);
return @files;
}
sub extract {
my ( $target, @sources ) = @_;
print "I: Extracting strings into $target.\n";
local %ENV = %ENV;
my $app = Foswiki::App->new( env => \%ENV );
my $extractor = $app->create('Foswiki::I18N::Extract');
die(
"Could not create an extractor. Is Locale::Maketext installed? (see above messages)"
) unless $extractor;
my $progress = undef;
eval "use Term::ProgressBar;";
if ($@) {
print
"W: (You could have a nice progress bar if you had Term::ProgressBar installed.)\n";
}
else {
$progress = Term::ProgressBar->new( scalar(@sources) );
}
my $counter = 0;
foreach my $file (@sources) {
$extractor->extract_file($file);
$counter++;
$progress->update($counter) if $progress;
}
$extractor->compile();
$extractor->write_po($target);
}
sub merge {
my ( $old, $new ) = @_;
print("I: Merging new strings in $new into $old.\n");
system("msgmerge --update $old $new");
}
# read the top-level MANIFEST, and any other included by it
my @sources = read_MANIFEST( 'core/lib/MANIFEST', '' );
# handle --list: only list files to be scanned
if ( $ARGV[0] eq '--list' ) {
foreach my $file (@sources) {
print $file, "\n";
}
exit 0;
}
# extract the strings to the template POT file.
try {
extract( $newpotfile, @sources );
}
catch {
say STDERR Foswiki::Exception::errorStr($_);
exit 1;
};
# merge with old POT file
if ( -e $potfile ) {
merge( $potfile, $newpotfile );
}
else {
system("cp $newpotfile $potfile");
}
# merge with existing translations:
my @translations = <${localesDir}/*.po>;
merge( $_, $potfile ) for @translations;
# cleanup
system("rm -rf $newpotfile");