CC: | mdom [...] cpan.org |
Subject: | Two extensions to Template::Multilingual |
Date: | Thu, 20 Feb 2014 14:34:16 +0100 |
To: | bug-Template-Multilingual [...] rt.cpan.org, cholet [...] logilune.com |
From: | Edgar Fuß <ef [...] math.uni-bonn.de> |
Some six years ago, Mario Domgörgen, a former colleague of mine, has extended Template::Multilingual (0.09) in two ways that our (www.math.uni-bonn.de) website has been using ever since:
1. Supply the name of a Template variable that, in the Template code, gets assigned a list of languages for which variants are actually available in the multilingual template. This enables you to, in a higher-level template, automatically provide a HTML language selector offering only the languages actually present:
my $template = Template::Multilingual->new(LANGUAGE_LIST_VAR => 'languages');
[% FOREACH lang IN languages %]
<a href="[% cgi.url _ '?language=' _ lang %]">[[% lang %]]</a>
[% END %]
2. In the Template code generated, insert, in parenthesis, this list of languages after the LANGUAGE_VAR variable. This enables that variable to actually refer to a Perl subroutine which, being passed the list of available languages, can select the language chosen based on the combination of languages available in the multilingual template and the HTTP Accept-Language parameter:
use I18N::AcceptLanguage;
my $acceptor = I18N::AcceptLanguage->new();
my $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
my $default_lang = 'de';
my $vars = {
language => sub { return $acceptor->accepts($lang, [@_]) || $default_lang; },
};
my $template = Template::Multilingual->new(LANGUAGE_VAR => 'language', LANGUAGE_LIST_VAR => 'languages');
$template->process('wrapper', $vars);
This is backward compatible due to the way Template::Stash works, i.e. inserting the parenthesis-surrounded list in case LANGUAGE_VAR actually refers to a simple variable (not a function) will do no harm. It will only be done if LANGUAGE_LIST_VAR is set anyway.
However, Mario then moved away from Bonn and so has never tidied up or documented his patch.
I now re-wrote a similar patch for Template::Multilingual 1.00, which you can find attached.
Message body is not shown because sender requested not to inline it.