[Catalyst] Returning to referer - which action to take?

Octavian Rasnita orasnita at gmail.com
Sun Sep 26 21:05:51 GMT 2010


From: "Ekki Plicht (DF4OR)" <ep at plicht.de>
> Hi.
> 
> In my app I use I18N, so the visitor should be able to select his or her 
> language at will. The available languages are available on all pages for 
> selection. Once selected the preferred language is stored in a session cookie, 
> otherwise the automatic mechanism of I18N uses the preferred language set in 
> the request header (if any)

Can't you use I18N's mechanism for detecting the current's page language?

For example, you can have urls like:

www.site.com/en/dir1/dir2
www.site.com/ro/dir1/dir2

In this case you may store the chosen language in the cookie only for displaying the first page of that site in that language the next time the visitor accesses your site.

Of course, you don't need to create a "En" or "Ro" controller for doing this.
You can override prepare_path() or you can use the module:

http://search.cpan.org/~mendel/Catalyst-Plugin-I18N-PathPrefix-0.02/lib/Catalyst/Plugin/I18N/PathPrefix.pm

There is another example that uses the chained dispatching mechanism you can find at:

http://www.catalystframework.org/calendar/2006/18


> Imagine that currently page 
> http://localhost:3000/foo/1/bar/2 
> is displayed in english. Now visitor clicks on 
> http://localhost:3000/sellang/de ,
> the internal variable for language is set. This normally involves per-user 
> session management and cookies. Now I want to display the very same page 
> (/foo/1/bar/2) but in german.

It is not a good idea to use the same URL for pages in different languages, because they won't be probably all indexed by the search engines.
You can add a query_string like &lang=en but it is not nice and it requires too many changes if your app is already half-built.

> Serving the pages in different languages works fine with C::P::I18N, but I 
> can't figure out how to return the visitor to the refering page. Simply 
> redirecting to the referer does not work, because a prepared cookie isn't 
> preserved over a redirect (isn't it?). 

A redirect is nothing else than a HTTP header that tells the browser to what URL it should visit immediately.
So the cookie already set by the first request should be sent by the next request too.

Ideally, the link to each language-changer on each page should display the same page in the chosen language, and not the first page of the site or another default page.

So if the visitor is on the page:

www.site.com/ro/dir1/dir2

and clicks on the "English" link, it should see immediately the page:

www.site.com/en/dir1/dir2

The referrer page is the one in Romanian so if the user clicks on the back button, the /ro/dir1/dir2 page will be re-displayed.

Octavian




More information about the Catalyst mailing list