in reply to content negotiation with Apache/cgi

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)];

Replies are listed 'Best First'.
Re^2: content negotiation with Apache/cgi
by punkish (Priest) on Apr 25, 2007 at 02:06 UTC
    Rhandom++ for a most thorough and clear explanation of what to do. Much appreciation.
    --

    when small people start casting long shadows, it is time to go to bed