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 = grep { /.*?_$menufile$/ } keys %dir_h;
82 unshift @menufiles, $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;
120 file => "$locales_dir/all",
121 header => $ALL_HEADER,
122 data_name => '$self->{texts}',
123 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
126 # calc and generate missing
127 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
131 file => "$locales_dir/missing",
132 header => $MISSING_HEADER,
133 data_name => '$missing',
134 data_sub => sub { _print_line($_, '', @_) for @missing },
138 # calc and generate lost
139 while (my ($text, $translation) = each %old_texts) {
140 next if ($alllocales{$text});
141 push @lost, { 'text' => $text, 'translation' => $translation };
145 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
147 file => "$locales_dir/lost",
148 header => $LOST_HEADER,
150 data_name => '@lost',
152 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
157 my $trlanguage = slurp("$locales_dir/LANGUAGE");
160 search_unused_htmlfiles() if $opt_c;
162 my $count = scalar keys %alllocales;
163 my $notext = scalar @missing;
164 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
165 print "\n$trlanguage - ${per}%";
166 print " - $notext/$count missing" if $notext;
175 # These are all the texts to build the translations files.
176 # The file has the form of 'english text' => 'foreign text',
177 # you can add the translation in this file or in the 'missing' file
178 # run locales.pl from this directory to rebuild the translation files
180 $MISSING_HEADER = <<EOL;
181 # add the missing texts and run locales.pl to rebuild
183 $LOST_HEADER = <<EOL;
184 # The last 50 text strings, that have been removed.
185 # This file has been auto-generated by locales.pl. Please don't edit!
193 'no-custom-files' => \$opt_n,
194 'check-files' => \$opt_c,
195 'verbose' => \$opt_v,
207 pod2usage(-exitstatus => 0, -verbose => 2);
212 my $arg = shift @ARGV;
214 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
215 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
221 print "The locale directory '$arg' could not be found.\n";
225 } elsif (!-f 'all' || !-f 'LANGUAGE') {
226 print "locales.pl was not called from a locale/* subdirectory,\n"
227 . "and no locale directory name was given.\n";
233 my ($file, $dir) = @_;
234 print "\n$file" if $opt_v;
238 &scanfile("$dir/$file");
240 # scan custom_{module}.pl or {login}_{module}.pl files
241 foreach my $customfile (@customfiles) {
242 if ($customfile =~ /_$file/) {
243 if (-f "$dir/$customfile") {
244 &scanfile("$dir/$customfile");
251 foreach my $text (keys %$missing) {
252 if ($locale{$text} || $htmllocale{$text}) {
253 unless ($self->{texts}{$text}) {
254 $self->{texts}{$text} = $missing->{$text};
260 sub extract_text_between_parenthesis {
261 my ($fh, $line) = @_;
262 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
265 if (length($line) <= $pos) {
267 return ($text, "") unless ($line);
271 my $cur_char = substr($line, $pos, 1);
273 if (!$inside_string) {
274 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
275 $inside_string = substr($line, $pos + 2, 1);
278 } elsif ((length($line) >= ($pos + 2)) &&
279 (substr($line, $pos, 1) eq "q")) {
280 $inside_string = substr($line, $pos + 1, 1);
283 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
284 $inside_string = $cur_char;
286 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
287 return ($text, substr($line, $pos + 1));
292 $text .= '\\' unless $cur_char eq "'";
296 } elsif ($cur_char eq '\\') {
299 } elsif ($cur_char eq $inside_string) {
300 undef($inside_string);
313 my $dont_include_subs = shift;
314 my $scanned_files = shift;
319 $scanned_files = {} unless ($scanned_files);
320 return if ($scanned_files->{$file});
321 $scanned_files->{$file} = 1;
323 if (!defined $cached{$file}) {
325 return unless (-f "$file");
327 my $fh = new FileHandle;
328 open $fh, "$file" or die "$! : $file";
330 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
333 last if /^\s*__END__/;
337 # is this another file
338 if (/require\s+\W.*\.pl/) {
340 $newfile =~ s/require\s+\W//;
341 $newfile =~ s|bin/mozilla||;
342 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
343 } elsif (/use\s+SL::([\w:]*)/) {
346 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
349 # is this a template call?
350 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
351 my $newfile = "$basedir/templates/webpages/$1.html";
352 if (/parse_html_template2/) {
353 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
356 $cached{$file}{scanh}{$newfile} = 1;
359 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
368 my ($null, $country) = split /,/;
369 $country =~ s/^ +[\"\']//;
370 $country =~ s/[\"\'].*//;
376 # is it a submit button before $locale->
377 if (/$submitsearch/) {
379 if ($` !~ /locale->text/) {
381 $sub_line_no = $line_no;
385 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
390 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
393 # if there is no $ in the string record it
394 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
396 # this guarantees one instance of string
397 $cached{$file}{locale}{$string} = 1;
399 # this one is for all the locales
400 $cached{$file}{all}{$string} = 1;
402 # is it a submit button before $locale->
404 $cached{$file}{submit}{$string} = 1;
407 } elsif ($postmatch =~ />/) {
411 # exit loop if there are no more locales on this line
412 ($rc) = ($postmatch =~ /locale->text/);
414 if ( ($postmatch =~ />/)
415 || (!$found && ($sub_line_no != $line_no) && />/)) {
425 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
426 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
427 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
429 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
430 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
431 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
433 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
439 my $fh = new FileHandle;
440 open $fh, "$file" or die "$! : $file";
442 my @a = grep m/^\[/, <$fh>;
446 grep { s/(\[|\])//g } @a;
448 foreach my $item (@a) {
449 my @b = split /--/, $item;
450 foreach my $string (@b) {
452 $locale{$string} = 1;
453 $alllocales{$string} = 1;
459 sub unescape_template_string {
470 if (!defined $cached{$file}) {
471 my %plugins = ( 'loaded' => { }, 'needed' => { } );
473 open(IN, $file) || die $file;
478 while (my $line = <IN>) {
481 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
482 $plugins{loaded}->{$1} = 1;
485 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
487 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
490 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
491 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
492 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
493 (.*?) # Der zu übersetzende String ::Parameter $3::
494 (?<!\\)\2 # Ende des zu übersetzenden Strings
495 | # Start von Variante 2: [% '...' | $T8 %]
496 \[\% # Template-Start-Tag
497 [\-~#]? # Whitespace-Unterdrückung
498 \s* # Optional beliebig viele Whitespace
499 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
500 (.*?) # Der zu übersetzende String ::Parameter $5::
501 (?<!\\)\4 # Ende des zu übersetzenden Strings
502 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
503 (\$T8) # Filteraufruf ::Parameter $6::
504 .*? # Optionale Argumente für den Filter
506 [\-~#]? # Whitespace-Unterdrückung
507 \%\] # Template-Ende-Tag
510 my $module = $1 || $6;
511 my $string = $3 || $5;
512 print "Found filter >>>$string<<<\n" if $debug;
513 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
515 $string = unescape_template_string($string);
516 $cached{$file}{all}{$string} = 1;
517 $cached{$file}{html}{$string} = 1;
518 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
519 $plugins{needed}->{T8} = 1 if $module eq '$T8';
520 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
523 while ($line =~ m/\[\% # Template-Start-Tag
524 [\-~#]? # Whitespace-Unterdrückung
525 \s* # Optional beliebig viele Whitespace
526 (?: # Die erkannten Template-Direktiven
531 \s+ # Mindestens ein Whitespace
532 [\'\"]? # Anfang des Dateinamens
533 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
534 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
536 my $new_file_name = "$basedir/templates/webpages/$1.html";
537 $cached{$file}{scanh}{$new_file_name} = 1;
538 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
544 foreach my $plugin (keys %{ $plugins{needed} }) {
545 next if ($plugins{loaded}->{$plugin});
546 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
550 # copy back into global arrays
551 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
552 $locale{$_} = 1 for keys %{$cached{$file}{html}};
553 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
555 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
557 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
560 sub search_unused_htmlfiles {
561 my @unscanned_dirs = ('../../templates/webpages');
563 while (scalar @unscanned_dirs) {
564 my $dir = shift @unscanned_dirs;
566 foreach my $entry (<$dir/*>) {
568 push @unscanned_dirs, $entry;
570 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
571 print "W: unused HTML template: " . strip_base($entry) . "\n";
579 my $s = "$_[0]"; # Create a copy of the string.
582 $s =~ s|templates/webpages/||;
589 $val =~ s/(\'|\\$)/\\$1/g;
590 return "'" . $val . "'";
594 my $key = _single_quote(shift);
595 my $text = _single_quote(shift);
597 my $template = $params{template} || qq| %-29s => %s,\n|;
598 my $fh = $params{fh} || croak 'need filehandle in _print_line';
600 print $fh sprintf $template, $key, $text;
606 my $file = $params{file} || croak 'need filename in generate_file';
607 my $header = $params{header};
608 my $lines = $params{data_sub};
609 my $data_name = $params{data_name};
610 my @delim = split //, ($params{delim} || '{}');
612 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
614 $charset =~ s/\r?\n//g;
615 my $emacs_charset = lc $charset;
617 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
618 print $fh $header, "\n" if $header;
619 print $fh "$data_name = $delim[0]\n" if $data_name;
623 print $fh qq|$delim[1];\n\n1;\n|;
629 do { local ( @ARGV, $/ ) = $file; <> }
636 locales.pl - Collect strings for translation in Lx-Office
640 locales.pl [options] lang_code
643 -n, --no-custom-files Do not process files whose name contains "_"
644 -c, --check-files Run extended checks on HTML files
645 -v, --verbose Be more verbose
646 -h, --help Show this help
652 =item B<-n>, B<--no-custom-files>
654 Do not process files whose name contains "_", e.g. "custom_io.pl".
656 =item B<-c>, B<--check-files>
658 Run extended checks on the usage of templates. This can be used to
659 discover HTML templates that are never used as well as the usage of
660 non-existing HTML templates.
662 =item B<-v>, B<--verbose>
670 This script collects strings from Perl files, the menu.ini file and
671 HTML templates and puts them into the file "all" for translation.