'type=s' => \$type,
'exclude=s' => \@excludes,
'sep=s' => \$split_char,
- 'help' => \$help);
-
-help() if $help;
+ 'help' => sub { help() },
+) || usage();
# utiliser glob() pour tous les fichiers d'un repertoire
# Checks for missing input and string list arguments
-if( !defined(@in_files) || !defined($str_file) )
+if( !@in_files || !defined($str_file) )
{
usage("You must at least specify input and string list filenames.");
}
# Input is not a file nor a directory
if( !(-d $in_files[0]) && !(-f $in_files[0]))
{
- usage("Unknow input. Input must a file or a directory. (Symbolic links are not supported for the moment).");
+ usage("Unknown input. Input must a file or a directory. (Symbolic links are not supported for the moment.)");
}
elsif( -d $in_files[0] )
{
@in_files = ();
@in_files = &listfiles(\@in_files, $in_dir, $type, $recursive);
-
+
if(scalar(@in_files) == 0)
{
warn "Nothing to process in $in_dir matching *.$type.";
elsif( $action eq "update" )
{
# restores the string list from file
- %strhash = &restore_strhash(\%strhash, $str_file, $split_char);
+ %strhash = &restore_strhash(\%strhash, $str_file, $split_char,1);
# updates the list, adding new entries if any
%strhash = &update_strhash(\%strhash, \@in_files, $exclude_regex, $filter);
# saves the list to the file
}
# restores the string list from file
- %strhash = &restore_strhash(\%strhash, $str_file, $split_char);
+ %strhash = &restore_strhash(\%strhash, $str_file, $split_char,0);
# creates the new tmpl file using the new translation
&install_strhash(\%strhash, \@in_files, $in_dir, $out_dir);
}
my $fh_in; my $fh_out; # handles for input and output files
my $tmp_dir; # temporary directory name (used to create destination dir)
- $out_dir =~ s/\/$//; # chops the trailing / if any.
+ my $starttime = time();
+ $out_dir =~ s/\/$//; # chops the trailing / if any.
# Processes every entry found.
foreach my $file (@{$in_files})
{
# generates the name of the output file
my $out_file = $file;
+ my $out_file_tmp = $file.".tmp"; # used to check if file has changed or not.
if(!defined $in_dir)
{
close($fh_in);
exit(1);
}
-
+
# opens handle for output
- if( !open($fh_out, "> $out_file") )
+ if( !open($fh_out, "> $out_file_tmp") )
{
- warn "Can't write $out_file : $!\n";
+ warn "Can''t write $out_file : $!\n";
close($fh_in);
next;
}
-
- print "Generating $out_file...\n";
-
+ my $lines;
while(my $line = <$fh_in>)
{
- foreach my $text (sort {length($b) <=> length($a)} keys %{$strhash})
+ $lines.=$line;
+ }
+ foreach my $text (sort {length($b) <=> length($a) || uc($b) cmp uc($a) } keys %{$strhash})
+ {
+ # Test if the key has been translated
+ if( %{$strhash}->{$text} != 1)
{
- # Test if the key has been translated
- if( %{$strhash}->{$text} != 1 )
+ # Does the file contains text that needs to be changed ?
+ # escaping \|()[{}^$*+?.
+ my $subst = %{$strhash}->{$text};
+ $text =~ s/\\/\\\\/g;
+ $text =~ s/\//\\\//g;
+ $text =~ s/\|/\\\|/g;
+ $text =~ s/\(/\\\(/g;
+ $text =~ s/\)/\\\)/g;
+ $text =~ s/\[/\\\[/g;
+ $text =~ s/\]/\\\]/g;
+ $text =~ s/\{/\\\{/g;
+ $text =~ s/\}/\\\}/g;
+ $text =~ s/\^/\\\^/g;
+ $text =~ s/\$/\\\$/g;
+ $text =~ s/\*/\\\*/g;
+ $text =~ s/\+/\\\+/g;
+ $text =~ s/\?/\\\?/g;
+ $text =~ s/\./\\\./g;
+ if(%{$strhash}->{$text} ne "IGNORE" )
{
- # Does the line contains text that needs to be changed ?
- if( $line =~ /$text/ && %{$strhash}->{$text} ne "IGNORE")
+ if (%{$strhash}->{$text} =~ "LIMITED")
{
# changing text
- my $subst = %{$strhash}->{$text};
- $line =~ s/(\W)$text(\W)/$1$subst$2/g;
+ $subst =~ s/UNUSED;//;
+ $subst =~ s/^LIMITED;//g;
+ $lines =~ s/(.*)>$text(\W)/$1>$subst$2/g;
+ $lines =~ s/(.*) title="$text/$1 title="$subst/g;
+ $lines =~ s/(.*) alt="$text/$1 alt="$subst/g;
+ } else {
+ # changing text
+ $subst =~ s/UNUSED;//;
+ $lines =~ s/(\W)$text(\W)/$1$subst$2/g;
}
}
}
-
- # Writing the modified (or not) line to output
- printf($fh_out "%s", $line);
}
-
+ $lines =~ s/\<TMPL_(.*?)\>/\<\!-- TMPL_$1 --\>/g;
+ $lines =~ s/\<\/TMPL_(.*?)\>/\<\!-- \/TMPL_$1 --\>/g;
+ # Writing the modified (or not) file to output
+ printf($fh_out "%s", $lines);
close($fh_in);
close($fh_out);
+ # check if fh_out and previous fh_out has changed or not.
+ my $diff;
+ if(-f $out_file) {
+ $diff = `diff $out_file $out_file_tmp`;
+ } else {
+ $diff = "write it, it's new";#'
+ }
+ if ($diff) {
+ print "WRITING : $out_file\n";
+ unlink $out_file;
+ system("mv $out_file_tmp $out_file");
+ } else {
+ print "no changes in $out_file\n";
+ unlink $out_file_tmp;
+ }
}
+ my $timeneeded = time() - $starttime;
+ print "done in $timeneeded seconds\n";
+
}
########################################################
print "Processing $in...\n";
- # Creates a filehandle containing all the strings returned by
+ # 'Creates a filehandle containing all the strings returned by
# the plain text program extractor
open($fh, "$filter $in |") or print "$filter $in : $!";
next $in if !defined $fh;
while(my $str = <$fh>)
{
$str =~ s/[\n\r\f]+$//; # chomps the trailing \n (or <cr><lf> if file was edited with Windows)
- $str =~ s/^\s+//; # remove trailing blanks, ':' or '*'
- $str =~ s/\s*\**:*\s*$//;
+ $str =~ s/^[\s+:\(]*//; # remove useless characters
+ $str =~ s/[\s\*:\[*\(|\.,\)]*$//;#]
# the line begins with letter(s) followed by optional words and/or spaces
if($str =~ /^[ ]*[\w]+[ \w]*/)
{
# the line is not already in the list so add it
%{$strhash}->{$str}=1;
+ } else {
+ # remove UNUSED;
+ %{$strhash}->{$str} =~ s/^UNUSED;//;
}
}
}
sub restore_strhash
{
- my($strhash, $str_file, $split_char) = @_;
-
+ my($strhash, $str_file, $split_char, $detect_unused) = @_;
+
my $fh;
open($fh, "< $str_file") or die "$str_file : $!";
print "Restoring string list from $str_file...\n";
-
+
while( my $line = <$fh> )
{
chomp $line;
# extracts the two fields
- my ($original, $translated,$nb) = split(/$split_char/, $line, 3);
+ my ($original, $translated) = split(/$split_char/, $line, 2);
- if($translated ne "")
+ if($translated ne "*****")
{
# the key has been translated
%{$strhash}->{$original} = $translated;
# the key exist but has no translation.
%{$strhash}->{$original} = 1;
}
+ if ($detect_unused) {
+ %{$strhash}->{$original} = "UNUSED;".%{$strhash}->{$original} unless %{$strhash}->{$original}=~ /^UNUSED;/;
+ }
}
print "Writing string list to $str_file...\n";
- foreach my $str(sort {uc($a) cmp uc($b) || length($a) <=> length($b)} keys %{$strhash})
+ foreach my $str(sort {uc($a) cmp uc($b) || length($b) <=> length($a)} keys %{$strhash})
{
- if(%{$strhash}->{$str} != 1)
+ if(%{$strhash}->{$str}!=1)
{
printf($fh "%s%s%s\n", $str, $split_char, %{$strhash}->{$str});
}
else
{
- printf($fh "%s%s\n", $str, $split_char);
+ printf($fh "%s%s%s\n", $str, $split_char,"*****") unless ($str >0 || length($str) eq 1);
}
}
{
@{$in_files} = listfiles($in_files, "$dir/$tmp_file", $type);
}
- elsif( $tmp_file =~ /\.$type$/ )
+ elsif( $tmp_file =~ /\.$type$/ && !($tmp_file =~ /^\./))
{
push(@{$in_files}, "$dir/$tmp_file");
}
Specify the output directory to use when generating the translated
input files.
- -r or --recursive=
+ -r or --recursive
Use the recursive mode to process every entry in subdirectories.
Note: Symbolic links used to link directories are not supported.
EOF
exit(0);
}
+