Typist - A template engine and framework like the ones found
package Typist;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
use Typist::L10N;
use vars qw( $typist $VERSION );
$VERSION = 0.02;
my @FIELDS = qw( prefix publish_charset timezone_offset tmpl_path );
Typist->mk_accessors(@FIELDS);
sub new {
my ($class, %param) = @_;
my $self = bless {}, $class;
map { $self->$_($param{$_}) if $param{$_} } @FIELDS;
$self->prefix('MT') unless $self->prefix;
$typist = $self;
$self;
}
sub instance {
return $typist if $typist;
my $class = shift;
$typist = $class->new(@_);
}
my $has_encode = ($] > 5.007003) && (eval { require Encode; 1 });
my %encode_map = (
'shiftjis' => 'shift_jis',
'iso-2022-jp' => 'jis',
'euc-jp' => 'euc',
'utf-8' => 'utf-8',
'utf8' => 'utf-8',
'iso-8859-1' => 'iso-8859-1',
);
my $LH;
sub set_language { $LH = Typist::L10N->get_handle($_[1]) }
sub translate {
my $this = shift;
my $phrase = $LH->maketext(@_);
return $phrase if $LH->ascii_only || !$has_encode;
my $from = lc $LH->encoding;
$from = $encode_map{$from} || $from;
my $to = lc(Typist->instance->publish_charset || $from);
$to = $encode_map{$to} || $to;
Encode::from_to($phrase, $from, $to) if $to ne $from;
$phrase;
}
sub current_language { $LH->language_tag }
sub language_handle { $LH }
1;
__END__