+sub scanhtmlfile {
+ local *IN;
+
+ if (!defined $cached{$_[0]}) {
+ my %plugins = ( 'loaded' => { }, 'needed' => { } );
+
+ open(IN, $_[0]) || die $_[0];
+
+ my $copying = 0;
+ my $issubmit = 0;
+ my $text = "";
+ while (my $line = <IN>) {
+ chomp($line);
+
+ while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
+ $plugins{loaded}->{$1} = 1;
+ }
+
+ while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
+ my $plugin = $1;
+ $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator));
+ }
+
+ while ("" ne $line) {
+ if (!$copying) {
+ if ($line =~ m|<translate>|i) {
+ my $eom = $+[0];
+ if ($` =~ /$submitsearch/) {
+ $issubmit = 1
+ }
+ substr($line, 0, $eom) = "";
+ $copying = 1;
+ } else {
+ $line = "";
+ }
+
+ } else {
+ if ($line =~ m|</translate>|i) {
+ $text .= $`;
+ substr($line, 0, $+[0]) = "";
+ $text =~ s/\s+/ /g;
+
+ $copying = 0;
+ if ($issubmit) {
+ # $submit{$text} = 1;
+ $cached{$_[0]}{submit}{$text} = 1;
+ $issubmit = 0;
+ }
+ # $alllocales{$text} = 1;
+ $cached{$_[0]}{all}{$text} = 1;
+ # $htmllocale{$text} = 1;
+ $cached{$_[0]}{html}{$text} = 1;
+ $text = "";
+
+ } else {
+ $text .= $line;
+ $line = "";
+ }
+ }
+ }
+ }
+
+ close(IN);
+
+ foreach my $plugin (keys %{ $plugins{needed} }) {
+ next if ($plugins{loaded}->{$plugin});
+ print "E: " . strip_base($_[0]) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
+ }
+
+ &converthtmlfile($_[0]);
+ }
+
+ # copy back into global arrays
+ map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
+ map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
+ map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
+}
+
+sub converthtmlfile {
+ local *IN;
+ local *OUT;
+
+ my $file = shift;
+
+ open(IN, $file) || die;
+
+ my $langcode = (split("/", getcwd()))[-1];
+ $file =~ s/_master.html$/_${langcode}.html/;
+
+ open(OUT, ">$file") || die;
+
+ my $copying = 0;
+ my $text = "";
+ while (my $line = <IN>) {
+ chomp($line);
+ if ("" eq $line) {
+ print(OUT "\n");
+ next;
+ }
+
+ while ("" ne $line) {
+ if (!$copying) {
+ if ($line =~ m|<translate>|i) {
+ print(OUT $`);
+ substr($line, 0, $+[0]) = "";
+ $copying = 1;
+ print(OUT "\n") if ("" eq $line);
+
+ } else {
+ print(OUT "${line}\n");
+ $line = "";
+ }
+
+ } else {
+ if ($line =~ m|</translate>|i) {
+ $text .= $`;
+ substr($line, 0, $+[0]) = "";
+ $text =~ s/\s+/ /g;
+ $copying = 0;
+ $alllocales{$text} = 1;
+ $htmllocale{$text} = 1;
+ print(OUT $self->{"texts"}{$text} || $text);
+ print(OUT "\n") if ("" eq $line);
+ $text = "";
+
+ } else {
+ $text .= $line;
+ $line = "";
+ }
+ }
+ }
+ }
+
+ close(IN);
+ close(OUT);
+}
+
+sub search_unused_htmlfiles {
+ my @unscanned_dirs = ('../../templates/webpages');
+
+ while (scalar @unscanned_dirs) {
+ my $dir = shift @unscanned_dirs;
+
+ foreach my $entry (<$dir/*>) {
+ if (-d $entry) {
+ push @unscanned_dirs, $entry;
+
+ } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
+ print "W: unused HTML template: " . strip_base($entry) . "\n";
+
+ }
+ }
+ }
+}
+
+sub search_translated_htmlfiles_wo_master {
+ my @unscanned_dirs = ('../../templates/webpages');
+
+ while (scalar @unscanned_dirs) {
+ my $dir = shift @unscanned_dirs;
+
+ foreach my $entry (<$dir/*>) {
+ if (-d $entry) {
+ push @unscanned_dirs, $entry;
+
+ } elsif (($entry =~ /_[a-z]+\.html$/) && ($entry !~ /_master.html$/) && -f $entry) {
+ my $master = $entry;
+ $master =~ s/[a-z]+\.html$/master.html/;
+ if (! -f $master) {
+ print "W: translated HTML template without master: " . strip_base($entry) . "\n";
+ }
+ }
+ }
+ }
+}
+
+sub strip_base {
+ my $s = "$_[0]"; # Create a copy of the string.
+
+ $s =~ s|^../../||;
+ $s =~ s|templates/webpages/||;
+
+ return $s;
+}
+
+__END__
+
+=head1 NAME
+
+locales.pl - Collect strings for translation in Lx-Office
+
+=head1 SYNOPSIS
+
+locales.pl [options]
+
+ Options:
+ -n, --no-custom-files Do not process files whose name contains "_"
+ -c, --check-files Run extended checks on HTML files
+ -v, --verbose Be more verbose
+ -h, --help Show this help
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<-n>, B<--no-custom-files>
+
+Do not process files whose name contains "_", e.g. "custom_io.pl".
+
+=item B<-c>, B<--check-files>
+
+Run extended checks on the usage of templates. This can be used to
+discover HTML templates that are never used as well as the usage of
+non-existing HTML templates.
+
+=item B<-v>, B<--verbose>
+
+Be more verbose.
+
+=back
+
+=head1 DESCRIPTION
+
+This script collects strings from Perl files, the menu.ini file and
+HTML templates and puts them into the file "all" for translation. It
+also distributes those translations back to the individual files.
+
+=cut