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 use File::Slurp qw(slurp);
20 use List::Util qw(first);
24 $OUTPUT_AUTOFLUSH = 1;
34 my $basedir = "../..";
35 my $locales_dir = ".";
36 my $bindir = "$basedir/bin/mozilla";
37 my @progdirs = ( "$basedir/SL" );
38 my $menufile = "menu.ini";
39 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
40 my $javascript_output_dir = $basedir .'/js';
41 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
46 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
47 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
52 my ($top_dir_name) = @_;
59 tie my %dir_h, 'IO::Dir', $dir_name;
61 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
62 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
64 $finder->($_) for @sub_dirs;
67 $finder->($top_dir_name);
73 # overwrite existing entries with the ones from 'missing'
74 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
76 # try to set missing entries from lost ones
77 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
78 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
81 my @bindir_files = find_files($bindir);
82 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
83 my @customfiles = grep /_custom/, @bindir_files;
85 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
87 # put customized files into @customfiles
88 my (@menufiles, %dir_h);
92 @menufiles = ($menufile);
94 tie %dir_h, 'IO::Dir', $basedir;
95 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
96 unshift @menufiles, "$basedir/$menufile";
100 foreach my $sub_dir ("Pg-upgrade", "Pg-upgrade2", "Pg-upgrade2-auth") {
101 my $dir = "$basedir/sql/$sub_dir";
102 tie %dir_h, 'IO::Dir', $dir;
103 push @dbplfiles, map { [ $_, $dir ] } 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(@{ $_ }) for @dbplfiles;
126 scanmenu($_) for @menufiles;
128 for my $file_name (map({find_files($_)} @javascript_dirs)) {
129 scan_javascript_file($file_name);
132 # merge entries to translate with entries from files 'missing' and 'lost'
137 file => "$locales_dir/all",
138 header => $ALL_HEADER,
139 data_name => '$self->{texts}',
140 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
143 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'.js') || die;
144 print $js_file 'namespace("kivi").setupLocale({';
146 for my $key (sort(keys(%jslocale))) {
147 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
150 print $js_file ("\n");
151 print $js_file ('});'."\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 my %existing_lc = map { (lc $_ => $_) } grep { $self->{texts}->{$_} } keys %{ $self->{texts} };
169 foreach my $entry (@new_missing) {
170 my $other = $existing_lc{lc $entry};
171 print "W: No entry for '${entry}' exists, but there is one with different case: '${other}'\n" if $other;
176 file => "$locales_dir/missing",
177 header => $MISSING_HEADER,
178 data_name => '$missing',
179 data_sub => sub { _print_line($_, '', @_) for @new_missing },
183 # calc and generate lost
184 while (my ($text, $translation) = each %old_texts) {
185 next if ($alllocales{$text});
186 push @lost, { 'text' => $text, 'translation' => $translation };
190 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
192 file => "$locales_dir/lost",
193 header => $LOST_HEADER,
195 data_name => '@lost',
197 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
202 my $trlanguage = slurp("$locales_dir/LANGUAGE");
205 search_unused_htmlfiles() if $opt_c;
207 my $count = scalar keys %alllocales;
208 my $notext = scalar @new_missing;
209 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
210 print "\n$trlanguage - ${per}%";
211 print " - $notext/$count missing" if $notext;
220 # These are all the texts to build the translations files.
221 # The file has the form of 'english text' => 'foreign text',
222 # you can add the translation in this file or in the 'missing' file
223 # run locales.pl from this directory to rebuild the translation files
225 $MISSING_HEADER = <<EOL;
226 # add the missing texts and run locales.pl to rebuild
228 $LOST_HEADER = <<EOL;
229 # The last 50 text strings, that have been removed.
230 # This file has been auto-generated by locales.pl. Please don't edit!
238 'no-custom-files' => \$opt_n,
239 'check-files' => \$opt_c,
240 'verbose' => \$opt_v,
252 pod2usage(-exitstatus => 0, -verbose => 2);
257 my $arg = shift @ARGV;
259 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
260 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
269 print "The locale directory '$arg' could not be found.\n";
273 } elsif (!-f 'all' || !-f 'LANGUAGE') {
274 print "locales.pl was not called from a locale/* subdirectory,\n"
275 . "and no locale directory name was given.\n";
279 $locale ||= (grep { $_ } split m:/:, getcwd())[-1];
284 my ($file, $dir) = @_;
285 print "\n$file" if $opt_v;
289 &scanfile("$dir/$file");
291 # scan custom_{module}.pl or {login}_{module}.pl files
292 foreach my $customfile (@customfiles) {
293 if ($customfile =~ /_$file/) {
294 if (-f "$dir/$customfile") {
295 &scanfile("$dir/$customfile");
303 sub extract_text_between_parenthesis {
304 my ($fh, $line) = @_;
305 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
308 if (length($line) <= $pos) {
310 return ($text, "") unless ($line);
314 my $cur_char = substr($line, $pos, 1);
316 if (!$inside_string) {
317 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
318 $inside_string = substr($line, $pos + 2, 1);
321 } elsif ((length($line) >= ($pos + 2)) &&
322 (substr($line, $pos, 1) eq "q")) {
323 $inside_string = substr($line, $pos + 1, 1);
326 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
327 $inside_string = $cur_char;
329 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
330 return ($text, substr($line, $pos + 1));
335 $text .= '\\' unless $cur_char eq "'";
339 } elsif ($cur_char eq '\\') {
342 } elsif ($cur_char eq $inside_string) {
343 undef($inside_string);
356 my $dont_include_subs = shift;
357 my $scanned_files = shift;
362 $scanned_files = {} unless ($scanned_files);
363 return if ($scanned_files->{$file});
364 $scanned_files->{$file} = 1;
366 if (!defined $cached{$file}) {
368 return unless (-f "$file");
370 my $fh = new FileHandle;
371 open $fh, "$file" or die "$! : $file";
373 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
376 last if /^\s*__END__/;
380 # is this another file
381 if (/require\s+\W.*\.pl/) {
383 $newfile =~ s/require\s+\W//;
384 $newfile =~ s|bin/mozilla||;
385 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
386 } elsif (/use\s+SL::([\w:]*)/) {
389 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
392 # Some calls to render() are split over multiple lines. Deal
394 while (/(?:parse_html_template2?|render)\s*\( *$/) {
399 # is this a template call?
400 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
401 my $new_file_base = "$basedir/templates/webpages/$1.";
402 if (/parse_html_template2/) {
403 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
407 foreach my $ext (qw(html js json)) {
408 my $new_file = "${new_file_base}${ext}";
410 $cached{$file}{scanh}{$new_file} = 1;
416 if ($opt_c && !$found_one) {
417 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
426 my ($null, $country) = split(/,/);
427 $country =~ s/^ +[\"\']//;
428 $country =~ s/[\"\'].*//;
434 # is it a submit button before $locale->
435 if (/$submitsearch/) {
437 if ($` !~ /locale->text/) {
439 $sub_line_no = $line_no;
443 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
448 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
451 # if there is no $ in the string record it
452 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
454 # this guarantees one instance of string
455 $cached{$file}{locale}{$string} = 1;
457 # this one is for all the locales
458 $cached{$file}{all}{$string} = 1;
460 # is it a submit button before $locale->
462 $cached{$file}{submit}{$string} = 1;
465 } elsif ($postmatch =~ />/) {
469 # exit loop if there are no more locales on this line
470 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
472 if ( ($postmatch =~ />/)
473 || (!$found && ($sub_line_no != $line_no) && />/)) {
483 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
484 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
485 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
487 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
488 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
489 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
491 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
497 my $fh = new FileHandle;
498 open $fh, "$file" or die "$! : $file";
500 my @a = grep m/^\[/, <$fh>;
504 grep { s/(\[|\])//g } @a;
506 foreach my $item (@a) {
507 my @b = split /--/, $item;
508 foreach my $string (@b) {
510 $locale{$string} = 1;
511 $alllocales{$string} = 1;
517 sub unescape_template_string {
528 return if defined $cached{$file};
530 my %plugins = ( 'loaded' => { }, 'needed' => { } );
532 if (!open(IN, $file)) {
533 print "E: template file '$file' not found\n";
540 while (my $line = <IN>) {
543 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
544 $plugins{loaded}->{$1} = 1;
547 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
549 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
552 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
554 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
555 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
556 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
557 (.*?) # Der zu übersetzende String ::Parameter $3::
558 (?<!\\)\2 # Ende des zu übersetzenden Strings
559 | # Start von Variante 2: [% '...' | $T8 %]
560 \[\% # Template-Start-Tag
561 [\-~#]? # Whitespace-Unterdrückung
562 \s* # Optional beliebig viele Whitespace
563 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
564 (.*?) # Der zu übersetzende String ::Parameter $5::
565 (?<!\\)\4 # Ende des zu übersetzenden Strings
566 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
567 (\$T8) # Filteraufruf ::Parameter $6::
568 .*? # Optionale Argumente für den Filter
570 [\-~#]? # Whitespace-Unterdrückung
571 \%\] # Template-Ende-Tag
574 my $module = $1 || $6;
575 my $string = $3 || $5;
576 print "Found filter >>>$string<<<\n" if $debug;
577 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
579 $string = unescape_template_string($string);
580 $cached{$file}{all}{$string} = 1;
581 $cached{$file}{html}{$string} = 1;
582 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
583 $plugins{needed}->{T8} = 1 if $module eq '$T8';
584 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
587 while ($line =~ m/\[\% # Template-Start-Tag
588 [\-~#]? # Whitespace-Unterdrückung
589 \s* # Optional beliebig viele Whitespace
590 (?: # Die erkannten Template-Direktiven
595 \s+ # Mindestens ein Whitespace
596 [\'\"]? # Anfang des Dateinamens
597 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
598 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
600 my $new_file_name = "$basedir/templates/webpages/$1.html";
601 $cached{$file}{scanh}{$new_file_name} = 1;
602 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
608 foreach my $plugin (keys %{ $plugins{needed} }) {
609 next if ($plugins{loaded}->{$plugin});
610 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
613 # copy back into global arrays
614 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
615 $locale{$_} = 1 for keys %{$cached{$file}{html}};
616 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
618 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
620 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
623 sub scan_javascript_file {
626 open(my $fh, $file) || die('can not open file: '. $file);
628 while( my $line = readline($fh) ) {
639 my $text = unescape_template_string($2);
641 $jslocale{$text} = 1;
642 $alllocales{$text} = 1;
648 sub search_unused_htmlfiles {
649 my @unscanned_dirs = ('../../templates/webpages');
651 while (scalar @unscanned_dirs) {
652 my $dir = shift @unscanned_dirs;
654 foreach my $entry (<$dir/*>) {
656 push @unscanned_dirs, $entry;
658 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
659 print "W: unused HTML template: " . strip_base($entry) . "\n";
667 my $s = "$_[0]"; # Create a copy of the string.
670 $s =~ s|templates/webpages/||;
677 $val =~ s/(\'|\\$)/\\$1/g;
678 return "'" . $val . "'";
683 $val =~ s/(\"|\\$)/\\$1/g;
684 return '"'. $val .'"';
688 my $key = _single_quote(shift);
689 my $text = _single_quote(shift);
691 my $template = $params{template} || qq| %-29s => %s,\n|;
692 my $fh = $params{fh} || croak 'need filehandle in _print_line';
694 print $fh sprintf $template, $key, $text;
700 my $file = $params{file} || croak 'need filename in generate_file';
701 my $header = $params{header};
702 my $lines = $params{data_sub};
703 my $data_name = $params{data_name};
704 my @delim = split //, ($params{delim} || '{}');
706 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
708 $charset =~ s/\r?\n//g;
709 my $emacs_charset = lc $charset;
711 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
712 print $fh $header, "\n" if $header;
713 print $fh "$data_name = $delim[0]\n" if $data_name;
717 print $fh qq|$delim[1];\n\n1;\n|;
723 do { local ( @ARGV, $/ ) = $file; <> }
730 locales.pl - Collect strings for translation in kivitendo
734 locales.pl [options] lang_code
737 -n, --no-custom-files Do not process files whose name contains "_"
738 -c, --check-files Run extended checks on HTML files
739 -v, --verbose Be more verbose
740 -h, --help Show this help
746 =item B<-n>, B<--no-custom-files>
748 Do not process files whose name contains "_", e.g. "custom_io.pl".
750 =item B<-c>, B<--check-files>
752 Run extended checks on the usage of templates. This can be used to
753 discover HTML templates that are never used as well as the usage of
754 non-existing HTML templates.
756 =item B<-v>, B<--verbose>
764 This script collects strings from Perl files, the menu.ini file and
765 HTML templates and puts them into the file "all" for translation.