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 foreach $item (@ARGV) {
27 opendir DIR, "$bindir" or die "$!";
28 @progfiles = grep { /\.pl/; !/(_|^\.)/ } readdir DIR;
30 @customfiles = grep /_/, readdir DIR;
33 # put customized files into @customfiles
34 @customfiles = () if ($arg{n});
37 @menufiles = ($menufile);
39 opendir DIR, "$basedir" or die "$!";
40 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
42 unshift @menufiles, $menufile;
45 opendir DIR, $dbupdir or die "$!";
46 @dbplfiles = grep { /\.pl$/ } readdir DIR;
49 opendir DIR, $dbupdir2 or die "$!";
50 @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
53 # slurp the translations in
58 # Read HTML templates.
60 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
61 #foreach $file (@htmltemplates) {
62 # scanhtmlfile($file);
65 map({ handle_file($_, $bindir); } @progfiles);
66 map({ handle_file($_, $dbupdir); } @dbplfiles);
67 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
70 my ($file, $dir) = @_;
71 print "\n$file" if $arg{v};
76 &scanfile("$dir/$file");
78 # scan custom_{module}.pl or {login}_{module}.pl files
79 foreach $customfile (@customfiles) {
80 if ($customfile =~ /_$file/) {
81 if (-f "$dir/$customfile") {
82 &scanfile("$dir/$customfile");
87 # if this is the menu.pl file
88 if ($file eq 'menu.pl') {
89 foreach $item (@menufiles) {
90 &scanmenu("$basedir/$item");
94 if ($file eq 'menunew.pl') {
95 foreach $item (@menufiles) {
96 &scanmenu("$basedir/$item");
103 eval { require 'missing'; };
106 foreach $text (keys %$missing) {
107 if ($locale{$text} || $htmllocale{$text}) {
108 unless ($self->{texts}{$text}) {
109 $self->{texts}{$text} = $missing->{$text};
114 open FH, ">$file" or die "$! : $file";
116 print FH q|$self->{texts} = {
119 foreach $key (sort keys %locale) {
120 if ($self->{texts}{$key}) {
121 $text = $self->{texts}{$key};
126 $text =~ s/\\$/\\\\/;
129 $keytext =~ s/'/\\'/g;
130 $keytext =~ s/\\$/\\\\/;
132 print FH qq| '$keytext'|
133 . (' ' x (27 - length($keytext)))
134 . qq| => '$text',\n|;
142 foreach $key (sort keys %subrt) {
145 $text =~ s/\\$/\\\\/;
146 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
149 foreach $key (sort keys %submit) {
150 $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
152 $text =~ s/\\$/\\\\/;
155 $english_sub =~ s/'/\\'/g;
156 $english_sub =~ s/\\$/\\\\/;
157 $english_sub = lc $key;
159 $translated_sub = lc $text;
160 $english_sub =~ s/( |-|,)/_/g;
161 $translated_sub =~ s/( |-|,)/_/g;
162 print FH qq| '$translated_sub'|
163 . (' ' x (27 - length($translated_sub)))
164 . qq| => '$english_sub',\n|;
176 #foreach $file (@htmltemplates) {
177 # converthtmlfile($file);
182 open FH, ">all" or die "$! : all";
184 print FH q|# These are all the texts to build the translations files.
185 # The file has the form of 'english text' => 'foreign text',
186 # you can add the translation in this file or in the 'missing' file
187 # run locales.pl from this directory to rebuild the translation files
192 foreach $key (sort keys %alllocales) {
193 $text = $self->{texts}{$key};
198 $text =~ s/\\$/\\\\/;
207 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
219 open FH, ">missing" or die "$! : missing";
221 print FH q|# add the missing texts and run locales.pl to rebuild
226 foreach $text (@missing) {
227 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
239 open(FH, "LANGUAGE");
242 $trlanguage = $language[0];
245 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
246 print "\n$trlanguage - ${per}%";
247 print " - $notext missing" if $notext;
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 ")") {
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;
310 $scanned_files = {} unless ($scanned_files);
311 return if ($scanned_files->{$file});
312 $scanned_files->{$file} = 1;
314 if (!defined $cached{$file}) {
316 return unless (-f "$file");
318 my $fh = new FileHandle;
319 open $fh, "$file" or die "$! : $file";
321 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
326 # is this another file
327 if (/require\s+\W.*\.pl/) {
329 $newfile =~ s/require\s+\W//;
330 $newfile =~ s/\$form->{path}\///;
331 # &scanfile("$bindir/$newfile", 0, $scanned_files);
332 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
333 } elsif (/use\s+SL::(.*?);/) {
334 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
335 $cached{$file}{scannosubs}{"../../SL/${1}.pm"} = 1;
338 # is this a template call?
339 if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)/) {
340 my $newfile = "$basedir/templates/webpages/$1_master.html";
342 # &scanhtmlfile($newfile);
343 # &converthtmlfile($newfile);
344 $cached{$file}{scanh}{$newfile} = 1;
345 print "." if $arg{v};
351 next if ($dont_include_subs);
352 ($null, $subrt) = split / +/;
353 # $subrt{$subrt} = 1;
354 $cached{$file}{subr}{$subrt} = 1;
363 my ($null, $country) = split /,/;
364 $country =~ s/^ +[\"\']//;
365 $country =~ s/[\"\'].*//;
371 # is it a submit button before $locale->
372 if (/$submitsearch/) {
374 if ($` !~ /locale->text/) {
376 $sub_line_no = $line_no;
380 my ($found) = /locale->text.*?\(/;
381 my $postmatch = "$'";
385 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
388 # if there is no $ in the string record it
389 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
391 # this guarantees one instance of string
392 # $locale{$string} = 1;
393 $cached{$file}{locale}{$string} = 1;
395 # this one is for all the locales
396 # $alllocales{$string} = 1;
397 $cached{$file}{all}{$string} = 1;
399 # is it a submit button before $locale->
401 # $submit{$string} = 1;
402 $cached{$file}{submit}{$string} = 1;
405 } elsif ($postmatch =~ />/) {
409 # exit loop if there are no more locales on this line
410 ($rc) = ($postmatch =~ /locale->text/);
412 if ( ($postmatch =~ />/)
413 || (!$found && ($sub_line_no != $line_no) && />/)) {
423 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
424 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
425 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
426 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
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}};
435 my $fh = new FileHandle;
436 open $fh, "$file" or die "$! : $file";
438 my @a = grep /^\[/, <$fh>;
442 grep { s/(\[|\])//g } @a;
444 foreach my $item (@a) {
445 @b = split /--/, $item;
446 foreach $string (@b) {
448 $locale{$string} = 1;
449 $alllocales{$string} = 1;
458 if (!defined $cached{$_[0]}) {
460 open(IN, $_[0]) || die $_[0];
465 while (my $line = <IN>) {
468 while ("" ne $line) {
470 if ($line =~ m|<translate>|i) {
472 if ($` =~ /$submitsearch/) {
475 substr($line, 0, $eom) = "";
482 if ($line =~ m|</translate>|i) {
484 substr($line, 0, $+[0]) = "";
489 # $submit{$text} = 1;
490 $cached{$_[0]}{submit}{$text} = 1;
493 # $alllocales{$text} = 1;
494 $cached{$_[0]}{all}{$text} = 1;
495 # $htmllocale{$text} = 1;
496 $cached{$_[0]}{html}{$text} = 1;
508 &converthtmlfile($_[0]);
511 # copy back into global arrays
512 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
513 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
514 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
517 sub converthtmlfile {
523 open(IN, $file) || die;
525 my $langcode = (split("/", getcwd()))[-1];
526 $file =~ s/_master.html$/_${langcode}.html/;
528 open(OUT, ">$file") || die;
532 while (my $line = <IN>) {
539 while ("" ne $line) {
541 if ($line =~ m|<translate>|i) {
543 substr($line, 0, $+[0]) = "";
545 print(OUT "\n") if ("" eq $line);
548 print(OUT "${line}\n");
553 if ($line =~ m|</translate>|i) {
555 substr($line, 0, $+[0]) = "";
558 $alllocales{$text} = 1;
559 $htmllocale{$text} = 1;
560 print(OUT $self->{"texts"}{$text} || $text);
561 print(OUT "\n") if ("" eq $line);