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.
13 use List::Util qw(first);
18 $bindir = "$basedir/bin/mozilla";
19 $dbupdir = "$basedir/sql/Pg-upgrade";
20 $dbupdir2 = "$basedir/sql/Pg-upgrade2";
21 $menufile = "menu.ini";
22 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
24 %referenced_html_files = ();
29 # -h extended checks on HTML templates
31 foreach $item (@ARGV) {
36 opendir DIR, "$bindir" or die "$!";
37 @progfiles = grep { /\.pl$/ && !/(_|^\.)/ } readdir DIR;
39 @customfiles = grep /_/, readdir DIR;
42 # put customized files into @customfiles
43 @customfiles = () if ($arg{n});
46 @menufiles = ($menufile);
48 opendir DIR, "$basedir" or die "$!";
49 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
51 unshift @menufiles, $menufile;
54 opendir DIR, $dbupdir or die "$!";
55 @dbplfiles = grep { /\.pl$/ } readdir DIR;
58 opendir DIR, $dbupdir2 or die "$!";
59 @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
62 # slurp the translations in
67 # Read HTML templates.
69 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
70 #foreach $file (@htmltemplates) {
71 # scanhtmlfile($file);
74 map({ handle_file($_, $bindir); } @progfiles);
75 map({ handle_file($_, $dbupdir); } @dbplfiles);
76 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
79 my ($file, $dir) = @_;
80 print "\n$file" if $arg{v};
85 &scanfile("$dir/$file");
87 # scan custom_{module}.pl or {login}_{module}.pl files
88 foreach $customfile (@customfiles) {
89 if ($customfile =~ /_$file/) {
90 if (-f "$dir/$customfile") {
91 &scanfile("$dir/$customfile");
96 # if this is the menu.pl file
97 if ($file eq 'menu.pl') {
98 foreach $item (@menufiles) {
99 &scanmenu("$basedir/$item");
103 if ($file eq 'menunew.pl') {
104 foreach $item (@menufiles) {
105 &scanmenu("$basedir/$item");
106 print "." if $arg{v};
112 eval { require 'missing'; };
115 foreach $text (keys %$missing) {
116 if ($locale{$text} || $htmllocale{$text}) {
117 unless ($self->{texts}{$text}) {
118 $self->{texts}{$text} = $missing->{$text};
123 open FH, ">$file" or die "$! : $file";
125 print FH q|#!/usr/bin/perl
130 foreach $key (sort keys %locale) {
131 if ($self->{texts}{$key}) {
132 $text = $self->{texts}{$key};
137 $text =~ s/\\$/\\\\/;
140 $keytext =~ s/'/\\'/g;
141 $keytext =~ s/\\$/\\\\/;
143 print FH qq| '$keytext'|
144 . (' ' x (27 - length($keytext)))
145 . qq| => '$text',\n|;
153 foreach $key (sort keys %subrt) {
156 $text =~ s/\\$/\\\\/;
157 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
160 foreach $key (sort keys %submit) {
161 $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
163 $text =~ s/\\$/\\\\/;
166 $english_sub =~ s/'/\\'/g;
167 $english_sub =~ s/\\$/\\\\/;
168 $english_sub = lc $key;
170 $translated_sub = lc $text;
171 $english_sub =~ s/( |-|,)/_/g;
172 $translated_sub =~ s/( |-|,)/_/g;
173 print FH qq| '$translated_sub'|
174 . (' ' x (27 - length($translated_sub)))
175 . qq| => '$english_sub',\n|;
187 #foreach $file (@htmltemplates) {
188 # converthtmlfile($file);
193 open FH, ">all" or die "$! : all";
195 print FH q|#!/usr/bin/perl
197 # These are all the texts to build the translations files.
198 # The file has the form of 'english text' => 'foreign text',
199 # you can add the translation in this file or in the 'missing' file
200 # run locales.pl from this directory to rebuild the translation files
205 foreach $key (sort keys %alllocales) {
206 $text = $self->{texts}{$key};
211 $text =~ s/\\$/\\\\/;
220 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
232 open FH, ">missing" or die "$! : missing";
234 print FH q|#!/usr/bin/perl
236 # add the missing texts and run locales.pl to rebuild
241 foreach $text (@missing) {
242 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
254 open(FH, "LANGUAGE");
257 $trlanguage = $language[0];
261 search_unused_htmlfiles();
262 search_translated_htmlfiles_wo_master();
265 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
266 print "\n$trlanguage - ${per}%";
267 print " - $notext missing" if $notext;
274 sub extract_text_between_parenthesis {
275 my ($fh, $line) = @_;
276 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
279 if (length($line) <= $pos) {
281 return ($text, "") unless ($line);
285 my $cur_char = substr($line, $pos, 1);
287 if (!$inside_string) {
288 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
289 $inside_string = substr($line, $pos + 2, 1);
292 } elsif ((length($line) >= ($pos + 2)) &&
293 (substr($line, $pos, 1) eq "q")) {
294 $inside_string = substr($line, $pos + 1, 1);
297 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
298 $inside_string = $cur_char;
300 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
301 return ($text, substr($line, $pos + 1));
309 } elsif ($cur_char eq '\\') {
313 } elsif ($cur_char eq $inside_string) {
314 undef($inside_string);
327 my $dont_include_subs = shift;
328 my $scanned_files = shift;
330 $scanned_files = {} unless ($scanned_files);
331 return if ($scanned_files->{$file});
332 $scanned_files->{$file} = 1;
334 if (!defined $cached{$file}) {
336 return unless (-f "$file");
338 my $fh = new FileHandle;
339 open $fh, "$file" or die "$! : $file";
341 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
346 # is this another file
347 if (/require\s+\W.*\.pl/) {
349 $newfile =~ s/require\s+\W//;
350 $newfile =~ s|bin/mozilla||;
351 # &scanfile("$bindir/$newfile", 0, $scanned_files);
352 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
353 } elsif (/use\s+SL::(.*?);/) {
356 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
357 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
360 # is this a template call?
361 if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
362 my $newfile = "$basedir/templates/webpages/$1_master.html";
363 if (/parse_html_template2/) {
364 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
367 # &scanhtmlfile($newfile);
368 # &converthtmlfile($newfile);
369 $cached{$file}{scanh}{$newfile} = 1;
370 print "." if $arg{v};
372 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
378 next if ($dont_include_subs);
379 ($null, $subrt) = split / +/;
380 # $subrt{$subrt} = 1;
381 $cached{$file}{subr}{$subrt} = 1;
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.*?\(/;
408 my $postmatch = "$'";
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 # $locale{$string} = 1;
420 $cached{$file}{locale}{$string} = 1;
422 # this one is for all the locales
423 # $alllocales{$string} = 1;
424 $cached{$file}{all}{$string} = 1;
426 # is it a submit button before $locale->
428 # $submit{$string} = 1;
429 $cached{$file}{submit}{$string} = 1;
432 } elsif ($postmatch =~ />/) {
436 # exit loop if there are no more locales on this line
437 ($rc) = ($postmatch =~ /locale->text/);
439 if ( ($postmatch =~ />/)
440 || (!$found && ($sub_line_no != $line_no) && />/)) {
450 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
451 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
452 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
453 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
454 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
455 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
456 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
458 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
464 my $fh = new FileHandle;
465 open $fh, "$file" or die "$! : $file";
467 my @a = grep m/^\[/, <$fh>;
471 grep { s/(\[|\])//g } @a;
473 foreach my $item (@a) {
474 @b = split /--/, $item;
475 foreach $string (@b) {
477 $locale{$string} = 1;
478 $alllocales{$string} = 1;
487 if (!defined $cached{$_[0]}) {
488 my %plugins = ( 'loaded' => { }, 'needed' => { } );
490 open(IN, $_[0]) || die $_[0];
495 while (my $line = <IN>) {
498 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
499 $plugins{loaded}->{$1} = 1;
502 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
504 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator));
507 while ("" ne $line) {
509 if ($line =~ m|<translate>|i) {
511 if ($` =~ /$submitsearch/) {
514 substr($line, 0, $eom) = "";
521 if ($line =~ m|</translate>|i) {
523 substr($line, 0, $+[0]) = "";
528 # $submit{$text} = 1;
529 $cached{$_[0]}{submit}{$text} = 1;
532 # $alllocales{$text} = 1;
533 $cached{$_[0]}{all}{$text} = 1;
534 # $htmllocale{$text} = 1;
535 $cached{$_[0]}{html}{$text} = 1;
548 foreach my $plugin (keys %{ $plugins{needed} }) {
549 next if ($plugins{loaded}->{$plugin});
550 print "E: " . strip_base($_[0]) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
553 &converthtmlfile($_[0]);
556 # copy back into global arrays
557 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
558 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
559 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
562 sub converthtmlfile {
568 open(IN, $file) || die;
570 my $langcode = (split("/", getcwd()))[-1];
571 $file =~ s/_master.html$/_${langcode}.html/;
573 open(OUT, ">$file") || die;
577 while (my $line = <IN>) {
584 while ("" ne $line) {
586 if ($line =~ m|<translate>|i) {
588 substr($line, 0, $+[0]) = "";
590 print(OUT "\n") if ("" eq $line);
593 print(OUT "${line}\n");
598 if ($line =~ m|</translate>|i) {
600 substr($line, 0, $+[0]) = "";
603 $alllocales{$text} = 1;
604 $htmllocale{$text} = 1;
605 print(OUT $self->{"texts"}{$text} || $text);
606 print(OUT "\n") if ("" eq $line);
621 sub search_unused_htmlfiles {
622 my @unscanned_dirs = ('../../templates/webpages');
624 while (scalar @unscanned_dirs) {
625 my $dir = shift @unscanned_dirs;
627 foreach my $entry (<$dir/*>) {
629 push @unscanned_dirs, $entry;
631 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
632 print "W: unused HTML template: " . strip_base($entry) . "\n";
639 sub search_translated_htmlfiles_wo_master {
640 my @unscanned_dirs = ('../../templates/webpages');
642 while (scalar @unscanned_dirs) {
643 my $dir = shift @unscanned_dirs;
645 foreach my $entry (<$dir/*>) {
647 push @unscanned_dirs, $entry;
649 } elsif (($entry =~ /_[a-z]+\.html$/) && ($entry !~ /_master.html$/) && -f $entry) {
651 $master =~ s/[a-z]+\.html$/master.html/;
653 print "W: translated HTML template without master: " . strip_base($entry) . "\n";
661 my $s = "$_[0]"; # Create a copy of the string.
664 $s =~ s|templates/webpages/||;