Skip to content

Commit 0c4b791

Browse files
authored
Refactor translation-tool.pl (#6317)
1 parent ee87e1e commit 0c4b791

File tree

1 file changed

+82
-62
lines changed

1 file changed

+82
-62
lines changed

translation-tool.pl

+82-62
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/perl -w
1+
#!/usr/bin/perl
22
# The MIT License
33
#
44
# Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number
@@ -50,52 +50,55 @@
5050
use File::Basename;
5151
use File::Find;
5252
use File::Path;
53+
use Getopt::Long;
5354

5455
my (
55-
$lang, $editor, $dir, $toiso, $toascii,
56-
$add, $remove, $reuse, $counter, $target
57-
) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./");
56+
$lang, $editor, $dir, $toiso, $toascii, $add,
57+
$remove, $reuse, $counter, $target, $help, $debug
58+
) = (undef, undef, "./", 0, 0, 0, 0, 0, 0, "./", 0, 0);
59+
60+
GetOptions(
61+
'help' => \$help,
62+
'lang=s' => \$lang,
63+
'editor' => \$editor,
64+
'dir=s' => \$dir,
65+
'to-iso' => \$toiso,
66+
'to-ascii' => \$toascii,
67+
'add' => \$add,
68+
'remove' => \$remove,
69+
'reuse' => \$reuse,
70+
'counter' => \$counter,
71+
'target=s' => \$target,
72+
'debug' => \$debug
73+
) or die("Error in command line arguments\n");
74+
75+
if ($help) {
76+
usage();
77+
exit(0);
78+
}
79+
80+
$add = 1 if ($editor);
81+
82+
if ($toiso && $toascii) {
83+
warn "You can't have --to-iso and --to-ascii options at the same time\n";
84+
usage();
85+
exit(1);
86+
}
87+
5888
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins,
5989
$countervalue)
6090
= (0, 0, 0, 0, 0, 0, 0, 1);
61-
## read arguments
62-
foreach (@ARGV) {
63-
if (/^--lang=(.*)$/) {
64-
$lang = $1;
65-
} elsif (/^--editor=(.*)$/) {
66-
$editor = $1;
67-
$add = 1;
68-
} elsif (/^--toiso$/ || /^--toiso=true$/) {
69-
$toiso = 1;
70-
$toascii = 0;
71-
} elsif (/^--toascii$/ || /^--toascii=true$/) {
72-
$toascii = 1;
73-
$toiso = 0;
74-
} elsif (/^--add$/ || /^--add=true$/) {
75-
$add = 1;
76-
} elsif (/^--remove$/ || /^--remove=true$/) {
77-
$remove = 1;
78-
} elsif (/^--reuse=(.*)$/) {
79-
$reuse = $1;
80-
} elsif (/^--counter$/ || /^--counter=true$/) {
81-
$counter = 1;
82-
} elsif (/^--target=(.*)$/) {
83-
$target = $1;
84-
} else {
85-
$dir = $_;
86-
}
87-
}
8891

8992
## language parameter is mandatory and shouldn't be 'en'
90-
if (!$lang || $lang eq "en") {
93+
unless (($lang) and ($lang ne 'en')) {
9194
usage();
92-
exit();
95+
exit(1);
9396
}
9497

95-
print STDERR "Finding files ...\n";
98+
print "Searching for files ...\n";
9699
## look for Message.properties and *.jelly files in the provided folder
97100
my @files = findTranslatableFiles($dir);
98-
print STDERR "Found " . (scalar keys @files) . " files\n";
101+
print 'Found ', scalar(@files), ' files', "\n";
99102

100103
## load a cache with keys already translated to utilize in the case the same key
101104
## is used
@@ -313,20 +316,36 @@ sub loadJellyFile {
313316
sub loadPropertiesFile {
314317
my $file = shift;
315318
my %ret;
316-
if (open(F, "$file")) {
317-
my ($cont, $key, $val) = (0, undef, undef);
318-
while (<F>) {
319-
s/[\r\n]+//;
320-
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
321-
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
322-
($key, $val) = (trim($1), trim($2));
323-
$ret{$key} = $val;
324-
}
325-
$cont = (/\\\s*$/) ? 1 : 0;
326-
}
327-
close(F);
319+
print "Trying to load $file... " if ($debug);
320+
321+
unless (-f $file) {
322+
print "file does not exist, skipping.\n" if ($debug);
323+
return %ret;
324+
}
325+
326+
print "done.\n" if ($debug);
327+
my $skip_comment = qr/^#/;
328+
open(my $in, '<', $file) or die "Cannot read $file: $!\n";
329+
my ($cont, $key, $val) = (0, undef, undef);
330+
331+
while (<$in>) {
332+
chomp;
333+
next if $_ eq '';
334+
next if $_ =~ $skip_comment;
335+
print 'Line: ', $_, "\n" if ($debug);
336+
s/[\r\n]+//;
328337
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
338+
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
339+
($key, $val) = (trim($1), trim($2));
340+
$ret{$key} = $val;
341+
}
342+
$cont = (/\\\s*$/) ? 1 : 0;
329343
}
344+
345+
close($in);
346+
# TODO: Use of uninitialized value $_ in pattern match (m//) at
347+
# ./translation-tool.pl line 345.
348+
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
330349
return %ret;
331350
}
332351

@@ -442,29 +461,30 @@ sub usage {
442461
443462
Usage: $0 --lang=xx [options] [dir]
444463
445-
dir: -> source folder for searching files (default current)
464+
dir: -> source folder for searching files (default is the current directory)
446465
options:
447-
--lang=xx -> language code to use (it is mandatory and it has to be different to English)
448-
--toiso=true|false -> convert files in UTF-8 to ISO-8859 (default false)
449-
--toascii=true|false -> convert files in UTF-8 to ASCII using the native2ascii command (default false)
450-
--add=true|false -> generate new files and add new keys to existing files (default false)
451-
--remove=true|false -> remove unused key/value pair for existing files (default false)
452-
--editor=command -> command to run over each updated file, implies add=true (default none)
453-
--reuse=folder -> load a cache with keys already translated in the folder provided in
454-
order to utilize them when the same key appears
455-
--counter=true -> to each translated key, unique value is added to easily identify match missing translation
456-
with value in source code (default false)
457-
--target=folder -> target folder for writing files
466+
--help -> print this help message and terminates the program with exit code 0.
467+
--lang=xx -> language code to use (it is mandatory and it has to be different to English)
468+
--to-iso -> optional, enables files in UTF-8 convertion to ISO-8859 if present
469+
--to-ascii -> optional, convert files in UTF-8 to ASCII using the native2ascii command if present
470+
--add -> optional, generate new files and add new keys to existing files if present
471+
--remove -> optional, remove unused key/value pair for existing files if present
472+
--editor=command -> command to run over each updated file, implies --add if present
473+
--reuse=folder -> optional, load a cache with keys already translated in the folder provided in
474+
order to utilize them when the same key appears if present
475+
--counter -> optional, to each translated key, unique value is added to easily identify match missing translation
476+
with value in source code if present
477+
--target=folder -> optional, target folder for writing files
478+
--debug -> optional, print debugging messages to STDOUT when they are available
458479
459480
Examples:
460481
- Look for Spanish files with incomplete keys in the 'main' folder,
461482
edit them with gedit, and finally convert them to ISO-8859
462-
$0 --lang=es --editor=gedit --toiso main
483+
$0 --lang=es --editor=gedit --to-iso main
463484
- Convert all Japanese files in the current folder encoded with UTF-8 to ASCII
464-
$0 --lang=ja --toascii .
485+
$0 --lang=ja --to-ascii .
465486
- Remove all orphaned keys from German files which are in the current file
466487
$0 --lang=de --remove .
467488
468489
";
469-
exit();
470490
}

0 commit comments

Comments
 (0)