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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#! /usr/bin/perl -w
#+##############################################################################
#
# manage_i18n.pl: manage translation files
#
# Copyright (C) 2003 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-##############################################################################
# This requires perl version 5 or higher
require 5.0;
use strict;
#use vars qw(
# $T2H_LANGUAGES
#);
use File::Copy;
use Data::Dumper;
select(STDERR);
$| = 1;
select(STDOUT);
$| = 1;
my $language;
my $i18n_dir = 'i18n'; # name of the directory containing the per language files
my $translation_file = 'translations.pl'; # file containing all the translations
my @known_languages = ('en', 'de', 'nl', 'es', 'no', 'pt', 'fr'); # The supported
# languages
#my $template = 'template';
my $template = 'en';
my $template_file = "$i18n_dir/$template";
my @source_files = ('texi2html.pl', 'texi2html.init', 'T2h_i18n.pm',
'examples/roff.init', 'examples/noheaders.init');
use vars qw(
$LANGUAGES
$T2H_OBSOLETE_STRINGS
);
# Strings not in code
my $template_strings =
{
'January' => '',
'February' => '',
'March' => '',
'April' => '',
'May' => '',
'June' => '',
'July' => '',
'August' => '',
'September' => '',
'October' => '',
'November' => '',
'December' => '',
'T2H_today' => '%s, %d %d',
};
# Handle per language files
$Data::Dumper::Sortkeys = 1;
if (@ARGV < 1)
{
die "Need at least one arg\n";
}
my $command = shift @ARGV;
sub update_language_file($);
die "No suitable $i18n_dir directory\n" unless (-d $i18n_dir and -r $i18n_dir);
sub get_languages
{
unless (opendir DIR, $i18n_dir)
{
die "Cannot open dir $i18n_dir: $!\n";
}
my @languages = grep {
! /^\./ &&
! /\.(bak|orig|old|dpkg-old|rpmnew|rpmsave)$/ &&
! /~$/ &&
! /^#.*#$/ &&
-f $i18n_dir . '/' . $_
} readdir DIR;
closedir DIR;
my @known = @known_languages;
foreach my $lang (@languages)
{
if (grep {$_ eq $lang} @known)
{
@known = grep {$_ ne $lang} @known;
}
else
{
warn "Remark: you could update the known languages array for `$lang'\n";
}
}
warn "Remark: the following known languages have no corresponding file: @known\n" if (@known);
return @languages;
}
sub manage_i18n_files($)
{
my $command = shift;
if ($command eq 'update')
{
update_i18n_files();
}
elsif ($command eq 'merge')
{
merge_i18n_files();
}
elsif ($command eq 'template')
{
update_template(@source_files);
}
elsif ($command eq 'all')
{
update_template(@source_files);
update_i18n_files();
merge_i18n_files();
}
else
{
warn "Unknown i18n command: $command\n";
}
exit 0;
}
sub merge_i18n_files
{
my @languages = get_languages();
die "No languages found\n" unless (@languages);
if (-f $translation_file)
{
unless (File::Copy::copy ($translation_file, "$translation_file.old"))
{
die "Error copying $translation_file to $translation_file.old\n";
}
}
#foreach my $lang ($template, @known_languages)
die "open $translation_file failed" unless (open (TRANSLATIONS, ">$translation_file"));
foreach my $lang (@languages)
{
my $file = "$i18n_dir/$lang";
next unless (-r $file);
unless (open (FILE, $file))
{
warn "open $file failed: $!\n";
return;
}
while (<FILE>)
{
print TRANSLATIONS;
}
close FILE;
}
}
sub update_language_hash($$$)
{
my $file = shift;
my $lang = shift;
my $reference = shift;
if (-f $file)
{
eval { require($file) ;};
if ($@)
{
warn "require $file failed: $@\n";
return;
}
unless (File::Copy::copy ($file, "$file.old"))
{
warn "Error copying $file to $file.old\n";
return;
}
if (!defined($LANGUAGES->{$lang}))
{
warn "LANGUAGES->{$lang} not defined in $file\n";
return;
}
}
if (!defined($T2H_OBSOLETE_STRINGS->{$lang}))
{
$T2H_OBSOLETE_STRINGS->{$lang} = {};
}
if (!defined($LANGUAGES->{$lang}))
{
$LANGUAGES->{$lang} = {};
}
foreach my $string (keys %{$LANGUAGES->{$lang}})
{
$T2H_OBSOLETE_STRINGS->{$lang}->{$string} = $LANGUAGES->{$lang}->{$string}
if (defined($LANGUAGES->{$lang}->{$string}) and ($LANGUAGES->{$lang}->{$string} ne ''));
}
$LANGUAGES->{$lang} = {};
foreach my $string (keys (%{$reference}))
{
if (exists($T2H_OBSOLETE_STRINGS->{$lang}->{$string}) and
defined($T2H_OBSOLETE_STRINGS->{$lang}->{$string}) and
($T2H_OBSOLETE_STRINGS->{$lang}->{$string} ne ''))
{
$LANGUAGES->{$lang}->{$string} = $T2H_OBSOLETE_STRINGS->{$lang}->{$string};
delete $T2H_OBSOLETE_STRINGS->{$lang}->{$string};
}
else
{
$LANGUAGES->{$lang}->{$string} = '';
}
}
return 1;
}
sub update_i18n_files
{
die "No suitable $i18n_dir directory\n" unless (-d $i18n_dir and -w $i18n_dir);
#my @languages = @known_languages;
my @languages = get_languages();
if (@ARGV)
{
@languages = ();
foreach my $lang (@ARGV)
{
unless (grep {$lang eq $_} @known_languages)
{
#warn "Unsupported language `$lang'\n";
#next;
warn "Remark: you could update the known languages array for `$lang'\n";
}
push (@languages, $lang) unless (grep {$lang eq $_} @languages);
}
}
unless (@languages)
{
warn "No languages to update\n" ;
return;
}
die "$template_file not readable\n" unless (-r $template_file);
eval { require($template_file) ;};
if ($@)
{
die "require $template_file failed: $@\n";
}
die "LANGUAGE->{'en'} undef after require $template_file\n" unless
(defined($LANGUAGES) and defined($LANGUAGES->{'en'}));
foreach my $string (keys(%$template_strings))
{
die "template string $string undef" unless (defined($LANGUAGES->{'en'}->{$string}));
}
foreach my $lang (@languages)
{
update_language_file($lang);
}
return 1;
}
sub update_language_file($)
{
my $lang = shift;
#unless (grep {$lang eq $_} @known_languages)
#{
# print STDERR "Unsupported language `$lang'\n";
# return;
#}
my $file = "$i18n_dir/$lang";
return unless (update_language_hash($file, $lang, $LANGUAGES->{'en'}));
unless (open (FILE, ">$file"))
{
warn "open $file failed: $!\n";
return;
}
print FILE "" . Data::Dumper->Dump([$LANGUAGES->{$lang}], [ "LANGUAGES->{'$lang'}" ]);
print FILE "\n";
print FILE Data::Dumper->Dump([$T2H_OBSOLETE_STRINGS->{$lang}], [ "T2H_OBSOLETE_STRINGS->{'$lang'}"]);
print FILE "\n";
print FILE "\n";
close FILE;
}
sub update_template (@)
{
my $source_strings = {};
foreach my $source_file (@_)
{
unless (-r $source_file)
{
warn "$source_file not readable\n";
next;
}
unless (open (FILE, "$source_file"))
{
warn "open $source_file failed: $!\n";
next;
}
my $line_nr = 0;
while (<FILE>)
{
$line_nr++;
my $string;
next if /^\s*#/;
while ($_)
{
if (defined($string))
{
if (s/^([^\\']*)(\\|')//)
{
$string .= $1 if (defined($1));
if ($2 eq "'")
{
$source_strings->{$string} = '' ;
$string = undef;
}
else
{
if (s/^(.)//)
{
#$string .= '\\' . $1;
$string .= $1;
}
else
{
warn "\\ at end of line, file $source_file, line nr $line_nr\n";
$source_strings->{$string} = '' ;
$string = undef;
}
}
}
else
{
warn "string not closed file $source_file, line nr $line_nr\n";
$source_strings->{$string} = '' ;
$string = undef;
}
}
elsif (s/^.*?&\$I\s*\('//)
{
$string = '';
}
else
{
last;
}
}
}
close FILE;
}
foreach my $string (keys (%$template_strings))
{
$source_strings->{$string} = $template_strings->{$string};
}
die unless(update_language_hash($template_file, 'en', $source_strings));
foreach my $string (keys(%$template_strings))
{ # use values in template_srings if it exists
$LANGUAGES->{'en'}->{$string} = $template_strings->{$string} if ($LANGUAGES->{'en'}->{$string} eq '');
}
unless (open (TEMPLATE, ">$template_file"))
{
die "open $template_file failed: $!\n";
}
print TEMPLATE "" . Data::Dumper->Dump([$LANGUAGES->{'en'}], [ "LANGUAGES->{'en'}" ]);
print TEMPLATE "\n";
if (keys(%{$T2H_OBSOLETE_STRINGS->{'en'}}))
{
print TEMPLATE Data::Dumper->Dump([$T2H_OBSOLETE_STRINGS->{'en'}], [ "T2H_OBSOLETE_STRINGS->{'en'}"]);
print TEMPLATE "\n";
print TEMPLATE "\n";
}
close TEMPLATE;
}
manage_i18n_files($command);
1;
|