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 $bindir = "$basedir/bin/mozilla";
17 $dbupdir = "$basedir/sql/Pg-upgrade";
18 $dbupdir2 = "$basedir/sql/Pg-upgrade2";
19 $menufile = "menu.ini";
20 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
22 %referenced_html_files = ();
27 # -h extended checks on HTML templates
29 foreach $item (@ARGV) {
34 opendir DIR, "$bindir" or die "$!";
35 @progfiles = grep { /\.pl$/ && !/(_|^\.)/ } readdir DIR;
37 @customfiles = grep /_/, readdir DIR;
40 # put customized files into @customfiles
41 @customfiles = () if ($arg{n});
44 @menufiles = ($menufile);
46 opendir DIR, "$basedir" or die "$!";
47 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
49 unshift @menufiles, $menufile;
52 opendir DIR, $dbupdir or die "$!";
53 @dbplfiles = grep { /\.pl$/ } readdir DIR;
56 opendir DIR, $dbupdir2 or die "$!";
57 @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
60 # slurp the translations in
65 # Read HTML templates.
67 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
68 #foreach $file (@htmltemplates) {
69 # scanhtmlfile($file);
72 map({ handle_file($_, $bindir); } @progfiles);
73 map({ handle_file($_, $dbupdir); } @dbplfiles);
74 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
77 my ($file, $dir) = @_;
78 print "\n$file" if $arg{v};
83 &scanfile("$dir/$file");
85 # scan custom_{module}.pl or {login}_{module}.pl files
86 foreach $customfile (@customfiles) {
87 if ($customfile =~ /_$file/) {
88 if (-f "$dir/$customfile") {
89 &scanfile("$dir/$customfile");
94 # if this is the menu.pl file
95 if ($file eq 'menu.pl') {
96 foreach $item (@menufiles) {
97 &scanmenu("$basedir/$item");
101 if ($file eq 'menunew.pl') {
102 foreach $item (@menufiles) {
103 &scanmenu("$basedir/$item");
104 print "." if $arg{v};
110 eval { require 'missing'; };
113 foreach $text (keys %$missing) {
114 if ($locale{$text} || $htmllocale{$text}) {
115 unless ($self->{texts}{$text}) {
116 $self->{texts}{$text} = $missing->{$text};
121 open FH, ">$file" or die "$! : $file";
123 print FH q|#!/usr/bin/perl
128 foreach $key (sort keys %locale) {
129 if ($self->{texts}{$key}) {
130 $text = $self->{texts}{$key};
135 $text =~ s/\\$/\\\\/;
138 $keytext =~ s/'/\\'/g;
139 $keytext =~ s/\\$/\\\\/;
141 print FH qq| '$keytext'|
142 . (' ' x (27 - length($keytext)))
143 . qq| => '$text',\n|;
151 foreach $key (sort keys %subrt) {
154 $text =~ s/\\$/\\\\/;
155 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
158 foreach $key (sort keys %submit) {
159 $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
161 $text =~ s/\\$/\\\\/;
164 $english_sub =~ s/'/\\'/g;
165 $english_sub =~ s/\\$/\\\\/;
166 $english_sub = lc $key;
168 $translated_sub = lc $text;
169 $english_sub =~ s/( |-|,)/_/g;
170 $translated_sub =~ s/( |-|,)/_/g;
171 print FH qq| '$translated_sub'|
172 . (' ' x (27 - length($translated_sub)))
173 . qq| => '$english_sub',\n|;
185 #foreach $file (@htmltemplates) {
186 # converthtmlfile($file);
191 open FH, ">all" or die "$! : all";
193 print FH q|#!/usr/bin/perl
195 # These are all the texts to build the translations files.
196 # The file has the form of 'english text' => 'foreign text',
197 # you can add the translation in this file or in the 'missing' file
198 # run locales.pl from this directory to rebuild the translation files
203 foreach $key (sort keys %alllocales) {
204 $text = $self->{texts}{$key};
209 $text =~ s/\\$/\\\\/;
218 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
230 open FH, ">missing" or die "$! : missing";
232 print FH q|#!/usr/bin/perl
234 # add the missing texts and run locales.pl to rebuild
239 foreach $text (@missing) {
240 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
252 open(FH, "LANGUAGE");
255 $trlanguage = $language[0];
259 search_unused_htmlfiles();
260 search_translated_htmlfiles_wo_master();
263 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
264 print "\n$trlanguage - ${per}%";
265 print " - $notext missing" if $notext;
272 sub extract_text_between_parenthesis {
273 my ($fh, $line) = @_;
274 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
277 if (length($line) <= $pos) {
279 return ($text, "") unless ($line);
283 my $cur_char = substr($line, $pos, 1);
285 if (!$inside_string) {
286 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
287 $inside_string = substr($line, $pos + 2, 1);
290 } elsif ((length($line) >= ($pos + 2)) &&
291 (substr($line, $pos, 1) eq "q")) {
292 $inside_string = substr($line, $pos + 1, 1);
295 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
296 $inside_string = $cur_char;
298 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
299 return ($text, substr($line, $pos + 1));
307 } elsif ($cur_char eq '\\') {
311 } elsif ($cur_char eq $inside_string) {
312 undef($inside_string);
325 my $dont_include_subs = shift;
326 my $scanned_files = shift;
328 $scanned_files = {} unless ($scanned_files);
329 return if ($scanned_files->{$file});
330 $scanned_files->{$file} = 1;
332 if (!defined $cached{$file}) {
334 return unless (-f "$file");
336 my $fh = new FileHandle;
337 open $fh, "$file" or die "$! : $file";
339 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
344 # is this another file
345 if (/require\s+\W.*\.pl/) {
347 $newfile =~ s/require\s+\W//;
348 $newfile =~ s|bin/mozilla||;
349 # &scanfile("$bindir/$newfile", 0, $scanned_files);
350 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
351 } elsif (/use\s+SL::(.*?);/) {
354 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
355 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
358 # is this a template call?
359 if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
360 my $newfile = "$basedir/templates/webpages/$1_master.html";
361 if (/parse_html_template2/) {
362 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
365 # &scanhtmlfile($newfile);
366 # &converthtmlfile($newfile);
367 $cached{$file}{scanh}{$newfile} = 1;
368 print "." if $arg{v};
370 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
376 next if ($dont_include_subs);
377 ($null, $subrt) = split / +/;
378 # $subrt{$subrt} = 1;
379 $cached{$file}{subr}{$subrt} = 1;
388 my ($null, $country) = split /,/;
389 $country =~ s/^ +[\"\']//;
390 $country =~ s/[\"\'].*//;
396 # is it a submit button before $locale->
397 if (/$submitsearch/) {
399 if ($` !~ /locale->text/) {
401 $sub_line_no = $line_no;
405 my ($found) = /locale->text.*?\(/;
406 my $postmatch = "$'";
410 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
413 # if there is no $ in the string record it
414 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
416 # this guarantees one instance of string
417 # $locale{$string} = 1;
418 $cached{$file}{locale}{$string} = 1;
420 # this one is for all the locales
421 # $alllocales{$string} = 1;
422 $cached{$file}{all}{$string} = 1;
424 # is it a submit button before $locale->
426 # $submit{$string} = 1;
427 $cached{$file}{submit}{$string} = 1;
430 } elsif ($postmatch =~ />/) {
434 # exit loop if there are no more locales on this line
435 ($rc) = ($postmatch =~ /locale->text/);
437 if ( ($postmatch =~ />/)
438 || (!$found && ($sub_line_no != $line_no) && />/)) {
448 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
449 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
450 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
451 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
452 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
453 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
454 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
456 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
462 my $fh = new FileHandle;
463 open $fh, "$file" or die "$! : $file";
465 my @a = grep m/^\[/, <$fh>;
469 grep { s/(\[|\])//g } @a;
471 foreach my $item (@a) {
472 @b = split /--/, $item;
473 foreach $string (@b) {
475 $locale{$string} = 1;
476 $alllocales{$string} = 1;
485 if (!defined $cached{$_[0]}) {
487 open(IN, $_[0]) || die $_[0];
492 while (my $line = <IN>) {
495 while ("" ne $line) {
497 if ($line =~ m|<translate>|i) {
499 if ($` =~ /$submitsearch/) {
502 substr($line, 0, $eom) = "";
509 if ($line =~ m|</translate>|i) {
511 substr($line, 0, $+[0]) = "";
516 # $submit{$text} = 1;
517 $cached{$_[0]}{submit}{$text} = 1;
520 # $alllocales{$text} = 1;
521 $cached{$_[0]}{all}{$text} = 1;
522 # $htmllocale{$text} = 1;
523 $cached{$_[0]}{html}{$text} = 1;
535 &converthtmlfile($_[0]);
538 # copy back into global arrays
539 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
540 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
541 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
544 sub converthtmlfile {
550 open(IN, $file) || die;
552 my $langcode = (split("/", getcwd()))[-1];
553 $file =~ s/_master.html$/_${langcode}.html/;
555 open(OUT, ">$file") || die;
559 while (my $line = <IN>) {
566 while ("" ne $line) {
568 if ($line =~ m|<translate>|i) {
570 substr($line, 0, $+[0]) = "";
572 print(OUT "\n") if ("" eq $line);
575 print(OUT "${line}\n");
580 if ($line =~ m|</translate>|i) {
582 substr($line, 0, $+[0]) = "";
585 $alllocales{$text} = 1;
586 $htmllocale{$text} = 1;
587 print(OUT $self->{"texts"}{$text} || $text);
588 print(OUT "\n") if ("" eq $line);
603 sub search_unused_htmlfiles {
604 my @unscanned_dirs = ('../../templates/webpages');
606 while (scalar @unscanned_dirs) {
607 my $dir = shift @unscanned_dirs;
609 foreach my $entry (<$dir/*>) {
611 push @unscanned_dirs, $entry;
613 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
614 print "W: unused HTML template: " . strip_base($entry) . "\n";
621 sub search_translated_htmlfiles_wo_master {
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 =~ /_[a-z]+\.html$/) && ($entry !~ /_master.html$/) && -f $entry) {
633 $master =~ s/[a-z]+\.html$/master.html/;
635 print "W: translated HTML template without master: " . strip_base($entry) . "\n";
643 my $s = "$_[0]"; # Create a copy of the string.
646 $s =~ s|templates/webpages/||;