Question

I use Locale::TextDomain in a Dancer environment with Template-Toolkit.

And yes, I know Dancer-Plugin-I18N and tried it, but I don't wan't to use it!

I read Defining_Custom_Virtual_Methods and adjusted it do my needs.

# load Template::Stash to make method tables visible
use Template::Stash;

$Template::Stash::ROOT_OPS->{ 'localize' }    = sub {
    return __(shift); 
};

and in a view

[% localize("Hello")  %] 

That works very well.

But I would like to wrapp all the Locale::TextDomain functions like:

$Template::Stash::ROOT_OPS->{__}    = sub { return __x(shift) };
$Template::Stash::ROOT_OPS->{__x}   = sub { return __x(shift, @_) };
$Template::Stash::ROOT_OPS->{__n}   = sub { return __n(shift, shift, shift) };
$Template::Stash::ROOT_OPS->{__nx}  = sub { return __nx(shift, shift, shift, @_) };
$Template::Stash::ROOT_OPS->{__xn}  = sub { return __xn(shift, shift, shift, @_) };
$Template::Stash::ROOT_OPS->{__p}   = sub { return __p(shift, shift) };
$Template::Stash::ROOT_OPS->{__px}  = sub { return __px(shift, shift, @_) };
$Template::Stash::ROOT_OPS->{__np}  = sub { return __np(shift, shift, shift, shift) };
$Template::Stash::ROOT_OPS->{__npx} = sub { return __npx(shift, shift, shift, shift, @_) };

but unfortunately it is not possible the use '__' or '__x' as token.

So my question: How do I create '__' (and friends) root operations in Template-Toolkit?

Was it helpful?

Solution

Template Toolkit doesnt export functions that start with _ because they are considered private. You can get around this by setting the following before your Stash operations:

$Template::Stash::PRIVATE = 0;

OTHER TIPS

The lexer does not recognize them as identifiers. Changing the module to accept leading _ appears to be far from trivial.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top