[Catalyst] Multi-language and REST

Octavian Rasnita orasnita at gmail.com
Mon Apr 28 21:51:28 BST 2008

From: "Matt Rosin" <telebody at gmail.com>
FWIW after all that such a module should allow keys stored in cookies
> to supplement/overwrite the url. So instead of prepending lang code, I
> could keep the same url but have a javascript language button set the
> language.

I think that the pages with different content (in different languages) 
should have a unique URL (for allowing the search engines to index all the 

I first made an application in a single language then I needed to make it 
multilanguage, and the most easy way I found was to add the language ID in 
the query string, like:  ?lang=FR.

The app uses Catalyst::Plugin::I18N. If the request doesn't contain any lang 
variable and any lang cookie, it gets the languages prefered by the browser 
and displays the page in the most apropriate language.
If the request contains the lang cookie, the app displays the pages using 
that language.

If the request contains the lang variable, it also displays the page using 
that language.

The links which should be used for changing the language send a lang 
variable, and a "cook" variable. When the app receives both of these 
variables, it displays the site in the specified language, and it also sets 
a cookie with that language, and the next time the visitor will visit the 
site, he will see the page displayed in the previously chosen language. This 
won't happen if this wasn't his choice, but only a browser preference.

For doing this I use the code I put below. I put it in the Root.pm, in the 
auto subroutine, so it is executed for all the other controllers and it sets 
the lang var in the stash for using it later in those controllers if needed 
(for sending emails in the chosen language, for example).

Of course, after doing this, I also needed to modify some templates and 
controllers in which I hard-coded URLs because I needed to insert lang=[% 
lang %].



Here is the code I use:

my $lang = $c->req->param('lang');
my $cook = $c->req->param('cook');

if ($lang and $cook) {
#set cookie
$c->res->cookies->{lang} = {value => $lang, domain => 
$c->config->{cookie_domain}, path => '/', expires => '+1y'};

elsif ($lang) {
elsif ($c->req->cookies->{lang}) {
$lang = $c->req->cookies->{lang}->value;
else {
#read the browser prefered languages
my @accept_language = split /,/, $c->req->header('Accept-Language');
my @langs;
foreach(@accept_language) {
push (@langs, $_);

$lang = $c->language; #the chosen language

$c->stash->{lang} = $lang;

More information about the Catalyst mailing list