1 package Packages::Template;
8 use Benchmark ':hireswallclock';
11 use Packages::I18N::Locale;
12 use Packages::I18N::Languages;
13 use Packages::I18N::LanguageNames;
15 our @ISA = qw( Exporter );
16 #our @EXPORT = qw( head );
18 use constant COMPILE => 1;
21 my ($classname, $include, $format, $vars, $options) = @_;
25 bless( $self, $classname );
27 my @timestamp = gmtime;
28 $vars->{timestamp} = {
29 year => $timestamp[5]+1900,
30 string => scalar gmtime() .' UTC',
33 $self->{template} = Template->new( {
34 PRE_PROCESS => [ 'config.tmpl' ],
35 INCLUDE_PATH => $include,
37 COMPILE_EXT => '.ttc',
39 } ) or fatal_error( sprintf( _g( "Initialization of Template Engine failed: %s" ), $Template::ERROR ) );
40 $self->{format} = $format;
41 $self->{vars} = $vars;
48 return $self->{template}->process(@_);
52 return $self->{template}->error(@_);
56 my ($self, $action, $page_content) = @_;
59 #die Dumper($self, $action, $page_content);
60 $page_content->{used_langs} ||= [ 'en' ];
61 $page_content->{langs} = languages( $page_content->{lang}
62 || $self->{vars}{lang} || 'en',
63 @{$page_content->{used_langs}} );
66 $self->process("$self->{format}/$action.tmpl", $page_content, \$txt)
67 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
73 my ($self, $page_content) = @_;
76 # warn Dumper($page_content);
79 $self->process("html/error.tmpl", $page_content, \$txt)
80 or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
86 my ( $lang, @used_langs ) = @_;
92 my @printed_langs = ();
93 foreach (@used_langs) {
94 next if $_ eq $lang; # Never print the current language
95 unless (get_selfname($_)) { warn "missing language $_"; next } #DEBUG
96 push @printed_langs, $_;
98 return [] unless scalar @printed_langs;
99 # Sort on uppercase to work with languages which use lowercase initial
101 foreach my $cur_lang (sort langcmp @printed_langs) {
103 $lang{lang} = $cur_lang;
104 $lang{tooltip} = dgettext( "langs", get_language_name($cur_lang) );
105 $lang{selfname} = get_selfname($cur_lang);
106 $lang{transliteration} = get_transliteration($cur_lang) if defined get_transliteration($cur_lang);