Refactor `translation-tool.pl` (#6317)

This commit is contained in:
Alceu Rodrigues de Freitas Junior 2022-03-06 13:11:38 -03:00 committed by GitHub
parent ee87e1e869
commit 0c4b79128d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 83 additions and 63 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/perl -w #!/usr/bin/perl
# The MIT License # The MIT License
# #
# Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number # Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number
@ -50,52 +50,55 @@ use strict;
use File::Basename; use File::Basename;
use File::Find; use File::Find;
use File::Path; use File::Path;
use Getopt::Long;
my ( my (
$lang, $editor, $dir, $toiso, $toascii, $lang, $editor, $dir, $toiso, $toascii, $add,
$add, $remove, $reuse, $counter, $target $remove, $reuse, $counter, $target, $help, $debug
) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./"); ) = (undef, undef, "./", 0, 0, 0, 0, 0, 0, "./", 0, 0);
GetOptions(
'help' => \$help,
'lang=s' => \$lang,
'editor' => \$editor,
'dir=s' => \$dir,
'to-iso' => \$toiso,
'to-ascii' => \$toascii,
'add' => \$add,
'remove' => \$remove,
'reuse' => \$reuse,
'counter' => \$counter,
'target=s' => \$target,
'debug' => \$debug
) or die("Error in command line arguments\n");
if ($help) {
usage();
exit(0);
}
$add = 1 if ($editor);
if ($toiso && $toascii) {
warn "You can't have --to-iso and --to-ascii options at the same time\n";
usage();
exit(1);
}
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins, my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins,
$countervalue) $countervalue)
= (0, 0, 0, 0, 0, 0, 0, 1); = (0, 0, 0, 0, 0, 0, 0, 1);
## read arguments
foreach (@ARGV) {
if (/^--lang=(.*)$/) {
$lang = $1;
} elsif (/^--editor=(.*)$/) {
$editor = $1;
$add = 1;
} elsif (/^--toiso$/ || /^--toiso=true$/) {
$toiso = 1;
$toascii = 0;
} elsif (/^--toascii$/ || /^--toascii=true$/) {
$toascii = 1;
$toiso = 0;
} elsif (/^--add$/ || /^--add=true$/) {
$add = 1;
} elsif (/^--remove$/ || /^--remove=true$/) {
$remove = 1;
} elsif (/^--reuse=(.*)$/) {
$reuse = $1;
} elsif (/^--counter$/ || /^--counter=true$/) {
$counter = 1;
} elsif (/^--target=(.*)$/) {
$target = $1;
} else {
$dir = $_;
}
}
## language parameter is mandatory and shouldn't be 'en' ## language parameter is mandatory and shouldn't be 'en'
if (!$lang || $lang eq "en") { unless (($lang) and ($lang ne 'en')) {
usage(); usage();
exit(); exit(1);
} }
print STDERR "Finding files ...\n"; print "Searching for files ...\n";
## look for Message.properties and *.jelly files in the provided folder ## look for Message.properties and *.jelly files in the provided folder
my @files = findTranslatableFiles($dir); my @files = findTranslatableFiles($dir);
print STDERR "Found " . (scalar keys @files) . " files\n"; print 'Found ', scalar(@files), ' files', "\n";
## load a cache with keys already translated to utilize in the case the same key ## load a cache with keys already translated to utilize in the case the same key
## is used ## is used
@ -313,9 +316,23 @@ sub loadJellyFile {
sub loadPropertiesFile { sub loadPropertiesFile {
my $file = shift; my $file = shift;
my %ret; my %ret;
if (open(F, "$file")) { print "Trying to load $file... " if ($debug);
unless (-f $file) {
print "file does not exist, skipping.\n" if ($debug);
return %ret;
}
print "done.\n" if ($debug);
my $skip_comment = qr/^#/;
open(my $in, '<', $file) or die "Cannot read $file: $!\n";
my ($cont, $key, $val) = (0, undef, undef); my ($cont, $key, $val) = (0, undef, undef);
while (<F>) {
while (<$in>) {
chomp;
next if $_ eq '';
next if $_ =~ $skip_comment;
print 'Line: ', $_, "\n" if ($debug);
s/[\r\n]+//; s/[\r\n]+//;
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/); $ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) { if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
@ -324,9 +341,11 @@ sub loadPropertiesFile {
} }
$cont = (/\\\s*$/) ? 1 : 0; $cont = (/\\\s*$/) ? 1 : 0;
} }
close(F);
close($in);
# TODO: Use of uninitialized value $_ in pattern match (m//) at
# ./translation-tool.pl line 345.
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/); $ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
}
return %ret; return %ret;
} }
@ -442,29 +461,30 @@ Translation Tool for Jenkins
Usage: $0 --lang=xx [options] [dir] Usage: $0 --lang=xx [options] [dir]
dir: -> source folder for searching files (default current) dir: -> source folder for searching files (default is the current directory)
options: options:
--help -> print this help message and terminates the program with exit code 0.
--lang=xx -> language code to use (it is mandatory and it has to be different to English) --lang=xx -> language code to use (it is mandatory and it has to be different to English)
--toiso=true|false -> convert files in UTF-8 to ISO-8859 (default false) --to-iso -> optional, enables files in UTF-8 convertion to ISO-8859 if present
--toascii=true|false -> convert files in UTF-8 to ASCII using the native2ascii command (default false) --to-ascii -> optional, convert files in UTF-8 to ASCII using the native2ascii command if present
--add=true|false -> generate new files and add new keys to existing files (default false) --add -> optional, generate new files and add new keys to existing files if present
--remove=true|false -> remove unused key/value pair for existing files (default false) --remove -> optional, remove unused key/value pair for existing files if present
--editor=command -> command to run over each updated file, implies add=true (default none) --editor=command -> command to run over each updated file, implies --add if present
--reuse=folder -> load a cache with keys already translated in the folder provided in --reuse=folder -> optional, load a cache with keys already translated in the folder provided in
order to utilize them when the same key appears order to utilize them when the same key appears if present
--counter=true -> to each translated key, unique value is added to easily identify match missing translation --counter -> optional, to each translated key, unique value is added to easily identify match missing translation
with value in source code (default false) with value in source code if present
--target=folder -> target folder for writing files --target=folder -> optional, target folder for writing files
--debug -> optional, print debugging messages to STDOUT when they are available
Examples: Examples:
- Look for Spanish files with incomplete keys in the 'main' folder, - Look for Spanish files with incomplete keys in the 'main' folder,
edit them with gedit, and finally convert them to ISO-8859 edit them with gedit, and finally convert them to ISO-8859
$0 --lang=es --editor=gedit --toiso main $0 --lang=es --editor=gedit --to-iso main
- Convert all Japanese files in the current folder encoded with UTF-8 to ASCII - Convert all Japanese files in the current folder encoded with UTF-8 to ASCII
$0 --lang=ja --toascii . $0 --lang=ja --to-ascii .
- Remove all orphaned keys from German files which are in the current file - Remove all orphaned keys from German files which are in the current file
$0 --lang=de --remove . $0 --lang=de --remove .
"; ";
exit();
} }