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.
16 use List::Util qw(first);
20 use File::Slurp qw(slurp);
22 $OUTPUT_AUTOFLUSH = 1;
31 my $basedir = "../..";
32 my $locales_dir = ".";
33 my $bindir = "$basedir/bin/mozilla";
34 my @progdirs = ( "$basedir/SL/Controller", "$basedir/SL/Template/Plugin", "$basedir/SL/Auth" );
35 my $dbupdir = "$basedir/sql/Pg-upgrade";
36 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
37 my $menufile = "menu.ini";
38 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
40 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
41 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
45 opendir DIR, "$bindir" or die "$!";
46 my @progfiles = map { [ $_, $bindir ] } grep { /\.pl$/ && !/(_custom|^\.)/ } readdir DIR;
48 my @customfiles = grep /_custom/, readdir DIR;
51 foreach my $dir (@progdirs) {
52 opendir DIR, $dir or die "$!";
53 push @progfiles, map { [ $_, $dir ] } grep { /\.pm$/ } readdir DIR;
57 # put customized files into @customfiles
62 @menufiles = ($menufile);
64 opendir DIR, "$basedir" or die "$!";
65 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
67 unshift @menufiles, $menufile;
70 opendir DIR, $dbupdir or die "$!";
71 my @dbplfiles = grep { /\.pl$/ } readdir DIR;
74 opendir DIR, $dbupdir2 or die "$!";
75 my @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
78 # slurp the translations in
84 if (-f "$locales_dir/all") {
85 require "$locales_dir/all";
87 if (-f "$locales_dir/missing") {
88 require "$locales_dir/missing" ;
89 unlink "$locales_dir/missing";
91 if (-f "$locales_dir/lost") {
92 require "$locales_dir/lost";
93 unlink "$locales_dir/lost";
96 my $charset = slurp("$locales_dir/charset") || 'utf-8';
99 my %old_texts = %{ $self->{texts} || {} };
101 map({ handle_file(@{ $_ }); } @progfiles);
102 map({ handle_file($_, $dbupdir); } @dbplfiles);
103 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
107 file => "$locales_dir/all",
108 header => $ALL_HEADER,
109 data_name => '$self->{texts}',
110 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
113 # calc and generate missing
114 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
118 file => "$locales_dir/missing",
119 header => $MISSING_HEADER,
120 data_name => '$missing',
121 data_sub => sub { _print_line($_, '', @_) for @missing },
125 # calc and generate lost
126 while (my ($text, $translation) = each %old_texts) {
127 next if ($alllocales{$text});
128 push @lost, { 'text' => $text, 'translation' => $translation };
132 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
134 file => "$locales_dir/lost",
135 header => $LOST_HEADER,
137 data_name => '@lost',
139 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
144 my $trlanguage = slurp("$locales_dir/LANGUAGE");
147 search_unused_htmlfiles() if $opt_c;
149 my $count = scalar keys %alllocales;
150 my $notext = scalar @missing;
151 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
152 print "\n$trlanguage - ${per}%";
153 print " - $notext/$count missing" if $notext;
162 # These are all the texts to build the translations files.
163 # The file has the form of 'english text' => 'foreign text',
164 # you can add the translation in this file or in the 'missing' file
165 # run locales.pl from this directory to rebuild the translation files
167 $MISSING_HEADER = <<EOL;
168 # add the missing texts and run locales.pl to rebuild
170 $LOST_HEADER = <<EOL;
171 # The last 50 text strings, that have been removed.
172 # This file has been auto-generated by locales.pl. Please don't edit!
180 'no-custom-files' => \$opt_n,
181 'check-files' => \$opt_c,
182 'verbose' => \$opt_v,
194 pod2usage(-exitstatus => 0, -verbose => 2);
199 my $arg = shift @ARGV;
201 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
202 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
208 print "The locale directory '$arg' could not be found.\n";
212 } elsif (!-f 'all' || !-f 'LANGUAGE') {
213 print "locales.pl was not called from a locale/* subdirectory,\n"
214 . "and no locale directory name was given.\n";
220 my ($file, $dir) = @_;
221 print "\n$file" if $opt_v;
225 &scanfile("$dir/$file");
227 # scan custom_{module}.pl or {login}_{module}.pl files
228 foreach my $customfile (@customfiles) {
229 if ($customfile =~ /_$file/) {
230 if (-f "$dir/$customfile") {
231 &scanfile("$dir/$customfile");
236 # if this is the menu.pl file
237 if ($file eq 'menu.pl') {
238 foreach my $item (@menufiles) {
239 &scanmenu("$basedir/$item");
243 if ($file eq 'menunew.pl') {
244 foreach my $item (@menufiles) {
245 &scanmenu("$basedir/$item");
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.*?\(/;
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 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
427 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
428 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
429 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
430 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
431 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
433 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar 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 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
552 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
553 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
555 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
557 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar 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.