3 # -n do not include custom_ scripts
5 # this version of locles processes not only all required .pl files
6 # but also all parse_html_templated files.
13 $bindir = "$basedir/bin/mozilla";
14 $menufile = "menu.ini";
15 $submitsearch = qr/type\s*=\s*["']?submit/i;
17 foreach $item (@ARGV) {
22 opendir DIR, "$bindir" or die "$!";
23 @progfiles = grep { /\.pl$/ && !/(_|^\.)/ } readdir DIR;
25 @customfiles = grep /_/, readdir DIR;
28 # put customized files into @customfiles
29 @customfiles = () if ($arg{n});
32 @menufiles = ($menufile);
34 opendir DIR, "$basedir" or die "$!";
35 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
37 unshift @menufiles, $menufile;
40 # slurp the translations in
45 # Read HTML templates.
47 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
48 #foreach $file (@htmltemplates) {
49 # scanhtmlfile($file);
52 foreach $file (@progfiles) {
58 &scanfile("$bindir/$file");
60 # scan custom_{module}.pl or {login}_{module}.pl files
61 foreach $customfile (@customfiles) {
62 if ($customfile =~ /_$file/) {
63 if (-f "$bindir/$customfile") {
64 &scanfile("$bindir/$customfile");
69 # if this is the menu.pl file
70 if ($file eq 'menu.pl') {
71 foreach $item (@menufiles) {
72 &scanmenu("$basedir/$item");
76 if ($file eq 'menunew.pl') {
77 foreach $item (@menufiles) {
78 &scanmenu("$basedir/$item");
84 eval { require 'missing'; };
87 foreach $text (keys %$missing) {
88 if ($locale{$text} || $htmllocale{$text}) {
89 unless ($self{texts}{$text}) {
90 $self{texts}{$text} = $missing->{$text};
95 open FH, ">$file" or die "$! : $file";
97 print FH q|$self{texts} = {
100 foreach $key (sort keys %locale) {
101 if ($self{texts}{$key}) {
102 $text = $self{texts}{$key};
107 $text =~ s/\\$/\\\\/;
110 $keytext =~ s/'/\\'/g;
111 $keytext =~ s/\\$/\\\\/;
113 print FH qq| '$keytext'|
114 . (' ' x (27 - length($keytext)))
115 . qq| => '$text',\n|;
123 foreach $key (sort keys %subrt) {
126 $text =~ s/\\$/\\\\/;
127 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
130 foreach $key (sort keys %submit) {
131 $text = ($self{texts}{$key}) ? $self{texts}{$key} : $key;
133 $text =~ s/\\$/\\\\/;
136 $english_sub =~ s/'/\\'/g;
137 $english_sub =~ s/\\$/\\\\/;
138 $english_sub = lc $key;
140 $translated_sub = lc $text;
141 $english_sub =~ s/( |-|,)/_/g;
142 $translated_sub =~ s/( |-|,)/_/g;
143 print FH qq| '$translated_sub'|
144 . (' ' x (27 - length($translated_sub)))
145 . qq| => '$english_sub',\n|;
156 foreach $file (@htmltemplates) {
157 converthtmlfile($file);
162 open FH, ">all" or die "$! : all";
164 print FH q|# These are all the texts to build the translations files.
165 # The file has the form of 'english text' => 'foreign text',
166 # you can add the translation in this file or in the 'missing' file
167 # run locales.pl from this directory to rebuild the translation files
172 foreach $key (sort keys %alllocales) {
173 $text = $self{texts}{$key};
178 $text =~ s/\\$/\\\\/;
187 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
199 open FH, ">missing" or die "$! : missing";
201 print FH q|# add the missing texts and run locales.pl to rebuild
206 foreach $text (@missing) {
207 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
219 open(FH, "LANGUAGE");
222 $trlanguage = $language[0];
225 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
226 print "\n$trlanguage - ${per}%\n";
232 sub extract_text_between_parenthesis {
233 my ($fh, $line) = @_;
234 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
237 if (length($line) <= $pos) {
239 return ($text, "") unless ($line);
243 my $cur_char = substr($line, $pos, 1);
245 if (!$inside_string) {
246 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
247 $inside_string = substr($line, $pos + 2, 1);
250 } elsif ((length($line) >= ($pos + 2)) &&
251 (substr($line, $pos, 1) eq "q")) {
252 $inside_string = substr($line, $pos + 1, 1);
255 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
256 $inside_string = $cur_char;
258 } elsif ($cur_char eq ")") {
259 return ($text, substr($line, $pos + 1));
267 } elsif ($cur_char eq '\\') {
271 } elsif ($cur_char eq $inside_string) {
272 undef($inside_string);
286 return unless (-f "$file");
288 my $fh = new FileHandle;
289 open $fh, "$file" or die "$! : $file";
291 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
296 # is this another file
297 if (/require\s+\W.*\.pl/) {
299 $newfile =~ s/require\s+\W//;
300 $newfile =~ s/\$form->{path}\///;
301 &scanfile("$bindir/$newfile");
304 # is this a template call?
305 if (/parse_html_template\s*\(\s*["']([\w\/]+)/) {
306 my $newfile = "$basedir/templates/webpages/$1_master.html";
308 &scanhtmlfile($newfile);
309 &converthtmlfile($newfile);
315 ($null, $subrt) = split / +/;
325 my ($null, $country) = split /,/;
326 $country =~ s/^ +[\"\']//;
327 $country =~ s/[\"\'].*//;
333 # is it a submit button before $locale->
334 if (/$submitsearch/) {
336 if ($` !~ /\$locale->text/) {
338 $sub_line_no = $line_no;
342 my ($found) = /\$locale->text.*?\(/;
347 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
350 # if there is no $ in the string record it
351 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
353 # this guarantees one instance of string
354 $locale{$string} = 1;
356 # this one is for all the locales
357 $alllocales{$string} = 1;
359 # is it a submit button before $locale->
361 $submit{$string} = 1;
364 } elsif ($postmatch =~ />/) {
368 # exit loop if there are no more locales on this line
369 ($rc) = ($postmatch =~ /\$locale->text/);
372 s/^.*?\$locale->text.*?\)//;
374 if ( ($postmatch =~ />/)
375 || (!$found && ($sub_line_no != $line_no) && />/)) {
388 my $fh = new FileHandle;
389 open $fh, "$file" or die "$! : $file";
391 my @a = grep /^\[/, <$fh>;
395 grep { s/(\[|\])//g } @a;
397 foreach my $item (@a) {
398 @b = split /--/, $item;
399 foreach $string (@b) {
401 $locale{$string} = 1;
402 $alllocales{$string} = 1;
411 open(IN, $_[0]) || die $_[0];
416 while (my $line = <IN>) {
419 while ("" ne $line) {
421 if ($line =~ m|<translate>|i) {
423 if ($` =~ /$submitsearch/) {
426 substr($line, 0, $eom) = "";
433 if ($line =~ m|</translate>|i) {
435 substr($line, 0, $+[0]) = "";
442 $alllocales{$text} = 1;
443 $htmllocale{$text} = 1;
457 sub converthtmlfile {
463 open(IN, $file) || die;
465 my $langcode = (split("/", getcwd()))[-1];
466 $file =~ s/_master.html$/_${langcode}.html/;
468 open(OUT, ">$file") || die;
472 while (my $line = <IN>) {
479 while ("" ne $line) {
481 if ($line =~ m|<translate>|i) {
483 substr($line, 0, $+[0]) = "";
485 print(OUT "\n") if ("" eq $line);
488 print(OUT "${line}\n");
493 if ($line =~ m|</translate>|i) {
495 substr($line, 0, $+[0]) = "";
497 $alllocales{$text} = 1;
498 $htmllocale{$text} = 1;
499 print(OUT $self{"texts"}{$text});
500 print(OUT "\n") if ("" eq $line);