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 File::Slurp qw(slurp);
19 use List::Util qw(first);
23 $OUTPUT_AUTOFLUSH = 1;
33 my $basedir = "../..";
34 my $locales_dir = ".";
35 my $bindir = "$basedir/bin/mozilla";
36 my @progdirs = ( "$basedir/SL" );
37 my $dbupdir = "$basedir/sql/Pg-upgrade";
38 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
39 my $menufile = "menu.ini";
40 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
41 my $javascript_output_dir = $basedir .'/js';
42 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
47 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
48 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
53 my ($top_dir_name) = @_;
60 tie my %dir_h, 'IO::Dir', $dir_name;
62 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
63 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
65 $finder->($_) for @sub_dirs;
68 $finder->($top_dir_name);
74 # overwrite existing entries with the ones from 'missing'
75 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
77 # try to set missing entries from lost ones
78 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
79 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
82 my @bindir_files = find_files($bindir);
83 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
84 my @customfiles = grep /_custom/, @bindir_files;
86 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
88 # put customized files into @customfiles
89 my (@menufiles, %dir_h);
93 @menufiles = ($menufile);
95 tie %dir_h, 'IO::Dir', $basedir;
96 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
97 unshift @menufiles, "$basedir/$menufile";
100 tie %dir_h, 'IO::Dir', $dbupdir;
101 my @dbplfiles = grep { /\.pl$/ } keys %dir_h;
103 tie %dir_h, 'IO::Dir', $dbupdir2;
104 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
106 # slurp the translations in
107 if (-f "$locales_dir/all") {
108 require "$locales_dir/all";
110 if (-f "$locales_dir/missing") {
111 require "$locales_dir/missing" ;
112 unlink "$locales_dir/missing";
114 if (-f "$locales_dir/lost") {
115 require "$locales_dir/lost";
116 unlink "$locales_dir/lost";
119 my $charset = slurp("$locales_dir/charset") || 'utf-8';
122 my %old_texts = %{ $self->{texts} || {} };
124 handle_file(@{ $_ }) for @progfiles;
125 handle_file($_, $dbupdir) for @dbplfiles;
126 handle_file($_, $dbupdir2) for @dbplfiles2;
127 scanmenu($_) for @menufiles;
129 for my $file_name (map({find_files($_)} @javascript_dirs)) {
130 scan_javascript_file($file_name);
133 # merge entries to translate with entries from files 'missing' and 'lost'
138 file => "$locales_dir/all",
139 header => $ALL_HEADER,
140 data_name => '$self->{texts}',
141 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
144 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'js') || die;
147 for my $key (sort(keys(%jslocale))) {
148 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
151 print $js_file ("\n".'}'."\n");
154 foreach my $text (keys %$missing) {
155 if ($locale{$text} || $htmllocale{$text}) {
156 unless ($self->{texts}{$text}) {
157 $self->{texts}{$text} = $missing->{$text};
163 # calc and generate missing
164 my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
168 file => "$locales_dir/missing",
169 header => $MISSING_HEADER,
170 data_name => '$missing',
171 data_sub => sub { _print_line($_, '', @_) for @new_missing },
175 # calc and generate lost
176 while (my ($text, $translation) = each %old_texts) {
177 next if ($alllocales{$text});
178 push @lost, { 'text' => $text, 'translation' => $translation };
182 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
184 file => "$locales_dir/lost",
185 header => $LOST_HEADER,
187 data_name => '@lost',
189 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
194 my $trlanguage = slurp("$locales_dir/LANGUAGE");
197 search_unused_htmlfiles() if $opt_c;
199 my $count = scalar keys %alllocales;
200 my $notext = scalar @new_missing;
201 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
202 print "\n$trlanguage - ${per}%";
203 print " - $notext/$count missing" if $notext;
212 # These are all the texts to build the translations files.
213 # The file has the form of 'english text' => 'foreign text',
214 # you can add the translation in this file or in the 'missing' file
215 # run locales.pl from this directory to rebuild the translation files
217 $MISSING_HEADER = <<EOL;
218 # add the missing texts and run locales.pl to rebuild
220 $LOST_HEADER = <<EOL;
221 # The last 50 text strings, that have been removed.
222 # This file has been auto-generated by locales.pl. Please don't edit!
230 'no-custom-files' => \$opt_n,
231 'check-files' => \$opt_c,
232 'verbose' => \$opt_v,
244 pod2usage(-exitstatus => 0, -verbose => 2);
249 my $arg = shift @ARGV;
251 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
252 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
261 print "The locale directory '$arg' could not be found.\n";
265 } elsif (!-f 'all' || !-f 'LANGUAGE') {
266 print "locales.pl was not called from a locale/* subdirectory,\n"
267 . "and no locale directory name was given.\n";
273 my ($file, $dir) = @_;
274 print "\n$file" if $opt_v;
278 &scanfile("$dir/$file");
280 # scan custom_{module}.pl or {login}_{module}.pl files
281 foreach my $customfile (@customfiles) {
282 if ($customfile =~ /_$file/) {
283 if (-f "$dir/$customfile") {
284 &scanfile("$dir/$customfile");
292 sub extract_text_between_parenthesis {
293 my ($fh, $line) = @_;
294 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
297 if (length($line) <= $pos) {
299 return ($text, "") unless ($line);
303 my $cur_char = substr($line, $pos, 1);
305 if (!$inside_string) {
306 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
307 $inside_string = substr($line, $pos + 2, 1);
310 } elsif ((length($line) >= ($pos + 2)) &&
311 (substr($line, $pos, 1) eq "q")) {
312 $inside_string = substr($line, $pos + 1, 1);
315 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
316 $inside_string = $cur_char;
318 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
319 return ($text, substr($line, $pos + 1));
324 $text .= '\\' unless $cur_char eq "'";
328 } elsif ($cur_char eq '\\') {
331 } elsif ($cur_char eq $inside_string) {
332 undef($inside_string);
345 my $dont_include_subs = shift;
346 my $scanned_files = shift;
351 $scanned_files = {} unless ($scanned_files);
352 return if ($scanned_files->{$file});
353 $scanned_files->{$file} = 1;
355 if (!defined $cached{$file}) {
357 return unless (-f "$file");
359 my $fh = new FileHandle;
360 open $fh, "$file" or die "$! : $file";
362 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
365 last if /^\s*__END__/;
369 # is this another file
370 if (/require\s+\W.*\.pl/) {
372 $newfile =~ s/require\s+\W//;
373 $newfile =~ s|bin/mozilla||;
374 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
375 } elsif (/use\s+SL::([\w:]*)/) {
378 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
381 # Some calls to render() are split over multiple lines. Deal
383 while (/(?:parse_html_template2?|render)\s*\( *$/) {
388 # is this a template call?
389 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
390 my $new_file_base = "$basedir/templates/webpages/$1.";
391 if (/parse_html_template2/) {
392 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
396 foreach my $ext (qw(html js json)) {
397 my $new_file = "${new_file_base}${ext}";
399 $cached{$file}{scanh}{$new_file} = 1;
405 if ($opt_c && !$found_one) {
406 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
415 my ($null, $country) = split(/,/);
416 $country =~ s/^ +[\"\']//;
417 $country =~ s/[\"\'].*//;
423 # is it a submit button before $locale->
424 if (/$submitsearch/) {
426 if ($` !~ /locale->text/) {
428 $sub_line_no = $line_no;
432 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
437 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
440 # if there is no $ in the string record it
441 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
443 # this guarantees one instance of string
444 $cached{$file}{locale}{$string} = 1;
446 # this one is for all the locales
447 $cached{$file}{all}{$string} = 1;
449 # is it a submit button before $locale->
451 $cached{$file}{submit}{$string} = 1;
454 } elsif ($postmatch =~ />/) {
458 # exit loop if there are no more locales on this line
459 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
461 if ( ($postmatch =~ />/)
462 || (!$found && ($sub_line_no != $line_no) && />/)) {
472 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
473 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
474 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
476 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
477 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
478 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
480 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
486 my $fh = new FileHandle;
487 open $fh, "$file" or die "$! : $file";
489 my @a = grep m/^\[/, <$fh>;
493 grep { s/(\[|\])//g } @a;
495 foreach my $item (@a) {
496 my @b = split /--/, $item;
497 foreach my $string (@b) {
499 $locale{$string} = 1;
500 $alllocales{$string} = 1;
506 sub unescape_template_string {
517 if (!defined $cached{$file}) {
518 my %plugins = ( 'loaded' => { }, 'needed' => { } );
520 open(IN, $file) || die $file;
525 while (my $line = <IN>) {
528 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
529 $plugins{loaded}->{$1} = 1;
532 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
534 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
537 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
539 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
540 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
541 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
542 (.*?) # Der zu übersetzende String ::Parameter $3::
543 (?<!\\)\2 # Ende des zu übersetzenden Strings
544 | # Start von Variante 2: [% '...' | $T8 %]
545 \[\% # Template-Start-Tag
546 [\-~#]? # Whitespace-Unterdrückung
547 \s* # Optional beliebig viele Whitespace
548 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
549 (.*?) # Der zu übersetzende String ::Parameter $5::
550 (?<!\\)\4 # Ende des zu übersetzenden Strings
551 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
552 (\$T8) # Filteraufruf ::Parameter $6::
553 .*? # Optionale Argumente für den Filter
555 [\-~#]? # Whitespace-Unterdrückung
556 \%\] # Template-Ende-Tag
559 my $module = $1 || $6;
560 my $string = $3 || $5;
561 print "Found filter >>>$string<<<\n" if $debug;
562 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
564 $string = unescape_template_string($string);
565 $cached{$file}{all}{$string} = 1;
566 $cached{$file}{html}{$string} = 1;
567 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
568 $plugins{needed}->{T8} = 1 if $module eq '$T8';
569 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
572 while ($line =~ m/\[\% # Template-Start-Tag
573 [\-~#]? # Whitespace-Unterdrückung
574 \s* # Optional beliebig viele Whitespace
575 (?: # Die erkannten Template-Direktiven
580 \s+ # Mindestens ein Whitespace
581 [\'\"]? # Anfang des Dateinamens
582 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
583 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
585 my $new_file_name = "$basedir/templates/webpages/$1.html";
586 $cached{$file}{scanh}{$new_file_name} = 1;
587 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
593 foreach my $plugin (keys %{ $plugins{needed} }) {
594 next if ($plugins{loaded}->{$plugin});
595 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
599 # copy back into global arrays
600 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
601 $locale{$_} = 1 for keys %{$cached{$file}{html}};
602 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
604 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
606 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
609 sub scan_javascript_file {
612 open(my $fh, $file) || die('can not open file: '. $file);
614 while( my $line = readline($fh) ) {
625 my $text = unescape_template_string($2);
627 $jslocale{$text} = 1;
628 $alllocales{$text} = 1;
634 sub search_unused_htmlfiles {
635 my @unscanned_dirs = ('../../templates/webpages');
637 while (scalar @unscanned_dirs) {
638 my $dir = shift @unscanned_dirs;
640 foreach my $entry (<$dir/*>) {
642 push @unscanned_dirs, $entry;
644 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
645 print "W: unused HTML template: " . strip_base($entry) . "\n";
653 my $s = "$_[0]"; # Create a copy of the string.
656 $s =~ s|templates/webpages/||;
663 $val =~ s/(\'|\\$)/\\$1/g;
664 return "'" . $val . "'";
669 $val =~ s/(\"|\\$)/\\$1/g;
670 return '"'. $val .'"';
674 my $key = _single_quote(shift);
675 my $text = _single_quote(shift);
677 my $template = $params{template} || qq| %-29s => %s,\n|;
678 my $fh = $params{fh} || croak 'need filehandle in _print_line';
680 print $fh sprintf $template, $key, $text;
686 my $file = $params{file} || croak 'need filename in generate_file';
687 my $header = $params{header};
688 my $lines = $params{data_sub};
689 my $data_name = $params{data_name};
690 my @delim = split //, ($params{delim} || '{}');
692 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
694 $charset =~ s/\r?\n//g;
695 my $emacs_charset = lc $charset;
697 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
698 print $fh $header, "\n" if $header;
699 print $fh "$data_name = $delim[0]\n" if $data_name;
703 print $fh qq|$delim[1];\n\n1;\n|;
709 do { local ( @ARGV, $/ ) = $file; <> }
716 locales.pl - Collect strings for translation in kivitendo
720 locales.pl [options] lang_code
723 -n, --no-custom-files Do not process files whose name contains "_"
724 -c, --check-files Run extended checks on HTML files
725 -v, --verbose Be more verbose
726 -h, --help Show this help
732 =item B<-n>, B<--no-custom-files>
734 Do not process files whose name contains "_", e.g. "custom_io.pl".
736 =item B<-c>, B<--check-files>
738 Run extended checks on the usage of templates. This can be used to
739 discover HTML templates that are never used as well as the usage of
740 non-existing HTML templates.
742 =item B<-v>, B<--verbose>
750 This script collects strings from Perl files, the menu.ini file and
751 HTML templates and puts them into the file "all" for translation.