Le 16 juil. 08 à 16:43, Ayhan Ulusoy a écrit :

> I use Template-Toolkit for the web and I am quite happy with it.  
> Now, I will have to come up with a way to render the template in  
> multiple languages.
>
> I've looked at several CPAN modules such as i18n and  
> Locale::Maketext::Lexicon, . Both function with lexicons and both  
> use PO files. i18n is a Perl source filter that wraps around the  
> latter. And they seem to be very good when you deal with strings  
> from within a Perl program.
>
> But what about Template-Toolkit?
>
> Would any of you know of a cool way of doing this just like i18n  
> does for Perl? Is there a Plugin or a filter for Template-Toolkit?
>
> I've seen a plugin Template::Multilingual, but it doesn't seem to  
> fit my needs, because I would NOT like to write text in multiple  
> languages in one Template file. This is not practical for  
> transaltions and maintenance.
>
> I would not like to fetch the strings from the database either,  
> because this obfuscates the strings
>
> In an ideal world, would like someting just like i18n for Template- 
> Toolkit. The original text should reside in the template itslef  
> while the translations would come from a PO file or something  
> similar. There would be very little line noise in the Template file.  
> Therefore a source filter would be good.
>
> Would anyone know of a simple and practical solution for this? A  
> Template-Toolkit filter perhaps?
>
> Your help would be greatly appreciated here.
>
> Cheers,


I use Locale::Maketext::Lexicon and store strings in po files.  
Although I store the original text (e.g. English version) in a po file  
as well, I use the English version of each phrase as the key. So in  
en.po:

msgid "Your name"
msgstr "Your name"

This keeps the template readable, but not special casing the original  
language makes it easy to compare po files, for example is my fr.po  
complete ?

% diff -u <(grep ^msgid en.po) <(grep ^msgid fr.po)

Locale::Maketext::Lexicon will load po files automatically via its  
import() function:

     Locale::Maketext::Lexicon->import({
         '*' => [ Gettext => '/somedir/*.po' ],
         _auto   => 0,
         _decode => 1,
         _style  => 'gettext',
     });

You then get the handle for a particular language via get_handle(),
   $lh = get_handle('en')

and pass that to the template, then from within the template:

   [% lh.maketext('Your name') %]

To make things a little easier to read I've subclassed  
Template::Parser so that I can write the above as

   {{Your name}}

--
Éric Cholet


_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to