Discussion:
[Templates] i18n with Template-Toolkit (localization, internationalization, multiple languages, multilingual)
Tosh Cooey
2008-07-17 13:03:05 UTC
Permalink
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?
Some cool ideas from Eric and Lee, although this:

[% lh.maketext('Great googly moogly, why did you kill me [_1]?', you) %]

Kinda rubs me the wrong way because it seems like *another* language ;)

As an exercise I wonder how many lanugages are needed to render the
above... PERL, probably SQL, TT, Maketext, HTML, likely CSS and/or
Javascript, not to mention English! Horror.

Anyway... what would be really cool is if some of the people here using
TT in i18n-type contexts could describe how and what they are doing
because I am for sure going to have to do this in the next months and
I'd like to pick from the genius of others :)

So in addition to the methods from Eric:
http://mail.template-toolkit.org/pipermail/templates/2008-July/010293.html
and Lee:
http://mail.template-toolkit.org/pipermail/templates/2008-July/010292.html

there's also this from PM: http://www.perlmonks.org/?node_id=466269

Also in this PM thread are some great approaches too.

I hope this helps somebody looking for these solutions.

Tosh
--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/
Éric Cholet
2008-07-17 17:08:11 UTC
Permalink
Post by Tosh Cooey
http://mail.template-toolkit.org/pipermail/templates/2008-July/010293.html
http://mail.template-toolkit.org/pipermail/templates/2008-July/010292.html
there's also this from PM: http://www.perlmonks.org/?node_id=466269
That's just syntactic sugar though, not a new method. Once you're
using Locale::MakeText, it's easy to use

[% lh.maketext('some message') %]
[% loc('some message') %]
[% 'some message' | loc %]

depending on your aesthetic preference.

--
?ric Cholet
Ayhan Ulusoy
2008-07-17 20:07:53 UTC
Permalink
Thank you very much for your responses.

A question comes to my mind. How do you harvest (extract) the
messages from the templates into a PO or (POT) file? Do you have to do
that manually?

I've seen a module on CPAN called Locale::Maketext::Extract which
comes with a command line utility called "xgettext.pl". This module
extracts strings from various file formats (Perl, TT, Mason, ...) and
puts them in a PO file. In its documentation, it says that :

Template Toolkit

Strings inside [%|l%]...[%END%] or [%|loc%]...[%END%] are
extracted.


That looks like "loc" would have to be a TT filter for that to work.
What do you think?
Post by Éric Cholet
[% loc('some message') %]
[% 'some message' | loc %]
Is "loc" an exported function in that case? Which module exports it
then? Besides, how does "loc" know which language handle to use? I am
perplexed...


Cheers,
Ayhan
Post by Éric Cholet
Post by Tosh Cooey
http://mail.template-toolkit.org/pipermail/templates/2008-July/010293.html
http://mail.template-toolkit.org/pipermail/templates/2008-July/010292.html
there's also this from PM: http://www.perlmonks.org/?node_id=466269
That's just syntactic sugar though, not a new method. Once you're
using Locale::MakeText, it's easy to use
[% lh.maketext('some message') %]
[% loc('some message') %]
[% 'some message' | loc %]
depending on your aesthetic preference.
--
?ric Cholet
_______________________________________________
templates mailing list
templates at template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080717/b4bdf7b2/attachment.htm
Ask Bjørn Hansen
2008-07-17 20:26:13 UTC
Permalink
Post by Ayhan Ulusoy
A question comes to my mind. How do you harvest (extract) the
messages from the templates into a PO or (POT) file? Do you have to
do that manually?
I've seen a module on CPAN called Locale::Maketext::Extract which
comes with a command line utility called "xgettext.pl". This module
extracts strings from various file formats (Perl, TT, Mason, ...)
Template Toolkit
Strings inside [%|l%]...[%END%] or [%|loc%]...[%END%] are
extracted.
That looks like "loc" would have to be a TT filter for that to work.
What do you think?
We're doing it manually to have better control over what's going on.
IIRC xgettext.pl only supports the syntax you mentioned and not the [%
"foo" | loc %] syntax which sometimes is more convenient. It also
doesn't preserve comments. In the scheme of things maintaining the
files semi-manually isn't as much hassle as it sounds.
Post by Ayhan Ulusoy
Post by Éric Cholet
[% loc('some message') %]
[% 'some message' | loc %]
Is "loc" an exported function in that case? Which module exports it
then? Besides, how does "loc" know which language handle to use? I
am perplexed...
It's setup as a filter.


Another variation is to use the TT include paths to have the system
search multiple paths. We have it setup so when TT is looking for
"foobar/template.html" it'll look for

docs/en/foobar/template.html
docs/foobar/template.html

(actually, we have a bunch more overrides for brands and such - but
you get the idea).

That way we can easily put in language specific templates for pages
that have big blocks of text rather than stuffing it all into .po files.


- ask
--
http://develooper.com/ - http://askask.com/
Éric Cholet
2008-07-17 20:33:29 UTC
Permalink
Post by Ayhan Ulusoy
Post by Éric Cholet
[% loc('some message') %]
[% 'some message' | loc %]
Is "loc" an exported function in that case? Which module exports it
then? Besides, how does "loc" know which language handle to use? I
am perplexed...
loc is common shorthand for $lh->maketext, where $lh is
Locale::MakeText::get_handle($language)
The current $language is determined by your business logic (from user
preference, cookie, or browser Accept-Language header, etc).
I usually subclass Template and override the process() method to
supply the loc function to templates.

--
?ric Cholet
Lee.M
2008-07-19 19:05:48 UTC
Permalink
Post by Ayhan Ulusoy
Thank you very much for your responses.
A question comes to my mind. How do you harvest (extract) the
messages from the templates into a PO or (POT) file? Do you have to
do that manually?
its not gettext, perldoc Locale::Maketext
Ayhan Ulusoy
2008-07-19 21:08:50 UTC
Permalink
I am aware that it's not gettext. You may want to have a look at
Locale::Maketext::Lexicon which allows using PO files with
Locale::Maketext.
I wouldn't want to keep messages in Perl code anyway.

Cheers,
Ayhan
Post by Lee.M
Post by Ayhan Ulusoy
Thank you very much for your responses.
A question comes to my mind. How do you harvest (extract) the
messages from the templates into a PO or (POT) file? Do you have to
do that manually?
its not gettext, perldoc Locale::Maketext
_______________________________________________
templates mailing list
templates at template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates
Lee.M
2008-07-19 19:04:37 UTC
Permalink
Post by Tosh Cooey
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?
[% lh.maketext('Great googly moogly, why did you kill me [_1]?', you) %]
Kinda rubs me the wrong way because it seems like *another*
language ;)
Its just a method call with a sprinf-ish bracket notation that has, in
practice, efficiently and completely allowed us to create things that
support dozens of languages *very* simply.

It's actually a simple elegant solution to localization, see
Local::Maketext or details.

Trust me, you thank me later :)
Post by Tosh Cooey
As an exercise I wonder how many lanugages are needed to render the
above... PERL, probably SQL, TT, Maketext, HTML, likely CSS and/or
Javascript, not to mention English! Horror.
This makes no sense, substitute 'maketext' for 'gettext', or 'some
plugin' etc: it *is* one more thing: localization support.

Depending on where you want it to render this is true of anything,
you'll have localize it somehow...

You might as well include Assembly code, C, Apache, Linux, etc since
those are also involved.

Locale::Maketext is excellent, in fact major software uses it, For
example cPanel will be using it soon:

http://www.cpanel.net/conference/08/files/Localization.pdf

(might want to read over that as it show's examples of why its great,
has TT example and compares it to gettext)
Loading...