3 # -n do not include custom_ scripts
4 # -v verbose mode, shows progress stuff
6 # this version of locles processes not only all required .pl files
7 # but also all parse_html_templated files.
15 use File::Slurp qw(slurp);
19 use List::Util qw(first);
23 $OUTPUT_AUTOFLUSH = 1;
32 my $basedir = "../..";
33 my $locales_dir = ".";
34 my $bindir = "$basedir/bin/mozilla";
35 my @progdirs = ( "$basedir/SL" );
36 my $dbupdir = "$basedir/sql/Pg-upgrade";
37 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
38 my $menufile = "menu.ini";
39 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
41 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
42 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
47 my ($top_dir_name) = @_;
54 tie my %dir_h, 'IO::Dir', $dir_name;
56 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
57 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
59 $finder->($_) for @sub_dirs;
62 $finder->($top_dir_name);
67 my @bindir_files = find_files($bindir);
68 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
69 my @customfiles = grep /_custom/, @bindir_files;
71 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
73 # put customized files into @customfiles
74 my (@menufiles, %dir_h);
78 @menufiles = ($menufile);
80 tie %dir_h, 'IO::Dir', $basedir;
81 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
82 unshift @menufiles, "$basedir/$menufile";
85 tie %dir_h, 'IO::Dir', $dbupdir;
86 my @dbplfiles = grep { /\.pl$/ } keys %dir_h;
88 tie %dir_h, 'IO::Dir', $dbupdir2;
89 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
91 # slurp the translations in
97 if (-f "$locales_dir/all") {
98 require "$locales_dir/all";
100 if (-f "$locales_dir/missing") {
101 require "$locales_dir/missing" ;
102 unlink "$locales_dir/missing";
104 if (-f "$locales_dir/lost") {
105 require "$locales_dir/lost";
106 unlink "$locales_dir/lost";
109 my $charset = slurp("$locales_dir/charset") || 'utf-8';
112 my %old_texts = %{ $self->{texts} || {} };
114 handle_file(@{ $_ }) for @progfiles;
115 handle_file($_, $dbupdir) for @dbplfiles;
116 handle_file($_, $dbupdir2) for @dbplfiles2;
117 scanmenu($_) for @menufiles;
121 file => "$locales_dir/all",
122 header => $ALL_HEADER,
123 data_name => '$self->{texts}',
124 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
127 # calc and generate missing
128 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
132 file => "$locales_dir/missing",
133 header => $MISSING_HEADER,
134 data_name => '$missing',
135 data_sub => sub { _print_line($_, '', @_) for @missing },
139 # calc and generate lost
140 while (my ($text, $translation) = each %old_texts) {
141 next if ($alllocales{$text});
142 push @lost, { 'text' => $text, 'translation' => $translation };
146 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
148 file => "$locales_dir/lost",
149 header => $LOST_HEADER,
151 data_name => '@lost',
153 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
158 my $trlanguage = slurp("$locales_dir/LANGUAGE");
161 search_unused_htmlfiles() if $opt_c;
163 my $count = scalar keys %alllocales;
164 my $notext = scalar @missing;
165 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
166 print "\n$trlanguage - ${per}%";
167 print " - $notext/$count missing" if $notext;
176 # These are all the texts to build the translations files.
177 # The file has the form of 'english text' => 'foreign text',
178 # you can add the translation in this file or in the 'missing' file
179 # run locales.pl from this directory to rebuild the translation files
181 $MISSING_HEADER = <<EOL;
182 # add the missing texts and run locales.pl to rebuild
184 $LOST_HEADER = <<EOL;
185 # The last 50 text strings, that have been removed.
186 # This file has been auto-generated by locales.pl. Please don't edit!
194 'no-custom-files' => \$opt_n,
195 'check-files' => \$opt_c,
196 'verbose' => \$opt_v,
208 pod2usage(-exitstatus => 0, -verbose => 2);
213 my $arg = shift @ARGV;
215 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
216 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
222 print "The locale directory '$arg' could not be found.\n";
226 } elsif (!-f 'all' || !-f 'LANGUAGE') {
227 print "locales.pl was not called from a locale/* subdirectory,\n"
228 . "and no locale directory name was given.\n";
234 my ($file, $dir) = @_;
235 print "\n$file" if $opt_v;
239 &scanfile("$dir/$file");
241 # scan custom_{module}.pl or {login}_{module}.pl files
242 foreach my $customfile (@customfiles) {
243 if ($customfile =~ /_$file/) {
244 if (-f "$dir/$customfile") {
245 &scanfile("$dir/$customfile");
252 foreach my $text (keys %$missing) {
253 if ($locale{$text} || $htmllocale{$text}) {
254 unless ($self->{texts}{$text}) {
255 $self->{texts}{$text} = $missing->{$text};
261 sub extract_text_between_parenthesis {
262 my ($fh, $line) = @_;
263 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
266 if (length($line) <= $pos) {
268 return ($text, "") unless ($line);
272 my $cur_char = substr($line, $pos, 1);
274 if (!$inside_string) {
275 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
276 $inside_string = substr($line, $pos + 2, 1);
279 } elsif ((length($line) >= ($pos + 2)) &&
280 (substr($line, $pos, 1) eq "q")) {
281 $inside_string = substr($line, $pos + 1, 1);
284 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
285 $inside_string = $cur_char;
287 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
288 return ($text, substr($line, $pos + 1));
293 $text .= '\\' unless $cur_char eq "'";
297 } elsif ($cur_char eq '\\') {
300 } elsif ($cur_char eq $inside_string) {
301 undef($inside_string);
314 my $dont_include_subs = shift;
315 my $scanned_files = shift;
320 $scanned_files = {} unless ($scanned_files);
321 return if ($scanned_files->{$file});
322 $scanned_files->{$file} = 1;
324 if (!defined $cached{$file}) {
326 return unless (-f "$file");
328 my $fh = new FileHandle;
329 open $fh, "$file" or die "$! : $file";
331 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
334 last if /^\s*__END__/;
338 # is this another file
339 if (/require\s+\W.*\.pl/) {
341 $newfile =~ s/require\s+\W//;
342 $newfile =~ s|bin/mozilla||;
343 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
344 } elsif (/use\s+SL::([\w:]*)/) {
347 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
350 # is this a template call?
351 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
352 my $newfile = "$basedir/templates/webpages/$1.html";
353 if (/parse_html_template2/) {
354 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
357 $cached{$file}{scanh}{$newfile} = 1;
360 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
369 my ($null, $country) = split /,/;
370 $country =~ s/^ +[\"\']//;
371 $country =~ s/[\"\'].*//;
377 # is it a submit button before $locale->
378 if (/$submitsearch/) {
380 if ($` !~ /locale->text/) {
382 $sub_line_no = $line_no;
386 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
391 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
394 # if there is no $ in the string record it
395 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
397 # this guarantees one instance of string
398 $cached{$file}{locale}{$string} = 1;
400 # this one is for all the locales
401 $cached{$file}{all}{$string} = 1;
403 # is it a submit button before $locale->
405 $cached{$file}{submit}{$string} = 1;
408 } elsif ($postmatch =~ />/) {
412 # exit loop if there are no more locales on this line
413 ($rc) = ($postmatch =~ /locale->text/);
415 if ( ($postmatch =~ />/)
416 || (!$found && ($sub_line_no != $line_no) && />/)) {
426 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
427 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
428 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
430 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
431 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
432 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
434 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
440 my $fh = new FileHandle;
441 open $fh, "$file" or die "$! : $file";
443 my @a = grep m/^\[/, <$fh>;
447 grep { s/(\[|\])//g } @a;
449 foreach my $item (@a) {
450 my @b = split /--/, $item;
451 foreach my $string (@b) {
453 $locale{$string} = 1;
454 $alllocales{$string} = 1;
460 sub unescape_template_string {
471 if (!defined $cached{$file}) {
472 my %plugins = ( 'loaded' => { }, 'needed' => { } );
474 open(IN, $file) || die $file;
479 while (my $line = <IN>) {
482 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
483 $plugins{loaded}->{$1} = 1;
486 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
488 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
491 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
492 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
493 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
494 (.*?) # Der zu übersetzende String ::Parameter $3::
495 (?<!\\)\2 # Ende des zu übersetzenden Strings
496 | # Start von Variante 2: [% '...' | $T8 %]
497 \[\% # Template-Start-Tag
498 [\-~#]? # Whitespace-Unterdrückung
499 \s* # Optional beliebig viele Whitespace
500 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
501 (.*?) # Der zu übersetzende String ::Parameter $5::
502 (?<!\\)\4 # Ende des zu übersetzenden Strings
503 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
504 (\$T8) # Filteraufruf ::Parameter $6::
505 .*? # Optionale Argumente für den Filter
507 [\-~#]? # Whitespace-Unterdrückung
508 \%\] # Template-Ende-Tag
511 my $module = $1 || $6;
512 my $string = $3 || $5;
513 print "Found filter >>>$string<<<\n" if $debug;
514 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
516 $string = unescape_template_string($string);
517 $cached{$file}{all}{$string} = 1;
518 $cached{$file}{html}{$string} = 1;
519 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
520 $plugins{needed}->{T8} = 1 if $module eq '$T8';
521 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
524 while ($line =~ m/\[\% # Template-Start-Tag
525 [\-~#]? # Whitespace-Unterdrückung
526 \s* # Optional beliebig viele Whitespace
527 (?: # Die erkannten Template-Direktiven
532 \s+ # Mindestens ein Whitespace
533 [\'\"]? # Anfang des Dateinamens
534 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
535 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
537 my $new_file_name = "$basedir/templates/webpages/$1.html";
538 $cached{$file}{scanh}{$new_file_name} = 1;
539 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
545 foreach my $plugin (keys %{ $plugins{needed} }) {
546 next if ($plugins{loaded}->{$plugin});
547 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
551 # copy back into global arrays
552 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
553 $locale{$_} = 1 for keys %{$cached{$file}{html}};
554 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
556 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
558 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
561 sub search_unused_htmlfiles {
562 my @unscanned_dirs = ('../../templates/webpages');
564 while (scalar @unscanned_dirs) {
565 my $dir = shift @unscanned_dirs;
567 foreach my $entry (<$dir/*>) {
569 push @unscanned_dirs, $entry;
571 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
572 print "W: unused HTML template: " . strip_base($entry) . "\n";
580 my $s = "$_[0]"; # Create a copy of the string.
583 $s =~ s|templates/webpages/||;
590 $val =~ s/(\'|\\$)/\\$1/g;
591 return "'" . $val . "'";
595 my $key = _single_quote(shift);
596 my $text = _single_quote(shift);
598 my $template = $params{template} || qq| %-29s => %s,\n|;
599 my $fh = $params{fh} || croak 'need filehandle in _print_line';
601 print $fh sprintf $template, $key, $text;
607 my $file = $params{file} || croak 'need filename in generate_file';
608 my $header = $params{header};
609 my $lines = $params{data_sub};
610 my $data_name = $params{data_name};
611 my @delim = split //, ($params{delim} || '{}');
613 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
615 $charset =~ s/\r?\n//g;
616 my $emacs_charset = lc $charset;
618 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
619 print $fh $header, "\n" if $header;
620 print $fh "$data_name = $delim[0]\n" if $data_name;
624 print $fh qq|$delim[1];\n\n1;\n|;
630 do { local ( @ARGV, $/ ) = $file; <> }
637 locales.pl - Collect strings for translation in Lx-Office
641 locales.pl [options] lang_code
644 -n, --no-custom-files Do not process files whose name contains "_"
645 -c, --check-files Run extended checks on HTML files
646 -v, --verbose Be more verbose
647 -h, --help Show this help
653 =item B<-n>, B<--no-custom-files>
655 Do not process files whose name contains "_", e.g. "custom_io.pl".
657 =item B<-c>, B<--check-files>
659 Run extended checks on the usage of templates. This can be used to
660 discover HTML templates that are never used as well as the usage of
661 non-existing HTML templates.
663 =item B<-v>, B<--verbose>
671 This script collects strings from Perl files, the menu.ini file and
672 HTML templates and puts them into the file "all" for translation.