[htmltmpl] HTML::Template and Locale::Maketext
Brought to you by:
samtregar
From: Johan K. <ku...@re...> - 2005-05-25 17:41:51
|
Hi, This is a long post, so feel free to stop reading now. :-) Anyone has used HTML::Template and Locale::Maketext together? Or HTML::Template in any I18N project? My doubts are mostly what approach I should use. The application I planning to start is not that big, but I always liked=20 separating things: a. .pl files - For the Perl programmer. b. .html files - for the web designer c. .po and/or .pm files containing just languages hashes - for the language= =20 translator (ok, Perl hashes, but very readable even for a non-programmer) To keep things separated, there is a problem: 1. If, on one hand, I want to use one single HTML::Template for the output = in=20 different languages, the template itself cannot have any "fixed strings" in= =20 one languages, but has to be filled up only with <TMPL_VAR NAME=3Dxxx> tags= for=20 every single message. This makes the job tougher for the web designer (the web page preview will = be=20 almost unreadable). 2. If, on the other hand, one HTML::Template per language, the translator h= as=20 to know some HTML to create a HTML::Template with "fixed strings" in her/hi= s=20 language. And this means that the web designer will end up with one templat= e=20 per language, which also is undesirable. Any suggestions are appreciated. I will try explaining details of the problem by using a couple of examples: Approach 1, using one template for all languages: hello.pl # ---------------------------------------- #!/usr/bin/perl -w use HTML::Template; use MyApp:I18N; my $locale =3D $ARGV || 'en'; # Use English as default my $lh =3D MyApp::I18N->get_handle($locale) || die "Can't get a language=20 handle!"; # open the html template my $template =3D HTML::Template->new(filename =3D> 'hello.world.tmpl.html'); # fill in some parameters $template->param( HELLO_WORLD =3D> $lh->maketext("Hello, world!"), WELCOME_TO_THIS_WEB_SITE =3D> $lh->maketext("Welcome to this web site."), ); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n", $template->output; # ---------------------------------------- hello.world.tmpl.html # ---------------------------------------- <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title><TMPL_VAR NAME=3DHELLO_WORLD></title> </head> <body> <b><TMPL_VAR NAME=3DHELLO_WORLD></b> <p><TMPL_VAR NAME=3DWELCOME_TO_THIS_WEB_SITE></p> </body> </html> # ---------------------------------------- MyApp/I18N/en.pm # ---------------------------------------- package MyApp::I18N::es; # Spanish language messages use base qw(MyApp::I18N); use strict; use vars qw(%Lexicon); %Lexicon =3D ( # Message to translator: Start here! "Hello, world!" =3D> "Hello, world!", "Welcome to this website." =3D> "Welcome to this website.", # Message to translator: Stop here! ); # ---------------------------------------- MyApp/I18N/es.pm # ---------------------------------------- package MyApp::I18N::es; # Spanish language messages use base qw(MyApp::I18N); use strict; use vars qw(%Lexicon); %Lexicon =3D ( # Mensaje a traductor: =A1Empieza aqu=ED! "Hello, world!" =3D> "=A1Hola, mundo!", "Welcome to this website." =3D> "Bienvenido a este sitio web.", # Mensaje a traductor: =A1Termina aqu=ED! ); # ---------------------------------------- MyApp/I18N/se.pm # ---------------------------------------- package MyApp::I18N::es; # Swedish language messages use base qw(MyApp::I18N); use strict; use vars qw(%Lexicon); %Lexicon =3D ( # Meddelande till =F6vers=E4ttaren: B=F6rja h=E4r! "Hello, world!" =3D> "Hej, v=E4rlden!", "Welcome to this website." =3D> "V=E4lkommen till den h=E4r webbsajten.", # Meddelande till =F6vers=E4ttaren: Sluta h=E4r! ); # ---------------------------------------- Approach 2, using one template per language: hello.pl # ---------------------------------------- #!/usr/bin/perl -w use HTML::Template; my $locale =3D $ARGV || 'en'; # Use English as default my $filename =3D 'hello.world.tmpl.html.' . $locale; # open the html template my $template =3D HTML::Template->new(filename =3D> $filename); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n", $template->output; # ---------------------------------------- hello.world.tmpl.html.en # ---------------------------------------- <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>Hello, world!</title> </head> <body> <b>Hello, world!</b> <p>Welcome to this website.</p> </body> </html> # ---------------------------------------- hello.world.tmpl.html.es # ---------------------------------------- <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>=A1Hola, mundo!</title> </head> <body> <b>=A1Hola, mundo!</b> <p>Bienvenido a este sitio web.</p> </body> </html> # ---------------------------------------- hello.world.tmpl.html.se # ---------------------------------------- <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>Hej, v=E4rlden!</title> </head> <body> <b>Hej, v=E4rlden!</b> <p>V=E4lkommen till den h=E4r webbsajten.</p> </body> </html> # ---------------------------------------- Best Regards, Johan Kuuse |