[htmltmpl] Re: language support
Brought to you by:
samtregar
From: <swp...@sn...> - 2004-10-29 11:04:47
|
Martin Sarfy wrote: > <TMPL_LANG lang=3D"eng">Hello</TMPL_LANG> > <TMPL_LANG lang=3D"spa">Ciao</TMPL_LANG> > <TMPL_LANG lang=3D"cze">Ahoj</TMPL_LANG> The biggest problem with this, I find, is that if I use that solution the template now needs to contain all the messages in all the languages right there in the template. An update nightmare and quite messy. Here is another suggestion: I use gettext, and so I've created this filter: my $gettextFilterSub =3D sub { my ($text_ref) =3D @_; $$text_ref =3D~ s/<gettext\(\"([^\"]*)\"\)>/gettext($1)/ge; # To satisify xgettext: " }; And then: $template =3D HTML::Template->new(filename =3D> $tFile, filter =3D> $gettextFilterSub) Now, I can just type strings that are subject to translation directly in=20 the template like: <h1><gettext("My Project Title")></h1> <gettext("Hello World")> Using exactly the /<gettext\(\"([^\"]*)\"\)>/ format makes it possible to use xgettext to extract the strings that need translation: xgettext --c++ -o template.po template.tmpl Now template.po contains entries for "My Project Title" and "Hello World" that the translator can go right ahead and translate in a separate file (completely standard GNU gettext and perl Locale::gettext stuff ). If the translation is present it is used, otherwise the English (or whatever) text is used. I don't yet know how this works with caching, but I trust there is some solution to that too... Cross that bridge when I get there... :-D (I've just joined the mailing list and so I hope this ends up in the right thread...) Peter P.S.: The magic I needed to get gettext to work was: ... # Or whatever language you like - We're in Denmark here... $ENV{LANG} =3D "da_DK"; # I have a /tmp/da/LC_MESSAGES/proj.mo - created from .po with msgfmt bindtextdomain("proj","/tmp"); # We can set the codeset explicitly or do so in a round-about way # using the LC_CTYPE... # setlocale(LC_CTYPE,""); bind_textdomain_codeset("proj", "ISO-8859-1"); setlocale(LC_MESSAGES, ""); textdomain("proj"); ... --=20 Peter Valdemar M=F8rch http://www.morch.com |