From 049fe2ce56b869aa807e00825cbd86615ea808fa Mon Sep 17 00:00:00 2001 From: Damian Szczepanik Date: Sat, 5 Jan 2019 22:14:07 +0100 Subject: [PATCH] Improvements for translation-tool.pl After several tests of 3bfe97e40f675d9ecd47d120fb9fe3d0576ab5a7 I found that library does not support: - proper using characters stored as \uXXXX so then translated files have values without \u - keys with spaces such as `jenkins\ title` is not supported So then I decided to revert last commit and add a little improvement but still there are problems with - values which ends with `\` so value is provided as multiline - lines with more than single `=` character that are already translated --- translation-tool.pl | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/translation-tool.pl b/translation-tool.pl index 439041a1c3..c062415cc4 100755 --- a/translation-tool.pl +++ b/translation-tool.pl @@ -43,8 +43,6 @@ use strict; use File::Find; -# to install the module: cpan install Config::Properties -use Config::Properties; my ($lang, $editor, $dir, $toiso, $toascii, $add, $remove, $reuse, $counter) = (undef, undef, "./", undef, undef, undef, undef, undef, undef); my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins, $countervalue) = (0, 0, 0, 0, 0, 0, 0, 1); @@ -78,13 +76,13 @@ if (!$lang || $lang eq "en") { exit(); } -print STDERR "\rFinding files ..."; +print STDERR "Finding files ...\n"; ## look for Message.properties and *.jelly files in the provided folder my @files = findTranslatableFiles($dir); +print STDERR "Found ".(scalar keys @files)." files\n"; ## load a cache with keys already translated to utilize in the case the same key is used my %cache = loadAllTranslatedKeys($reuse, $lang) if ($reuse && -e $reuse); -print STDERR "\r "; ## process each file foreach (@files) { @@ -277,13 +275,23 @@ sub loadJellyFile { # Fill a hash with key/value pairs from a .properties file sub loadPropertiesFile { - my $filename = shift; - my $properties = Config::Properties->new(); - - open my $file, '<', $filename or die "unable to open property file: ". $filename; - $properties->load($file); - close(F); - return $properties->properties; + my $file = shift; + my %ret; + if (open(F, "$file")) { + my ($cont, $key, $val) = (0, undef, undef); + while () { + s/[\r\n]+//; + $ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/); + if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) { + ($key, $val) = (trim($1), trim($2)); + $ret{$key}=$val; + } + $cont = (/\\\s*$/) ? 1 : 0; + } + close(F); + $ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/); + } + return %ret; } # remove unused keys from a file