views:

214

answers:

1

I'm currently trying to move a web project from a custom i18n system to gettext, however I'll need to prepare HTML::Template::Compiled templates for i18n too and don't know yet how to do it. My templates are stored in separate files, therefore I can't use Perl's string interpolation and I also would like to use gettext-typical _() syntax in the templates.

Any idea how to implement this properly?

+1  A: 

hi,

you can try this one: http://perlboard.svn.sourceforge.net/viewvc/perlboard/battie/lib/HTML/Template/Compiled/Plugin/Translate.pm?view=markup

I want to make a CPAN module out of it. Hopefully soon =) Here is an example, the comments in the module are out of date:

use HTML::Template::Compiled;
use HTML::Template::Compiled::Plugin::Translate;
my $t = <<"EOM";
<%translate id="search %1:s found %2:d videos" count=".items#" args=".search,.items#" %>
EOM

my $map = {
    "search %1:s found %2:d videos" => [
        q/Suche nach "%1:s" hat %2:020d Video gefunden/,
        q/Suche nach "%1:s" hat %2:d Videos gefunden/,
    ],
};
my $plug = HTML::Template::Compiled::Plugin::Translate->new({
    lang => "de",
    map => $map,
});

my $htc = HTML::Template::Compiled->new(
    scalarref => \$t,
    plugin => [$plug],
);
$htc->param(
    search => "search term",
    items => [qw/ result1 result2 /],
);
print $htc->output;

The template syntax is not as short as you wanted, and I don't use gettext, but maybe you like it or can build your own plugin from this example.

regards, tina