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 $dbupdir = "$basedir/sql/Pg-upgrade";
35 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
36 my $menufile = "menu.ini";
37 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
39 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
40 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
44 opendir DIR, "$bindir" or die "$!";
45 my @progfiles = grep { /\.pl$/ && !/(_custom|^\.)/ } readdir DIR;
47 my @customfiles = grep /_custom/, readdir DIR;
50 # put customized files into @customfiles
55 @menufiles = ($menufile);
57 opendir DIR, "$basedir" or die "$!";
58 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
60 unshift @menufiles, $menufile;
63 opendir DIR, $dbupdir or die "$!";
64 my @dbplfiles = grep { /\.pl$/ } readdir DIR;
67 opendir DIR, $dbupdir2 or die "$!";
68 my @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
71 # slurp the translations in
77 if (-f "$locales_dir/all") {
78 require "$locales_dir/all";
80 if (-f "$locales_dir/missing") {
81 require "$locales_dir/missing" ;
82 unlink "$locales_dir/missing";
84 if (-f "$locales_dir/lost") {
85 require "$locales_dir/lost";
86 unlink "$locales_dir/lost";
89 my $charset = slurp("$locales_dir/charset") || 'utf-8';
92 my %old_texts = %{ $self->{texts} || {} };
94 map({ handle_file($_, $bindir); } @progfiles);
95 map({ handle_file($_, $dbupdir); } @dbplfiles);
96 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
100 file => "$locales_dir/all",
101 header => $ALL_HEADER,
102 data_name => '$self->{texts}',
103 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
106 # calc and generate missing
107 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
111 file => "$locales_dir/missing",
112 header => $MISSING_HEADER,
113 data_name => '$missing',
114 data_sub => sub { _print_line($_, '', @_) for @missing },
118 # calc and generate lost
119 while (my ($text, $translation) = each %old_texts) {
120 next if ($alllocales{$text});
121 push @lost, { 'text' => $text, 'translation' => $translation };
125 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
127 file => "$locales_dir/lost",
128 header => $LOST_HEADER,
130 data_name => '@lost',
132 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
137 my $trlanguage = slurp("$locales_dir/LANGUAGE");
140 search_unused_htmlfiles() if $opt_c;
142 my $count = scalar keys %alllocales;
143 my $notext = scalar @missing;
144 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
145 print "\n$trlanguage - ${per}%";
146 print " - $notext/$count missing" if $notext;
155 # These are all the texts to build the translations files.
156 # The file has the form of 'english text' => 'foreign text',
157 # you can add the translation in this file or in the 'missing' file
158 # run locales.pl from this directory to rebuild the translation files
160 $MISSING_HEADER = <<EOL;
161 # add the missing texts and run locales.pl to rebuild
163 $LOST_HEADER = <<EOL;
164 # The last 50 text strings, that have been removed.
165 # This file has been auto-generated by locales.pl. Please don't edit!
173 'no-custom-files' => \$opt_n,
174 'check-files' => \$opt_c,
175 'verbose' => \$opt_v,
187 pod2usage(-exitstatus => 0, -verbose => 2);
192 my $arg = shift @ARGV;
194 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
195 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
201 print "The locale directory '$arg' could not be found.\n";
205 } elsif (!-f 'all' || !-f 'LANGUAGE') {
206 print "locales.pl was not called from a locale/* subdirectory,\n"
207 . "and no locale directory name was given.\n";
213 my ($file, $dir) = @_;
214 print "\n$file" if $opt_v;
218 &scanfile("$dir/$file");
220 # scan custom_{module}.pl or {login}_{module}.pl files
221 foreach my $customfile (@customfiles) {
222 if ($customfile =~ /_$file/) {
223 if (-f "$dir/$customfile") {
224 &scanfile("$dir/$customfile");
229 # if this is the menu.pl file
230 if ($file eq 'menu.pl') {
231 foreach my $item (@menufiles) {
232 &scanmenu("$basedir/$item");
236 if ($file eq 'menunew.pl') {
237 foreach my $item (@menufiles) {
238 &scanmenu("$basedir/$item");
245 foreach my $text (keys %$missing) {
246 if ($locale{$text} || $htmllocale{$text}) {
247 unless ($self->{texts}{$text}) {
248 $self->{texts}{$text} = $missing->{$text};
254 sub extract_text_between_parenthesis {
255 my ($fh, $line) = @_;
256 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
259 if (length($line) <= $pos) {
261 return ($text, "") unless ($line);
265 my $cur_char = substr($line, $pos, 1);
267 if (!$inside_string) {
268 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
269 $inside_string = substr($line, $pos + 2, 1);
272 } elsif ((length($line) >= ($pos + 2)) &&
273 (substr($line, $pos, 1) eq "q")) {
274 $inside_string = substr($line, $pos + 1, 1);
277 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
278 $inside_string = $cur_char;
280 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
281 return ($text, substr($line, $pos + 1));
289 } elsif ($cur_char eq '\\') {
293 } elsif ($cur_char eq $inside_string) {
294 undef($inside_string);
307 my $dont_include_subs = shift;
308 my $scanned_files = shift;
313 $scanned_files = {} unless ($scanned_files);
314 return if ($scanned_files->{$file});
315 $scanned_files->{$file} = 1;
317 if (!defined $cached{$file}) {
319 return unless (-f "$file");
321 my $fh = new FileHandle;
322 open $fh, "$file" or die "$! : $file";
324 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
329 # is this another file
330 if (/require\s+\W.*\.pl/) {
332 $newfile =~ s/require\s+\W//;
333 $newfile =~ s|bin/mozilla||;
334 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
335 } elsif (/use\s+SL::([\w:]*)/) {
338 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
341 # is this a template call?
342 if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
343 my $newfile = "$basedir/templates/webpages/$1.html";
344 if (/parse_html_template2/) {
345 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
348 $cached{$file}{scanh}{$newfile} = 1;
351 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
360 my ($null, $country) = split /,/;
361 $country =~ s/^ +[\"\']//;
362 $country =~ s/[\"\'].*//;
368 # is it a submit button before $locale->
369 if (/$submitsearch/) {
371 if ($` !~ /locale->text/) {
373 $sub_line_no = $line_no;
377 my ($found) = /locale->text.*?\(/;
382 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
385 # if there is no $ in the string record it
386 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
388 # this guarantees one instance of string
389 $cached{$file}{locale}{$string} = 1;
391 # this one is for all the locales
392 $cached{$file}{all}{$string} = 1;
394 # is it a submit button before $locale->
396 $cached{$file}{submit}{$string} = 1;
399 } elsif ($postmatch =~ />/) {
403 # exit loop if there are no more locales on this line
404 ($rc) = ($postmatch =~ /locale->text/);
406 if ( ($postmatch =~ />/)
407 || (!$found && ($sub_line_no != $line_no) && />/)) {
417 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
418 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
419 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
420 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
421 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
422 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
424 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
430 my $fh = new FileHandle;
431 open $fh, "$file" or die "$! : $file";
433 my @a = grep m/^\[/, <$fh>;
437 grep { s/(\[|\])//g } @a;
439 foreach my $item (@a) {
440 my @b = split /--/, $item;
441 foreach my $string (@b) {
443 $locale{$string} = 1;
444 $alllocales{$string} = 1;
450 sub unescape_template_string {
461 if (!defined $cached{$file}) {
462 my %plugins = ( 'loaded' => { }, 'needed' => { } );
464 open(IN, $file) || die $file;
469 while (my $line = <IN>) {
472 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
473 $plugins{loaded}->{$1} = 1;
476 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
478 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
481 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
482 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
483 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
484 (.*?) # Der zu übersetzende String ::Parameter $3::
485 (?<!\\)\2 # Ende des zu übersetzenden Strings
486 | # Start von Variante 2: [% '...' | $T8 %]
487 \[\% # Template-Start-Tag
488 [\-~#]? # Whitespace-Unterdrückung
489 \s* # Optional beliebig viele Whitespace
490 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
491 (.*?) # Der zu übersetzende String ::Parameter $5::
492 (?<!\\)\4 # Ende des zu übersetzenden Strings
493 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
494 (\$T8) # Filteraufruf ::Parameter $6::
495 .*? # Optionale Argumente für den Filter
497 [\-~#]? # Whitespace-Unterdrückung
498 \%\] # Template-Ende-Tag
501 my $module = $1 || $6;
502 my $string = $3 || $5;
503 print "Found filter >>>$string<<<\n" if $debug;
504 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
506 $string = unescape_template_string($string);
507 $cached{$file}{all}{$string} = 1;
508 $cached{$file}{html}{$string} = 1;
509 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
510 $plugins{needed}->{T8} = 1 if $module eq '$T8';
511 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
514 while ($line =~ m/\[\% # Template-Start-Tag
515 [\-~#]? # Whitespace-Unterdrückung
516 \s* # Optional beliebig viele Whitespace
517 (?: # Die erkannten Template-Direktiven
522 \s+ # Mindestens ein Whitespace
523 [\'\"]? # Anfang des Dateinamens
524 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
525 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
527 my $new_file_name = "$basedir/templates/webpages/$1.html";
528 $cached{$file}{scanh}{$new_file_name} = 1;
529 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
535 foreach my $plugin (keys %{ $plugins{needed} }) {
536 next if ($plugins{loaded}->{$plugin});
537 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
541 # copy back into global arrays
542 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
543 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
544 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
546 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
548 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
551 sub search_unused_htmlfiles {
552 my @unscanned_dirs = ('../../templates/webpages');
554 while (scalar @unscanned_dirs) {
555 my $dir = shift @unscanned_dirs;
557 foreach my $entry (<$dir/*>) {
559 push @unscanned_dirs, $entry;
561 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
562 print "W: unused HTML template: " . strip_base($entry) . "\n";
570 my $s = "$_[0]"; # Create a copy of the string.
573 $s =~ s|templates/webpages/||;
580 $val =~ s/(\'|\\$)/\\$1/g;
581 return "'" . $val . "'";
585 my $key = _single_quote(shift);
586 my $text = _single_quote(shift);
588 my $template = $params{template} || qq| %-29s => %s,\n|;
589 my $fh = $params{fh} || croak 'need filehandle in _print_line';
591 print $fh sprintf $template, $key, $text;
597 my $file = $params{file} || croak 'need filename in generate_file';
598 my $header = $params{header};
599 my $lines = $params{data_sub};
600 my $data_name = $params{data_name};
601 my @delim = split //, ($params{delim} || '{}');
603 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
605 $charset =~ s/\r?\n//g;
606 my $emacs_charset = lc $charset;
608 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
609 print $fh $header, "\n" if $header;
610 print $fh "$data_name = $delim[0]\n" if $data_name;
614 print $fh qq|$delim[1];\n\n1;\n|;
620 do { local ( @ARGV, $/ ) = $file; <> }
627 locales.pl - Collect strings for translation in Lx-Office
631 locales.pl [options] lang_code
634 -n, --no-custom-files Do not process files whose name contains "_"
635 -c, --check-files Run extended checks on HTML files
636 -v, --verbose Be more verbose
637 -h, --help Show this help
643 =item B<-n>, B<--no-custom-files>
645 Do not process files whose name contains "_", e.g. "custom_io.pl".
647 =item B<-c>, B<--check-files>
649 Run extended checks on the usage of templates. This can be used to
650 discover HTML templates that are never used as well as the usage of
651 non-existing HTML templates.
653 =item B<-v>, B<--verbose>
661 This script collects strings from Perl files, the menu.ini file and
662 HTML templates and puts them into the file "all" for translation.