First I would get the accept_language, either like the first poster mentioned, or simply using:
$ENV{HTTP_ACCEPT_LANGUAGE}
The second step, is I would override your path for your templates. Both HTML::Template and Template::Toolkit allow you to specify an arrayref of paths to look for your templates. I would unshift your language override directory on to the front of your path arrayref (in HTML::Template - INCLUDE_PATH in TT). That way, it will find you language override templates - but will fall back to your default templates if a language override can't be found.
my $base_dir_abs = "/var/www/templates";
my $default_lang = 'en';
my $lang = lc $ENV{'HTTP_ACCEPT_LANGUAGE'}
my @dirs = ("$base_dir_abs/$default_lang");
if ($lang
&& $lang ne $default_lang
&& $lang =~ /^(\w{2})/ # may look like "en-us"
&& -d "$base_dir_abs/$1") {
unshift @dirs, "$base_dir_abs/$1";
}
my $tt = Template->new({
INCLUDE_PATH => \@dirs
});
$tt->process($file);
OR
my $ht = HTML::Template->new(
filename => $file,
path => \@dirs,
);
Update: adding specification for how to store your content.
# Your directory structure may look like
/var/www/templates/en/my_cgi/index.html
/var/www/templates/en/my_cgi/step2.html
/var/www/templates/en/my_cgi/step3.html
/var/www/templates/en/another_cgi/index.html
/var/www/templates/sp/my_cgi/index.html
/var/www/templates/sp/my_cgi/step2.html
/var/www/templates/sp/another_cgi/index.html
If the CGI is passed HTTP_ACCEPT_LANGUAGE of sp
and
$file = 'my_cgi/index.html'; then you'll get
/var/www/templates/sp/my_cgi/index.html. If you call it with a language other than "sp" then you'll always get
/var/www/templates/en/my_cgi/index.html (well - always until you override my_cgi/index.html in that other language).
Likewise for templates such as my_cgi/step3.html that don't exist in sp - you'll always fall back and get en/my_cgi/step3.html.
my @a=qw(random brilliant braindead); print $a[rand(@a)];
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.