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" );
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);
46 my ($dir_name, $files) = @_;
52 opendir my $dir, $dir_name or die "$! $dir_name";
54 foreach my $name (readdir $dir) {
55 next if $name eq '.' || $name eq '..';
57 my $full_name = "${dir_name}/${name}";
59 push @dirs_to_check, $full_name;
61 push @{ $files }, $full_name;
67 map { find_files($_, $files) } @dirs_to_check;
72 my @bindir_files = find_files($bindir);
73 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
74 my @customfiles = grep /_custom/, @bindir_files;
76 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
78 # put customized files into @customfiles
83 @menufiles = ($menufile);
85 opendir DIR, "$basedir" or die "$!";
86 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
88 unshift @menufiles, $menufile;
91 opendir DIR, $dbupdir or die "$!";
92 my @dbplfiles = grep { /\.pl$/ } readdir DIR;
95 opendir DIR, $dbupdir2 or die "$!";
96 my @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
99 # slurp the translations in
105 if (-f "$locales_dir/all") {
106 require "$locales_dir/all";
108 if (-f "$locales_dir/missing") {
109 require "$locales_dir/missing" ;
110 unlink "$locales_dir/missing";
112 if (-f "$locales_dir/lost") {
113 require "$locales_dir/lost";
114 unlink "$locales_dir/lost";
117 my $charset = slurp("$locales_dir/charset") || 'utf-8';
120 my %old_texts = %{ $self->{texts} || {} };
122 map({ handle_file(@{ $_ }); } @progfiles);
123 map({ handle_file($_, $dbupdir); } @dbplfiles);
124 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
128 file => "$locales_dir/all",
129 header => $ALL_HEADER,
130 data_name => '$self->{texts}',
131 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
134 # calc and generate missing
135 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
139 file => "$locales_dir/missing",
140 header => $MISSING_HEADER,
141 data_name => '$missing',
142 data_sub => sub { _print_line($_, '', @_) for @missing },
146 # calc and generate lost
147 while (my ($text, $translation) = each %old_texts) {
148 next if ($alllocales{$text});
149 push @lost, { 'text' => $text, 'translation' => $translation };
153 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
155 file => "$locales_dir/lost",
156 header => $LOST_HEADER,
158 data_name => '@lost',
160 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
165 my $trlanguage = slurp("$locales_dir/LANGUAGE");
168 search_unused_htmlfiles() if $opt_c;
170 my $count = scalar keys %alllocales;
171 my $notext = scalar @missing;
172 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
173 print "\n$trlanguage - ${per}%";
174 print " - $notext/$count missing" if $notext;
183 # These are all the texts to build the translations files.
184 # The file has the form of 'english text' => 'foreign text',
185 # you can add the translation in this file or in the 'missing' file
186 # run locales.pl from this directory to rebuild the translation files
188 $MISSING_HEADER = <<EOL;
189 # add the missing texts and run locales.pl to rebuild
191 $LOST_HEADER = <<EOL;
192 # The last 50 text strings, that have been removed.
193 # This file has been auto-generated by locales.pl. Please don't edit!
201 'no-custom-files' => \$opt_n,
202 'check-files' => \$opt_c,
203 'verbose' => \$opt_v,
215 pod2usage(-exitstatus => 0, -verbose => 2);
220 my $arg = shift @ARGV;
222 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
223 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
229 print "The locale directory '$arg' could not be found.\n";
233 } elsif (!-f 'all' || !-f 'LANGUAGE') {
234 print "locales.pl was not called from a locale/* subdirectory,\n"
235 . "and no locale directory name was given.\n";
241 my ($file, $dir) = @_;
242 print "\n$file" if $opt_v;
246 &scanfile("$dir/$file");
248 # scan custom_{module}.pl or {login}_{module}.pl files
249 foreach my $customfile (@customfiles) {
250 if ($customfile =~ /_$file/) {
251 if (-f "$dir/$customfile") {
252 &scanfile("$dir/$customfile");
257 # if this is the menu.pl file
258 if ($file eq 'menu.pl') {
259 foreach my $item (@menufiles) {
260 &scanmenu("$basedir/$item");
264 if ($file eq 'menunew.pl') {
265 foreach my $item (@menufiles) {
266 &scanmenu("$basedir/$item");
273 foreach my $text (keys %$missing) {
274 if ($locale{$text} || $htmllocale{$text}) {
275 unless ($self->{texts}{$text}) {
276 $self->{texts}{$text} = $missing->{$text};
282 sub extract_text_between_parenthesis {
283 my ($fh, $line) = @_;
284 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
287 if (length($line) <= $pos) {
289 return ($text, "") unless ($line);
293 my $cur_char = substr($line, $pos, 1);
295 if (!$inside_string) {
296 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
297 $inside_string = substr($line, $pos + 2, 1);
300 } elsif ((length($line) >= ($pos + 2)) &&
301 (substr($line, $pos, 1) eq "q")) {
302 $inside_string = substr($line, $pos + 1, 1);
305 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
306 $inside_string = $cur_char;
308 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
309 return ($text, substr($line, $pos + 1));
314 $text .= '\\' unless $cur_char eq "'";
318 } elsif ($cur_char eq '\\') {
321 } elsif ($cur_char eq $inside_string) {
322 undef($inside_string);
335 my $dont_include_subs = shift;
336 my $scanned_files = shift;
341 $scanned_files = {} unless ($scanned_files);
342 return if ($scanned_files->{$file});
343 $scanned_files->{$file} = 1;
345 if (!defined $cached{$file}) {
347 return unless (-f "$file");
349 my $fh = new FileHandle;
350 open $fh, "$file" or die "$! : $file";
352 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
355 last if /^\s*__END__/;
359 # is this another file
360 if (/require\s+\W.*\.pl/) {
362 $newfile =~ s/require\s+\W//;
363 $newfile =~ s|bin/mozilla||;
364 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
365 } elsif (/use\s+SL::([\w:]*)/) {
368 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
371 # is this a template call?
372 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
373 my $newfile = "$basedir/templates/webpages/$1.html";
374 if (/parse_html_template2/) {
375 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
378 $cached{$file}{scanh}{$newfile} = 1;
381 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
390 my ($null, $country) = split /,/;
391 $country =~ s/^ +[\"\']//;
392 $country =~ s/[\"\'].*//;
398 # is it a submit button before $locale->
399 if (/$submitsearch/) {
401 if ($` !~ /locale->text/) {
403 $sub_line_no = $line_no;
407 my ($found) = /locale->text.*?\(/;
412 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
415 # if there is no $ in the string record it
416 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
418 # this guarantees one instance of string
419 $cached{$file}{locale}{$string} = 1;
421 # this one is for all the locales
422 $cached{$file}{all}{$string} = 1;
424 # is it a submit button before $locale->
426 $cached{$file}{submit}{$string} = 1;
429 } elsif ($postmatch =~ />/) {
433 # exit loop if there are no more locales on this line
434 ($rc) = ($postmatch =~ /locale->text/);
436 if ( ($postmatch =~ />/)
437 || (!$found && ($sub_line_no != $line_no) && />/)) {
447 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
448 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
449 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
450 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
451 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
452 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
454 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
460 my $fh = new FileHandle;
461 open $fh, "$file" or die "$! : $file";
463 my @a = grep m/^\[/, <$fh>;
467 grep { s/(\[|\])//g } @a;
469 foreach my $item (@a) {
470 my @b = split /--/, $item;
471 foreach my $string (@b) {
473 $locale{$string} = 1;
474 $alllocales{$string} = 1;
480 sub unescape_template_string {
491 if (!defined $cached{$file}) {
492 my %plugins = ( 'loaded' => { }, 'needed' => { } );
494 open(IN, $file) || die $file;
499 while (my $line = <IN>) {
502 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
503 $plugins{loaded}->{$1} = 1;
506 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
508 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
511 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
512 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
513 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
514 (.*?) # Der zu übersetzende String ::Parameter $3::
515 (?<!\\)\2 # Ende des zu übersetzenden Strings
516 | # Start von Variante 2: [% '...' | $T8 %]
517 \[\% # Template-Start-Tag
518 [\-~#]? # Whitespace-Unterdrückung
519 \s* # Optional beliebig viele Whitespace
520 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
521 (.*?) # Der zu übersetzende String ::Parameter $5::
522 (?<!\\)\4 # Ende des zu übersetzenden Strings
523 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
524 (\$T8) # Filteraufruf ::Parameter $6::
525 .*? # Optionale Argumente für den Filter
527 [\-~#]? # Whitespace-Unterdrückung
528 \%\] # Template-Ende-Tag
531 my $module = $1 || $6;
532 my $string = $3 || $5;
533 print "Found filter >>>$string<<<\n" if $debug;
534 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
536 $string = unescape_template_string($string);
537 $cached{$file}{all}{$string} = 1;
538 $cached{$file}{html}{$string} = 1;
539 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
540 $plugins{needed}->{T8} = 1 if $module eq '$T8';
541 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
544 while ($line =~ m/\[\% # Template-Start-Tag
545 [\-~#]? # Whitespace-Unterdrückung
546 \s* # Optional beliebig viele Whitespace
547 (?: # Die erkannten Template-Direktiven
552 \s+ # Mindestens ein Whitespace
553 [\'\"]? # Anfang des Dateinamens
554 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
555 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
557 my $new_file_name = "$basedir/templates/webpages/$1.html";
558 $cached{$file}{scanh}{$new_file_name} = 1;
559 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
565 foreach my $plugin (keys %{ $plugins{needed} }) {
566 next if ($plugins{loaded}->{$plugin});
567 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
571 # copy back into global arrays
572 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
573 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
574 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
576 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
578 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
581 sub search_unused_htmlfiles {
582 my @unscanned_dirs = ('../../templates/webpages');
584 while (scalar @unscanned_dirs) {
585 my $dir = shift @unscanned_dirs;
587 foreach my $entry (<$dir/*>) {
589 push @unscanned_dirs, $entry;
591 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
592 print "W: unused HTML template: " . strip_base($entry) . "\n";
600 my $s = "$_[0]"; # Create a copy of the string.
603 $s =~ s|templates/webpages/||;
610 $val =~ s/(\'|\\$)/\\$1/g;
611 return "'" . $val . "'";
615 my $key = _single_quote(shift);
616 my $text = _single_quote(shift);
618 my $template = $params{template} || qq| %-29s => %s,\n|;
619 my $fh = $params{fh} || croak 'need filehandle in _print_line';
621 print $fh sprintf $template, $key, $text;
627 my $file = $params{file} || croak 'need filename in generate_file';
628 my $header = $params{header};
629 my $lines = $params{data_sub};
630 my $data_name = $params{data_name};
631 my @delim = split //, ($params{delim} || '{}');
633 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
635 $charset =~ s/\r?\n//g;
636 my $emacs_charset = lc $charset;
638 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
639 print $fh $header, "\n" if $header;
640 print $fh "$data_name = $delim[0]\n" if $data_name;
644 print $fh qq|$delim[1];\n\n1;\n|;
650 do { local ( @ARGV, $/ ) = $file; <> }
657 locales.pl - Collect strings for translation in Lx-Office
661 locales.pl [options] lang_code
664 -n, --no-custom-files Do not process files whose name contains "_"
665 -c, --check-files Run extended checks on HTML files
666 -v, --verbose Be more verbose
667 -h, --help Show this help
673 =item B<-n>, B<--no-custom-files>
675 Do not process files whose name contains "_", e.g. "custom_io.pl".
677 =item B<-c>, B<--check-files>
679 Run extended checks on the usage of templates. This can be used to
680 discover HTML templates that are never used as well as the usage of
681 non-existing HTML templates.
683 =item B<-v>, B<--verbose>
691 This script collects strings from Perl files, the menu.ini file and
692 HTML templates and puts them into the file "all" for translation.