package Template::Multilingual::Parser; use strict; use base qw(Template::Parser); our $VERSION = '0.09'; sub new { my ($class, $options) = @_; my $self = $class->SUPER::new($options); $self->{_sections} = []; $self->{_langvar} = $options->{LANGUAGE_VAR} || 'language'; my $style = $self->{ STYLE }->[-1]; @$self{ qw(_start _end) } = @$style{ qw( START_TAG END_TAG ) }; for (qw( _start _end )) { $self->{$_} =~ s/\\([^\\])/$1/g; } return $self; } sub parse { my ($self, $text) = @_; # isolate multilingual sections $self->_tokenize($text); # replace multilingual sections with TT directives $text = ''; for my $section (@{$self->{_sections}}) { if ($section->{nolang}) { $text .= $section->{nolang}; } elsif (my $t = $section->{lang}) { $text .= "$self->{_start} SWITCH $self->{_langvar} $self->{_end}"; for my $lang (keys %$t) { $text .= "$self->{_start} CASE '$lang' $self->{_end}" . $t->{$lang}; } $text .= "$self->{_start} END $self->{_end}"; } } return $self->SUPER::parse ($text); } sub _tokenize { my ($self, $text) = @_; # extract all sections from the text $self->{_sections} = []; my @tokens = split m!(.*?)!s, $text; my $i = 0; for my $t (@tokens) { if ($i) { # ... multilingual section my %section; while ($t =~ m!<(\w+)>(.*?)!gs) { $section{$1} = $2; } push @{$self->{_sections}}, { lang => \%section } if %section; } else { # bare text push @{$self->{_sections}}, { nolang => $t } if $t; } $i = 1 - $i; } } sub sections { $_[0]->{_sections} } =head1 NAME Template::Multilingual::Parser - Multilingual template parser =head1 SYNOPSIS use Template; use Template::Multilingual::Parser; my $parser = Template::Multilingual::Parser->new(); my $template = Template->new(PARSER => $parser); $template->process('example.ttml', { language => 'en'}); =head1 DESCRIPTION This subclass of Template Toolkit's C parses multilingual templates: templates that contain text in several languages. Hello! Bonjour ! Use this module directly if you have subclassed C