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 unshift(@INC, $FindBin::Bin . '/../modules/override'); # Use our own versions of various modules (e.g. YAML).
16 push (@INC, $FindBin::Bin . '/..');
23 use File::Slurp qw(slurp);
27 use List::MoreUtils qw(apply);
28 use List::Util qw(first);
31 use SL::System::Process;
34 $OUTPUT_AUTOFLUSH = 1;
45 my $basedir = "../..";
46 my $locales_dir = ".";
47 my $bindir = "$basedir/bin/mozilla";
48 my @progdirs = ( "$basedir/SL" );
49 my @menufiles = glob("${basedir}/menus/*/*");
50 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
51 my $javascript_output_dir = $basedir .'/js';
52 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
57 my %ignore_unused_templates = (
58 map { $_ => 1 } qw(ct/testpage.html oe/periodic_invoices_email.txt part/testpage.html t/render.html t/render.js task_server/failure_notification_email.txt
59 failed_background_jobs_report/email.txt)
62 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
63 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
68 my ($top_dir_name) = @_;
75 tie my %dir_h, 'IO::Dir', $dir_name;
77 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
78 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
80 $finder->($_) for @sub_dirs;
83 $finder->($top_dir_name);
89 # overwrite existing entries with the ones from 'missing'
90 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
92 # try to set missing entries from lost ones
93 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
94 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
97 my @bindir_files = find_files($bindir);
98 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
99 my @customfiles = grep /_custom/, @bindir_files;
101 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
103 # put customized files into @customfiles
107 foreach my $sub_dir ("Pg-upgrade2", "Pg-upgrade2-auth") {
108 my $dir = "$basedir/sql/$sub_dir";
109 tie %dir_h, 'IO::Dir', $dir;
110 push @dbplfiles, map { [ $_, $dir ] } grep { /\.pl$/ } keys %dir_h;
113 # slurp the translations in
114 if (-f "$locales_dir/all") {
115 require "$locales_dir/all";
117 # load custom translation (more_texts)
118 for my $file (glob("${locales_dir}/more/*")) {
119 if (open my $in, "<", "$file") {
124 $self->{more_texts_temp}{$_} = $self->{more_texts}{$_} for keys %{ $self->{more_texts} };
127 $self->{more_texts} = delete $self->{more_texts_temp};
129 if (-f "$locales_dir/missing") {
130 require "$locales_dir/missing" ;
131 unlink "$locales_dir/missing";
133 if (-f "$locales_dir/lost") {
134 require "$locales_dir/lost";
135 unlink "$locales_dir/lost";
138 my %old_texts = %{ $self->{texts} || {} };
140 handle_file(@{ $_ }) for @progfiles;
141 handle_file(@{ $_ }) for @dbplfiles;
142 scanmenu($_) for @menufiles;
145 for my $file_name (grep { /\.(?:js|html)$/i } map({find_files($_)} @javascript_dirs)) {
146 scan_javascript_file($file_name);
149 # merge entries to translate with entries from files 'missing' and 'lost'
154 file => "$locales_dir/all",
155 header => $ALL_HEADER,
156 data_name => '$self->{texts}',
157 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
160 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'.js') || die;
161 print $js_file 'namespace("kivi").setupLocale({';
163 for my $key (sort(keys(%jslocale))) {
164 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
167 print $js_file ("\n");
168 print $js_file ('});'."\n");
171 foreach my $text (keys %$missing) {
172 if ($locale{$text} || $htmllocale{$text}) {
173 unless ($self->{texts}{$text}) {
174 $self->{texts}{$text} = $missing->{$text};
180 # calc and generate missing
181 # don't add missing ones if we have a translation in more_texts
182 my @new_missing = grep { !$self->{more_texts}{$_} && !$self->{texts}{$_} } sort keys %alllocales;
186 my %existing_lc = map { (lc $_ => $_) } grep { $self->{texts}->{$_} } keys %{ $self->{texts} };
187 foreach my $entry (@new_missing) {
188 my $other = $existing_lc{lc $entry};
189 print "W: No entry for '${entry}' exists, but there is one with different case: '${other}'\n" if $other;
194 for my $string (@new_missing) {
195 print "new string '$string' in files:\n";
198 apply { s{^(?:\.\./)+}{} }
199 grep { $cached{$_}{all}{$string} }
205 file => "$locales_dir/missing",
206 header => $MISSING_HEADER,
207 data_name => '$missing',
208 data_sub => sub { _print_line($_, '', @_) for @new_missing },
212 # calc and generate lost
213 while (my ($text, $translation) = each %old_texts) {
214 next if ($alllocales{$text});
215 push @lost, { 'text' => $text, 'translation' => $translation };
219 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
221 file => "$locales_dir/lost",
222 header => $LOST_HEADER,
224 data_name => '@lost',
226 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
231 my $trlanguage = slurp("$locales_dir/LANGUAGE");
234 search_unused_htmlfiles() if $opt_c;
236 my $count = scalar keys %alllocales;
237 my $notext = scalar @new_missing;
238 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
239 print "\n$trlanguage - ${per}%";
240 print " - $notext/$count missing" if $notext;
249 # These are all the texts to build the translations files.
250 # The file has the form of 'english text' => 'foreign text',
251 # you can add the translation in this file or in the 'missing' file
252 # run locales.pl from this directory to rebuild the translation files
254 $MISSING_HEADER = <<EOL;
255 # add the missing texts and run locales.pl to rebuild
257 $LOST_HEADER = <<EOL;
258 # The last 50 text strings, that have been removed.
259 # This file has been auto-generated by locales.pl. Please don't edit!
266 my ($opt_no_c, $ignore_for_compatiblity);
269 'no-custom-files' => \$opt_n,
270 'check-files' => \$ignore_for_compatiblity,
271 'no-check-files' => \$opt_no_c,
272 'verbose' => \$opt_v,
273 'filenames' => \$opt_f,
287 pod2usage(-exitstatus => 0, -verbose => 2);
292 my $arg = shift @ARGV;
294 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
295 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
304 print "The locale directory '$arg' could not be found.\n";
308 } elsif (!-f 'all' || !-f 'LANGUAGE') {
309 print "locales.pl was not called from a locale/* subdirectory,\n"
310 . "and no locale directory name was given.\n";
314 $locale ||= (grep { $_ } split m:/:, getcwd())[-1];
319 my ($file, $dir) = @_;
320 print "\n$file" if $opt_v;
324 &scanfile("$dir/$file");
326 # scan custom_{module}.pl or {login}_{module}.pl files
327 foreach my $customfile (@customfiles) {
328 if ($customfile =~ /_$file/) {
329 if (-f "$dir/$customfile") {
330 &scanfile("$dir/$customfile");
338 sub extract_text_between_parenthesis {
339 my ($fh, $line) = @_;
340 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
343 if (length($line) <= $pos) {
345 return ($text, "") unless ($line);
349 my $cur_char = substr($line, $pos, 1);
351 if (!$inside_string) {
352 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
353 $inside_string = substr($line, $pos + 2, 1);
356 } elsif ((length($line) >= ($pos + 2)) &&
357 (substr($line, $pos, 1) eq "q")) {
358 $inside_string = substr($line, $pos + 1, 1);
361 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
362 $inside_string = $cur_char;
364 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
365 return ($text, substr($line, $pos + 1));
370 $text .= '\\' unless $cur_char eq "'";
374 } elsif ($cur_char eq '\\') {
377 } elsif ($cur_char eq $inside_string) {
378 undef($inside_string);
391 my $dont_include_subs = shift;
392 my $scanned_files = shift;
397 $scanned_files = {} unless ($scanned_files);
398 return if ($scanned_files->{$file});
399 $scanned_files->{$file} = 1;
401 if (!defined $cached{$file}) {
403 return unless (-f "$file");
405 my $fh = new FileHandle;
406 open $fh, '<:encoding(utf8)', $file or die "$! : $file";
408 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
411 last if /^\s*__END__/;
415 # is this another file
416 if (/require\s+\W.*\.pl/) {
418 $newfile =~ s/require\s+\W//;
419 $newfile =~ s|bin/mozilla||;
420 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
421 } elsif (/use\s+SL::([\w:]*)/) {
424 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
427 # Some calls to render() are split over multiple lines. Deal
429 while (/(?:parse_html_template2?|render)\s*\( *$/) {
434 # is this a template call?
435 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
436 my $new_file_base = "$basedir/templates/webpages/$1.";
437 if (/parse_html_template2/) {
438 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
442 foreach my $ext (qw(html js json)) {
443 my $new_file = "${new_file_base}${ext}";
445 $cached{$file}{scanh}{$new_file} = 1;
451 if ($opt_c && !$found_one) {
452 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
461 my ($null, $country) = split(/,/);
462 $country =~ s/^ +[\"\']//;
463 $country =~ s/[\"\'].*//;
469 # is it a submit button before $locale->
470 if (/$submitsearch/) {
472 if ($` !~ /locale->text/) {
474 $sub_line_no = $line_no;
479 if (/ (?: locale->text | \b t8 ) \b .*? \(/x) {
486 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
489 # if there is no $ in the string record it
490 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
492 # this guarantees one instance of string
493 $cached{$file}{locale}{$string} = 1;
495 # this one is for all the locales
496 $cached{$file}{all}{$string} = 1;
498 # is it a submit button before $locale->
500 $cached{$file}{submit}{$string} = 1;
503 } elsif ($postmatch =~ />/) {
507 # exit loop if there are no more locales on this line
508 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
510 if ( ($postmatch =~ />/)
511 || (!$found && ($sub_line_no != $line_no) && />/)) {
521 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
522 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
523 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
525 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
526 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
527 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
529 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
535 my $menu = SL::YAML::LoadFile($file);
537 for my $node (@$menu) {
538 # possible for override files
539 next unless exists $node->{name};
541 $locale{$node->{name}} = 1;
542 $alllocales{$node->{name}} = 1;
543 $cached{$file}{all}{$node->{name}} = 1;
548 # we only need to do this for auth atm, because only auth scripts can include new rights, which are translateable
551 my $dbu = SL::DBUpgrade2->new(auth => $auth, path => SL::System::Process->exe_dir . '/sql/Pg-upgrade2-auth');
553 for my $upgrade ($dbu->sort_dbupdate_controls) {
554 for my $string (@{ $upgrade->{locales} || [] }) {
555 $locale{$string} = 1;
556 $alllocales{$string} = 1;
557 $cached{$upgrade->{tag}}{all}{$string} = 1;
562 sub unescape_template_string {
573 return if defined $cached{$file};
575 my %plugins = ( 'loaded' => { }, 'needed' => { } );
577 if (!open(IN, '<:encoding(utf8)', $file)) {
578 print "E: template file '$file' not found\n";
585 while (my $line = <IN>) {
588 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
589 $plugins{loaded}->{$1} = 1;
592 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
594 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
597 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
599 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
600 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
601 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
602 (.*?) # Der zu übersetzende String ::Parameter $3::
603 (?<!\\)\2 # Ende des zu übersetzenden Strings
604 | # Start von Variante 2: [% '...' | $T8 %]
605 \[\% # Template-Start-Tag
606 [\-~#]? # Whitespace-Unterdrückung
607 \s* # Optional beliebig viele Whitespace
608 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
609 (.*?) # Der zu übersetzende String ::Parameter $5::
610 (?<!\\)\4 # Ende des zu übersetzenden Strings
611 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
612 (\$T8) # Filteraufruf ::Parameter $6::
613 .*? # Optionale Argumente für den Filter
615 [\-~#]? # Whitespace-Unterdrückung
616 \%\] # Template-Ende-Tag
619 my $module = $1 || $6;
620 my $string = $3 || $5;
621 print "Found filter >>>$string<<<\n" if $debug;
622 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
624 $string = unescape_template_string($string);
625 $cached{$file}{all}{$string} = 1;
626 $cached{$file}{html}{$string} = 1;
627 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
628 $plugins{needed}->{T8} = 1 if $module eq '$T8';
629 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
632 while ($line =~ m/\[\% # Template-Start-Tag
633 [\-~#]* # Whitespace-Unterdrückung
634 \s* # Optional beliebig viele Whitespace
635 (?: # Die erkannten Template-Direktiven
640 \s+ # Mindestens ein Whitespace
641 [\'\"]? # Anfang des Dateinamens
642 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
643 \.(html|js) # Endung ".html" oder ".js", ansonsten kann es der Name eines Blocks sein
645 my $new_file_name = "$basedir/templates/webpages/$1.$2";
646 $cached{$file}{scanh}{$new_file_name} = 1;
647 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
653 foreach my $plugin (keys %{ $plugins{needed} }) {
654 next if ($plugins{loaded}->{$plugin});
655 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
658 # copy back into global arrays
659 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
660 $locale{$_} = 1 for keys %{$cached{$file}{html}};
661 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
663 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
665 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
668 sub scan_javascript_file {
671 open(my $fh, '<:encoding(utf8)', $file) || die('can not open file: '. $file);
673 while( my $line = readline($fh) ) {
684 my $text = unescape_template_string($2);
686 $jslocale{$text} = 1;
687 $alllocales{$text} = 1;
693 sub search_unused_htmlfiles {
694 my @unscanned_dirs = ('../../templates/webpages');
696 while (scalar @unscanned_dirs) {
697 my $dir = shift @unscanned_dirs;
699 foreach my $entry (<$dir/*>) {
701 push @unscanned_dirs, $entry;
703 } elsif (!$ignore_unused_templates{strip_base($entry)} && -f $entry && !$referenced_html_files{$entry}) {
704 print "W: unused HTML template: " . strip_base($entry) . "\n";
712 my $s = "$_[0]"; # Create a copy of the string.
715 $s =~ s|templates/webpages/||;
722 $val =~ s/(\'|\\$)/\\$1/g;
723 return "'" . $val . "'";
728 $val =~ s/(\"|\\$)/\\$1/g;
729 return '"'. $val .'"';
733 my $key = _single_quote(shift);
734 my $text = _single_quote(shift);
736 my $template = $params{template} || qq| %-29s => %s,\n|;
737 my $fh = $params{fh} || croak 'need filehandle in _print_line';
739 print $fh sprintf $template, $key, $text;
745 my $file = $params{file} || croak 'need filename in generate_file';
746 my $header = $params{header};
747 my $lines = $params{data_sub};
748 my $data_name = $params{data_name};
749 my @delim = split //, ($params{delim} || '{}');
751 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
753 print $fh "#!/usr/bin/perl\n# -*- coding: utf-8; -*-\n# vim: fenc=utf-8\n\nuse utf8;\n\n";
754 print $fh $header, "\n" if $header;
755 print $fh "$data_name = $delim[0]\n" if $data_name;
759 print $fh qq|$delim[1];\n\n1;\n|;
767 locales.pl - Collect strings for translation in kivitendo
771 locales.pl [options] lang_code
774 -n, --no-custom-files Do not process files whose name contains "_"
775 -c, --check-files Run extended checks on HTML files
776 -f, --filenames Show the filenames where new strings where found
777 -v, --verbose Be more verbose
778 -h, --help Show this help
784 =item B<-n>, B<--no-custom-files>
786 Do not process files whose name contains "_", e.g. "custom_io.pl".
788 =item B<-c>, B<--check-files>
790 Run extended checks on the usage of templates. This can be used to
791 discover HTML templates that are never used as well as the usage of
792 non-existing HTML templates.
794 =item B<-v>, B<--verbose>
802 This script collects strings from Perl files, the menu files and
803 HTML templates and puts them into the file "all" for translation.