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.
15 use List::Util qw(first);
19 use File::Slurp qw(slurp);
21 $OUTPUT_AUTOFLUSH = 1;
30 my $basedir = "../..";
31 my $locales_dir = ".";
32 my $bindir = "$basedir/bin/mozilla";
33 my $dbupdir = "$basedir/sql/Pg-upgrade";
34 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
35 my $menufile = "menu.ini";
36 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
38 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
39 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
43 opendir DIR, "$bindir" or die "$!";
44 my @progfiles = grep { /\.pl$/ && !/(_custom|^\.)/ } readdir DIR;
46 my @customfiles = grep /_custom/, readdir DIR;
49 # put customized files into @customfiles
54 @menufiles = ($menufile);
56 opendir DIR, "$basedir" or die "$!";
57 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
59 unshift @menufiles, $menufile;
62 opendir DIR, $dbupdir or die "$!";
63 my @dbplfiles = grep { /\.pl$/ } readdir DIR;
66 opendir DIR, $dbupdir2 or die "$!";
67 my @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
70 # slurp the translations in
76 if (-f "$locales_dir/all") {
77 require "$locales_dir/all";
79 if (-f "$locales_dir/missing") {
80 require "$locales_dir/missing" ;
81 unlink "$locales_dir/missing";
83 if (-f "$locales_dir/lost") {
84 require "$locales_dir/lost";
85 unlink "$locales_dir/lost";
88 my $charset = slurp("$locales_dir/charset") || 'utf-8';
91 my %old_texts = %{ $self->{texts} || {} };
93 map({ handle_file($_, $bindir); } @progfiles);
94 map({ handle_file($_, $dbupdir); } @dbplfiles);
95 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
99 file => "$locales_dir/all",
100 header => $ALL_HEADER,
101 data_name => '$self->{texts}',
102 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
105 # calc and generate missing
106 push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
110 file => "$locales_dir/missing",
111 header => $MISSING_HEADER,
112 data_name => '$missing',
113 data_sub => sub { _print_line($_, '', @_) for @missing },
117 # calc and generate lost
118 while (my ($text, $translation) = each %old_texts) {
119 next if ($alllocales{$text});
120 push @lost, { 'text' => $text, 'translation' => $translation };
124 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
126 file => "$locales_dir/lost",
127 header => $LOST_HEADER,
129 data_name => '@lost',
131 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
136 my $trlanguage = slurp("$locales_dir/LANGUAGE");
139 search_unused_htmlfiles() if $opt_c;
141 my $count = scalar keys %alllocales;
142 my $notext = scalar @missing;
143 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
144 print "\n$trlanguage - ${per}%";
145 print " - $notext/$count missing" if $notext;
154 # These are all the texts to build the translations files.
155 # The file has the form of 'english text' => 'foreign text',
156 # you can add the translation in this file or in the 'missing' file
157 # run locales.pl from this directory to rebuild the translation files
159 $MISSING_HEADER = <<EOL;
160 # add the missing texts and run locales.pl to rebuild
162 $LOST_HEADER = <<EOL;
163 # The last 50 text strings, that have been removed.
164 # This file has been auto-generated by locales.pl. Please don't edit!
172 'no-custom-files' => \$opt_n,
173 'check-files' => \$opt_c,
174 'verbose' => \$opt_v,
186 pod2usage(-exitstatus => 0, -verbose => 2);
191 my $arg = shift @ARGV;
193 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
194 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
200 print "The locale directory '$arg' could not be found.\n";
204 } elsif (!-f 'all' || !-f 'LANGUAGE') {
205 print "locales.pl was not called from a locale/* subdirectory,\n"
206 . "and no locale directory name was given.\n";
212 my ($file, $dir) = @_;
213 print "\n$file" if $opt_v;
217 &scanfile("$dir/$file");
219 # scan custom_{module}.pl or {login}_{module}.pl files
220 foreach my $customfile (@customfiles) {
221 if ($customfile =~ /_$file/) {
222 if (-f "$dir/$customfile") {
223 &scanfile("$dir/$customfile");
228 # if this is the menu.pl file
229 if ($file eq 'menu.pl') {
230 foreach my $item (@menufiles) {
231 &scanmenu("$basedir/$item");
235 if ($file eq 'menunew.pl') {
236 foreach my $item (@menufiles) {
237 &scanmenu("$basedir/$item");
244 foreach my $text (keys %$missing) {
245 if ($locale{$text} || $htmllocale{$text}) {
246 unless ($self->{texts}{$text}) {
247 $self->{texts}{$text} = $missing->{$text};
253 sub extract_text_between_parenthesis {
254 my ($fh, $line) = @_;
255 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
258 if (length($line) <= $pos) {
260 return ($text, "") unless ($line);
264 my $cur_char = substr($line, $pos, 1);
266 if (!$inside_string) {
267 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
268 $inside_string = substr($line, $pos + 2, 1);
271 } elsif ((length($line) >= ($pos + 2)) &&
272 (substr($line, $pos, 1) eq "q")) {
273 $inside_string = substr($line, $pos + 1, 1);
276 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
277 $inside_string = $cur_char;
279 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
280 return ($text, substr($line, $pos + 1));
288 } elsif ($cur_char eq '\\') {
292 } elsif ($cur_char eq $inside_string) {
293 undef($inside_string);
306 my $dont_include_subs = shift;
307 my $scanned_files = shift;
312 $scanned_files = {} unless ($scanned_files);
313 return if ($scanned_files->{$file});
314 $scanned_files->{$file} = 1;
316 if (!defined $cached{$file}) {
318 return unless (-f "$file");
320 my $fh = new FileHandle;
321 open $fh, "$file" or die "$! : $file";
323 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
328 # is this another file
329 if (/require\s+\W.*\.pl/) {
331 $newfile =~ s/require\s+\W//;
332 $newfile =~ s|bin/mozilla||;
333 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
334 } elsif (/use\s+SL::([\w:]*)/) {
337 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
340 # is this a template call?
341 if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
342 my $newfile = "$basedir/templates/webpages/$1.html";
343 if (/parse_html_template2/) {
344 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
347 $cached{$file}{scanh}{$newfile} = 1;
350 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
359 my ($null, $country) = split /,/;
360 $country =~ s/^ +[\"\']//;
361 $country =~ s/[\"\'].*//;
367 # is it a submit button before $locale->
368 if (/$submitsearch/) {
370 if ($` !~ /locale->text/) {
372 $sub_line_no = $line_no;
376 my ($found) = /locale->text.*?\(/;
381 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
384 # if there is no $ in the string record it
385 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
387 # this guarantees one instance of string
388 $cached{$file}{locale}{$string} = 1;
390 # this one is for all the locales
391 $cached{$file}{all}{$string} = 1;
393 # is it a submit button before $locale->
395 $cached{$file}{submit}{$string} = 1;
398 } elsif ($postmatch =~ />/) {
402 # exit loop if there are no more locales on this line
403 ($rc) = ($postmatch =~ /locale->text/);
405 if ( ($postmatch =~ />/)
406 || (!$found && ($sub_line_no != $line_no) && />/)) {
416 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
417 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
418 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
419 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
420 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
421 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
423 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
429 my $fh = new FileHandle;
430 open $fh, "$file" or die "$! : $file";
432 my @a = grep m/^\[/, <$fh>;
436 grep { s/(\[|\])//g } @a;
438 foreach my $item (@a) {
439 my @b = split /--/, $item;
440 foreach my $string (@b) {
442 $locale{$string} = 1;
443 $alllocales{$string} = 1;
449 sub unescape_template_string {
460 if (!defined $cached{$file}) {
461 my %plugins = ( 'loaded' => { }, 'needed' => { } );
463 open(IN, $file) || die $file;
468 while (my $line = <IN>) {
471 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
472 $plugins{loaded}->{$1} = 1;
475 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
477 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator));
480 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
481 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
482 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
483 (.*?) # Der zu übersetzende String ::Parameter $3::
484 (?<!\\)\2 # Ende des zu übersetzenden Strings
485 | # Start von Variante 2: [% '...' | $T8 %]
486 \[\% # Template-Start-Tag
487 [\-~#]? # Whitespace-Unterdrückung
488 \s* # Optional beliebig viele Whitespace
489 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
490 (.*?) # Der zu übersetzende String ::Parameter $5::
491 (?<!\\)\4 # Ende des zu übersetzenden Strings
492 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
493 (\$T8) # Filteraufruf ::Parameter $6::
494 .*? # Optionale Argumente für den Filter
496 [\-~#]? # Whitespace-Unterdrückung
497 \%\] # Template-Ende-Tag
500 my $module = $1 || $6;
501 my $string = $3 || $5;
502 print "Found filter >>>$string<<<\n" if $debug;
503 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
505 $string = unescape_template_string($string);
506 $cached{$file}{all}{$string} = 1;
507 $cached{$file}{html}{$string} = 1;
508 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
509 $plugins{needed}->{T8} = 1 if $module eq '$T8';
510 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
513 while ($line =~ m/\[\% # Template-Start-Tag
514 [\-~#]? # Whitespace-Unterdrückung
515 \s* # Optional beliebig viele Whitespace
516 (?: # Die erkannten Template-Direktiven
521 \s+ # Mindestens ein Whitespace
522 [\'\"]? # Anfang des Dateinamens
523 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
524 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
526 my $new_file_name = "$basedir/templates/webpages/$1.html";
527 $cached{$file}{scanh}{$new_file_name} = 1;
528 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
534 foreach my $plugin (keys %{ $plugins{needed} }) {
535 next if ($plugins{loaded}->{$plugin});
536 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
540 # copy back into global arrays
541 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
542 map { $locale{$_} = 1 } keys %{$cached{$file}{html}};
543 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
545 map { scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
547 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
550 sub search_unused_htmlfiles {
551 my @unscanned_dirs = ('../../templates/webpages');
553 while (scalar @unscanned_dirs) {
554 my $dir = shift @unscanned_dirs;
556 foreach my $entry (<$dir/*>) {
558 push @unscanned_dirs, $entry;
560 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
561 print "W: unused HTML template: " . strip_base($entry) . "\n";
569 my $s = "$_[0]"; # Create a copy of the string.
572 $s =~ s|templates/webpages/||;
579 $val =~ s/(\'|\\$)/\\$1/g;
580 return "'" . $val . "'";
584 my $key = _single_quote(shift);
585 my $text = _single_quote(shift);
587 my $template = $params{template} || qq| %-29s => %s,\n|;
588 my $fh = $params{fh} || croak 'need filehandle in _print_line';
590 print $fh sprintf $template, $key, $text;
596 my $file = $params{file} || croak 'need filename in generate_file';
597 my $header = $params{header};
598 my $lines = $params{data_sub};
599 my $data_name = $params{data_name};
600 my @delim = split //, ($params{delim} || '{}');
602 open my $fh, '>', $file or die "$! : $file";
604 print $fh "#!/usr/bin/perl\n# -*- coding: $charset; -*-\n# vim: fenc=$charset\n\n";
605 print $fh $header, "\n" if $header;
606 print $fh "$data_name = $delim[0]\n" if $data_name;
610 print $fh qq|$delim[1];\n\n1;\n|;
616 do { local ( @ARGV, $/ ) = $file; <> }
623 locales.pl - Collect strings for translation in Lx-Office
627 locales.pl [options] lang_code
630 -n, --no-custom-files Do not process files whose name contains "_"
631 -c, --check-files Run extended checks on HTML files
632 -v, --verbose Be more verbose
633 -h, --help Show this help
639 =item B<-n>, B<--no-custom-files>
641 Do not process files whose name contains "_", e.g. "custom_io.pl".
643 =item B<-c>, B<--check-files>
645 Run extended checks on the usage of templates. This can be used to
646 discover HTML templates that are never used as well as the usage of
647 non-existing HTML templates.
649 =item B<-v>, B<--verbose>
657 This script collects strings from Perl files, the menu.ini file and
658 HTML templates and puts them into the file "all" for translation.