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/Template/Plugin" );
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);
336 # is this another file
337 if (/require\s+\W.*\.pl/) {
339 $newfile =~ s/require\s+\W//;
340 $newfile =~ s|bin/mozilla||;
341 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
342 } elsif (/use\s+SL::([\w:]*)/) {
345 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
348 # is this a template call?
349 if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
350 my $newfile = "$basedir/templates/webpages/$1.html";
351 if (/parse_html_template2/) {
352 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
355 $cached{$file}{scanh}{$newfile} = 1;
358 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
367 my ($null, $country) = split /,/;
368 $country =~ s/^ +[\"\']//;
369 $country =~ s/[\"\'].*//;
375 # is it a submit button before $locale->
376 if (/$submitsearch/) {
378 if ($` !~ /locale->text/) {
380 $sub_line_no = $line_no;
384 my ($found) = /locale->text.*?\(/;
389 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
392 # if there is no $ in the string record it
393 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
395 # this guarantees one instance of string
396 $cached{$file}{locale}{$string} = 1;
398 # this one is for all the locales
399 $cached{$file}{all}{$string} = 1;
401 # is it a submit button before $locale->
403 $cached{$file}{submit}{$string} = 1;
406 } elsif ($postmatch =~ />/) {
410 # exit loop if there are no more locales on this line
411 ($rc) = ($postmatch =~ /locale->text/);
413 if ( ($postmatch =~ />/)
414 || (!$found && ($sub_line_no != $line_no) && />/)) {
424 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
425 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
426 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
427 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
428 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
429 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
431 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
437 my $fh = new FileHandle;
438 open $fh, "$file" or die "$! : $file";
440 my @a = grep m/^\[/, <$fh>;
444 grep { s/(\[|\])//g } @a;
446 foreach my $item (@a) {
447 my @b = split /--/, $item;
448 foreach my $string (@b) {
450 $locale{$string} = 1;
451 $alllocales{$string} = 1;
457 sub unescape_template_string {
468 if (!defined $cached{$file}) {
469 my %plugins = ( 'loaded' => { }, 'needed' => { } );
471 open(IN, $file) || die $file;
476 while (my $line = <IN>) {
479 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
480 $plugins{loaded}->{$1} = 1;
483 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
485 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
488 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
489 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
490 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
491 (.*?) # Der zu übersetzende String ::Parameter $3::
492 (?<!\\)\2 # Ende des zu übersetzenden Strings
493 | # Start von Variante 2: [% '...' | $T8 %]
494 \[\% # Template-Start-Tag
495 [\-~#]? # Whitespace-Unterdrückung
496 \s* # Optional beliebig viele Whitespace
497 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
498 (.*?) # Der zu übersetzende String ::Parameter $5::
499 (?<!\\)\4 # Ende des zu übersetzenden Strings
500 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
501 (\$T8) # Filteraufruf ::Parameter $6::
502 .*? # Optionale Argumente für den Filter
504 [\-~#]? # Whitespace-Unterdrückung
505 \%\] # Template-Ende-Tag
508 my $module = $1 || $6;
509 my $string = $3 || $5;
510 print "Found filter >>>$string<<<\n" if $debug;
511 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
513 $string = unescape_template_string($string);
514 $cached{$file}{all}{$string} = 1;
515 $cached{$file}{html}{$string} = 1;
516 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
517 $plugins{needed}->{T8} = 1 if $module eq '$T8';
518 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
521 while ($line =~ m/\[\% # Template-Start-Tag
522 [\-~#]? # Whitespace-Unterdrückung
523 \s* # Optional beliebig viele Whitespace
524 (?: # Die erkannten Template-Direktiven
529 \s+ # Mindestens ein Whitespace
530 [\'\"]? # Anfang des Dateinamens
531 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
532 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
534 my $new_file_name = "$basedir/templates/webpages/$1.html";
535 $cached{$file}{scanh}{$new_file_name} = 1;
536 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
542 foreach my $plugin (keys %{ $plugins{needed} }) {
543 next if ($plugins{loaded}->{$plugin});
544 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
548 # copy back into global arrays
549 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
550 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
551 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
553 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
555 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
558 sub search_unused_htmlfiles {
559 my @unscanned_dirs = ('../../templates/webpages');
561 while (scalar @unscanned_dirs) {
562 my $dir = shift @unscanned_dirs;
564 foreach my $entry (<$dir/*>) {
566 push @unscanned_dirs, $entry;
568 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
569 print "W: unused HTML template: " . strip_base($entry) . "\n";
577 my $s = "$_[0]"; # Create a copy of the string.
580 $s =~ s|templates/webpages/||;
587 $val =~ s/(\'|\\$)/\\$1/g;
588 return "'" . $val . "'";
592 my $key = _single_quote(shift);
593 my $text = _single_quote(shift);
595 my $template = $params{template} || qq| %-29s => %s,\n|;
596 my $fh = $params{fh} || croak 'need filehandle in _print_line';
598 print $fh sprintf $template, $key, $text;
604 my $file = $params{file} || croak 'need filename in generate_file';
605 my $header = $params{header};
606 my $lines = $params{data_sub};
607 my $data_name = $params{data_name};
608 my @delim = split //, ($params{delim} || '{}');
610 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
612 $charset =~ s/\r?\n//g;
613 my $emacs_charset = lc $charset;
615 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
616 print $fh $header, "\n" if $header;
617 print $fh "$data_name = $delim[0]\n" if $data_name;
621 print $fh qq|$delim[1];\n\n1;\n|;
627 do { local ( @ARGV, $/ ) = $file; <> }
634 locales.pl - Collect strings for translation in Lx-Office
638 locales.pl [options] lang_code
641 -n, --no-custom-files Do not process files whose name contains "_"
642 -c, --check-files Run extended checks on HTML files
643 -v, --verbose Be more verbose
644 -h, --help Show this help
650 =item B<-n>, B<--no-custom-files>
652 Do not process files whose name contains "_", e.g. "custom_io.pl".
654 =item B<-c>, B<--check-files>
656 Run extended checks on the usage of templates. This can be used to
657 discover HTML templates that are never used as well as the usage of
658 non-existing HTML templates.
660 =item B<-v>, B<--verbose>
668 This script collects strings from Perl files, the menu.ini file and
669 HTML templates and puts them into the file "all" for translation.