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");
259 foreach my $text (keys %$missing) {
260 if ($locale{$text} || $htmllocale{$text}) {
261 unless ($self->{texts}{$text}) {
262 $self->{texts}{$text} = $missing->{$text};
268 sub extract_text_between_parenthesis {
269 my ($fh, $line) = @_;
270 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
273 if (length($line) <= $pos) {
275 return ($text, "") unless ($line);
279 my $cur_char = substr($line, $pos, 1);
281 if (!$inside_string) {
282 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
283 $inside_string = substr($line, $pos + 2, 1);
286 } elsif ((length($line) >= ($pos + 2)) &&
287 (substr($line, $pos, 1) eq "q")) {
288 $inside_string = substr($line, $pos + 1, 1);
291 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
292 $inside_string = $cur_char;
294 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
295 return ($text, substr($line, $pos + 1));
300 $text .= '\\' unless $cur_char eq "'";
304 } elsif ($cur_char eq '\\') {
307 } elsif ($cur_char eq $inside_string) {
308 undef($inside_string);
321 my $dont_include_subs = shift;
322 my $scanned_files = shift;
327 $scanned_files = {} unless ($scanned_files);
328 return if ($scanned_files->{$file});
329 $scanned_files->{$file} = 1;
331 if (!defined $cached{$file}) {
333 return unless (-f "$file");
335 my $fh = new FileHandle;
336 open $fh, "$file" or die "$! : $file";
338 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
341 last if /^\s*__END__/;
345 # is this another file
346 if (/require\s+\W.*\.pl/) {
348 $newfile =~ s/require\s+\W//;
349 $newfile =~ s|bin/mozilla||;
350 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
351 } elsif (/use\s+SL::([\w:]*)/) {
354 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
357 # is this a template call?
358 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
359 my $newfile = "$basedir/templates/webpages/$1.html";
360 if (/parse_html_template2/) {
361 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
364 $cached{$file}{scanh}{$newfile} = 1;
367 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
376 my ($null, $country) = split /,/;
377 $country =~ s/^ +[\"\']//;
378 $country =~ s/[\"\'].*//;
384 # is it a submit button before $locale->
385 if (/$submitsearch/) {
387 if ($` !~ /locale->text/) {
389 $sub_line_no = $line_no;
393 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
398 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
401 # if there is no $ in the string record it
402 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
404 # this guarantees one instance of string
405 $cached{$file}{locale}{$string} = 1;
407 # this one is for all the locales
408 $cached{$file}{all}{$string} = 1;
410 # is it a submit button before $locale->
412 $cached{$file}{submit}{$string} = 1;
415 } elsif ($postmatch =~ />/) {
419 # exit loop if there are no more locales on this line
420 ($rc) = ($postmatch =~ /locale->text/);
422 if ( ($postmatch =~ />/)
423 || (!$found && ($sub_line_no != $line_no) && />/)) {
433 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
434 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
435 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
436 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
437 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
438 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
440 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
446 my $fh = new FileHandle;
447 open $fh, "$file" or die "$! : $file";
449 my @a = grep m/^\[/, <$fh>;
453 grep { s/(\[|\])//g } @a;
455 foreach my $item (@a) {
456 my @b = split /--/, $item;
457 foreach my $string (@b) {
459 $locale{$string} = 1;
460 $alllocales{$string} = 1;
466 sub unescape_template_string {
477 if (!defined $cached{$file}) {
478 my %plugins = ( 'loaded' => { }, 'needed' => { } );
480 open(IN, $file) || die $file;
485 while (my $line = <IN>) {
488 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
489 $plugins{loaded}->{$1} = 1;
492 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
494 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
497 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
498 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
499 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
500 (.*?) # Der zu übersetzende String ::Parameter $3::
501 (?<!\\)\2 # Ende des zu übersetzenden Strings
502 | # Start von Variante 2: [% '...' | $T8 %]
503 \[\% # Template-Start-Tag
504 [\-~#]? # Whitespace-Unterdrückung
505 \s* # Optional beliebig viele Whitespace
506 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
507 (.*?) # Der zu übersetzende String ::Parameter $5::
508 (?<!\\)\4 # Ende des zu übersetzenden Strings
509 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
510 (\$T8) # Filteraufruf ::Parameter $6::
511 .*? # Optionale Argumente für den Filter
513 [\-~#]? # Whitespace-Unterdrückung
514 \%\] # Template-Ende-Tag
517 my $module = $1 || $6;
518 my $string = $3 || $5;
519 print "Found filter >>>$string<<<\n" if $debug;
520 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
522 $string = unescape_template_string($string);
523 $cached{$file}{all}{$string} = 1;
524 $cached{$file}{html}{$string} = 1;
525 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
526 $plugins{needed}->{T8} = 1 if $module eq '$T8';
527 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
530 while ($line =~ m/\[\% # Template-Start-Tag
531 [\-~#]? # Whitespace-Unterdrückung
532 \s* # Optional beliebig viele Whitespace
533 (?: # Die erkannten Template-Direktiven
538 \s+ # Mindestens ein Whitespace
539 [\'\"]? # Anfang des Dateinamens
540 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
541 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
543 my $new_file_name = "$basedir/templates/webpages/$1.html";
544 $cached{$file}{scanh}{$new_file_name} = 1;
545 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
551 foreach my $plugin (keys %{ $plugins{needed} }) {
552 next if ($plugins{loaded}->{$plugin});
553 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
557 # copy back into global arrays
558 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
559 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
560 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
562 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
564 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
567 sub search_unused_htmlfiles {
568 my @unscanned_dirs = ('../../templates/webpages');
570 while (scalar @unscanned_dirs) {
571 my $dir = shift @unscanned_dirs;
573 foreach my $entry (<$dir/*>) {
575 push @unscanned_dirs, $entry;
577 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
578 print "W: unused HTML template: " . strip_base($entry) . "\n";
586 my $s = "$_[0]"; # Create a copy of the string.
589 $s =~ s|templates/webpages/||;
596 $val =~ s/(\'|\\$)/\\$1/g;
597 return "'" . $val . "'";
601 my $key = _single_quote(shift);
602 my $text = _single_quote(shift);
604 my $template = $params{template} || qq| %-29s => %s,\n|;
605 my $fh = $params{fh} || croak 'need filehandle in _print_line';
607 print $fh sprintf $template, $key, $text;
613 my $file = $params{file} || croak 'need filename in generate_file';
614 my $header = $params{header};
615 my $lines = $params{data_sub};
616 my $data_name = $params{data_name};
617 my @delim = split //, ($params{delim} || '{}');
619 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
621 $charset =~ s/\r?\n//g;
622 my $emacs_charset = lc $charset;
624 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
625 print $fh $header, "\n" if $header;
626 print $fh "$data_name = $delim[0]\n" if $data_name;
630 print $fh qq|$delim[1];\n\n1;\n|;
636 do { local ( @ARGV, $/ ) = $file; <> }
643 locales.pl - Collect strings for translation in Lx-Office
647 locales.pl [options] lang_code
650 -n, --no-custom-files Do not process files whose name contains "_"
651 -c, --check-files Run extended checks on HTML files
652 -v, --verbose Be more verbose
653 -h, --help Show this help
659 =item B<-n>, B<--no-custom-files>
661 Do not process files whose name contains "_", e.g. "custom_io.pl".
663 =item B<-c>, B<--check-files>
665 Run extended checks on the usage of templates. This can be used to
666 discover HTML templates that are never used as well as the usage of
667 non-existing HTML templates.
669 =item B<-v>, B<--verbose>
677 This script collects strings from Perl files, the menu.ini file and
678 HTML templates and puts them into the file "all" for translation.