|
1 |
| -#!/usr/bin/perl -w |
| 1 | +#!/usr/bin/perl |
2 | 2 | # The MIT License
|
3 | 3 | #
|
4 | 4 | # Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number
|
|
50 | 50 | use File::Basename;
|
51 | 51 | use File::Find;
|
52 | 52 | use File::Path;
|
| 53 | +use Getopt::Long; |
53 | 54 |
|
54 | 55 | 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 | + |
58 | 88 | my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins,
|
59 | 89 | $countervalue)
|
60 | 90 | = (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 |
| -} |
88 | 91 |
|
89 | 92 | ## language parameter is mandatory and shouldn't be 'en'
|
90 |
| -if (!$lang || $lang eq "en") { |
| 93 | +unless (($lang) and ($lang ne 'en')) { |
91 | 94 | usage();
|
92 |
| - exit(); |
| 95 | + exit(1); |
93 | 96 | }
|
94 | 97 |
|
95 |
| -print STDERR "Finding files ...\n"; |
| 98 | +print "Searching for files ...\n"; |
96 | 99 | ## look for Message.properties and *.jelly files in the provided folder
|
97 | 100 | my @files = findTranslatableFiles($dir);
|
98 |
| -print STDERR "Found " . (scalar keys @files) . " files\n"; |
| 101 | +print 'Found ', scalar(@files), ' files', "\n"; |
99 | 102 |
|
100 | 103 | ## load a cache with keys already translated to utilize in the case the same key
|
101 | 104 | ## is used
|
@@ -313,20 +316,36 @@ sub loadJellyFile {
|
313 | 316 | sub loadPropertiesFile {
|
314 | 317 | my $file = shift;
|
315 | 318 | 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]+//; |
328 | 337 | $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; |
329 | 343 | }
|
| 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]*$/); |
330 | 349 | return %ret;
|
331 | 350 | }
|
332 | 351 |
|
@@ -442,29 +461,30 @@ sub usage {
|
442 | 461 |
|
443 | 462 | Usage: $0 --lang=xx [options] [dir]
|
444 | 463 |
|
445 |
| - dir: -> source folder for searching files (default current) |
| 464 | + dir: -> source folder for searching files (default is the current directory) |
446 | 465 | 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 |
458 | 479 |
|
459 | 480 | Examples:
|
460 | 481 | - Look for Spanish files with incomplete keys in the 'main' folder,
|
461 | 482 | 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 |
463 | 484 | - 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 . |
465 | 486 | - Remove all orphaned keys from German files which are in the current file
|
466 | 487 | $0 --lang=de --remove .
|
467 | 488 |
|
468 | 489 | ";
|
469 |
| - exit(); |
470 | 490 | }
|
0 commit comments