[Catalyst] getting TT to translate automatically

Peter Edwards peterdragon at users.sourceforge.net
Sun Apr 9 21:04:02 CEST 2006


Hi Daniel, I think you'd be better off using a macro like

<body>
<H1>[% INCLUDE label.tt2 "key" => "HELLO" %]</H1>
</body>



and do your internationalisation in label.tt2 rather than using magic.

 

A couple of approaches are 

1) Generate static lookup .tt2 files from your database and at run time load
up the correct one based on the 'lang' value passed in the stash. The static
file can map key => internationalised label.

2) Call back to perl to look up the label using a Template Toolkit plugin.

 

 

We use a Template Toolkit perl plugin and call out to that to handle field
label mappings (in fact, to generate the HTML for the whole field, its label
and surrounds based on a data dictionary, so for example we can show a
DATETIME sql field as either a text input or as a date picker DHTML rich
element). It looks up dictionary settings from DBM format database files
generated overnight by reading from SQL. It's a lot quicker to look up from
DBM than SQL.

 

E.g. you could do something like this

 

myModule.pm:

 

            my $template = Template->new({

                        INCLUDE_PATH => "...",

                        PLUGINS => { D => 'Myapp::Dictionary' },

            )};

            $template->process('sometemplate.tt2', { username => 'Pierre' }
) || confess '.'

 

sometemplate.tt2:

 

    blah

    blah

    [% INCLUDE label.tt2 key = 'hello', someextraflag => 'foo' %] [%
username %]

    blah

    blah

 

label.tt2:

[%

   DEFAULT key = '';

   DEFAULT someextraflag = '';

 

   IF key == ''; THROW "key undefined"; END;

 

   USE D;

   D.label ( 'key' => key, 'somextraflag' => someextraflag );

%]

 

Myapp/Dictionary.pm:

            package Myapp::Dictionary;

            use base qw ( Template::Plugin );

            .

 

            sub new

{

   my $class = shift;

   my $context = shift;

   my $this = {

            _CONTEXT => $context,

                        @_,

            _lang => 'fr',

            };

   bless($this, $class);

   $this->load;

}

 

sub load

{

    Some code to read your dictionary from a filename based on
$this->{_lang} and eval it to a perl structure
}

 

sub setlang

{

   my $this = shift;

   $this->{_lang} = shift;

   Some validation of _lang value

   $this->load;
}

 

sub label
{

   my $this = shift;

   my $p = shift || {};

   .

   Look up and return internationalised label from key in $p->{key}
}

 

 

Regards, Peter

 

  _____  

On 4/9/06, Daniel McBrearty <danielmcbrearty at gmail.com> wrote:

Hi

I am looking at using TT with cat to rewrite my multilanguage website.

The way my current implementation works is that all text for the site is in
a database, each row has a text mnemonic as primary key. You get back text
in a given language by doing something like::

$text = Sitetext::get( $lang, $key);

You can also do substitution of variables like this :

$text = Sitetext::get( $lang, $key, {NAME => "Pierre"});


What I'd like to do is get TT to do this automatically for all items on the
page that it doesn't find in the stash. So, for a template called "hello.tt"
like this :

<body>
<H1>[%HELLO%]</H1>
</body>

and passed a stash like this :

{ substrings = {NAME => "Pierre"},
  lang => "fr",
  template => "hello.tt"}

TT would then do Sitetext::get( 'fr', 'HELLO', {NAME => 'Pierre'} ),
returning "Bonjour Pierre", and substitute it (because the HELLO var was not
explicitly defined on the stash).

Can anyone tell me if this is doable in TT? I have looked but don't see an
easy way. Discussion of other neat methods used to make a multilingual
catalyst app are also welcome ...

regards

Daniel



-- 
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131 


_______________________________________________
Catalyst mailing list
Catalyst at lists.rawmode.org 
http://lists.rawmode.org/mailman/listinfo/catalyst



 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20060409/b12a311a/attachment.htm 


More information about the Catalyst mailing list