playing with path_info before matching routes
Hi! I try to build multi-language site, where i could include laguages code into path, like this: www.exemple.com/en/some_page www.exemple.com/de/some_page www.exemple.com/fr/some_page www.exemple.com/fi/some_page www.exemple.com/ru/some_page etc I want to handle part of the path after language abbreviation in one route for every language and only render page with lang-specific template. I thought that best place to get language part from path is in before hook but : * it is fired just after checking routes * after modifiyng path_info before hook is fired again I wrote simple code like this: use Dancer ':syntax'; our $VERSION = '0.1'; my $lang; hook 'before' => sub { my $path = request->path_info(); if ( $path =~ s| ^/ (?<lang> [[:ascii:]]{2} ) / |/|x ) { $lang = $+{lang}; } request->path_info( $path ); }; get '/some_page' => sub { template ("$lang/some_page"); }; And basically it works fine. Still i'd like to get lang part out before trying match routes. Or is there at least a way that before hook is not fired again? And maybe there is even better way to achieve my goal? -- Wbr, Kõike hääd, Gunnar
One possibility is to use wildcard path matching and splat or named parameters in route paths - the upside, it doesn't call hook again, the downside, it matches anything not just your language codes. You could prefix it though with even a single letter like 'l' to just narrow it down to those pages: www.exemple.com/l/en/some_page www.exemple.com/l/de/some_page get '/l/*/*' => sub { my ($lang, $page) = splat; template ("$lang/$page"); }; It's a bit ugly though... Regards, Matt On 3/12/13 10:26 PM, WK wrote:
Hi!
I try to build multi-language site, where i could include laguages code into path, like this:
www.exemple.com/en/some_page www.exemple.com/de/some_page www.exemple.com/fr/some_page www.exemple.com/fi/some_page www.exemple.com/ru/some_page etc
I want to handle part of the path after language abbreviation in one route for every language and only render page with lang-specific template.
I thought that best place to get language part from path is in before hook but : * it is fired just after checking routes * after modifiyng path_info before hook is fired again
I wrote simple code like this:
use Dancer ':syntax'; our $VERSION = '0.1'; my $lang;
hook 'before' => sub { my $path = request->path_info(); if ( $path =~ s| ^/ (?<lang> [[:ascii:]]{2} ) / |/|x ) { $lang = $+{lang}; } request->path_info( $path ); };
get '/some_page' => sub { template ("$lang/some_page"); };
And basically it works fine.
Still i'd like to get lang part out before trying match routes. Or is there at least a way that before hook is not fired again?
And maybe there is even better way to achieve my goal?
On 13-12-03 07:23 AM, Matt Mallard wrote:
get '/l/*/*' => sub { my ($lang, $page) = splat;
template ("$lang/$page"); };
It's a bit ugly though...
Just for giggles, the megasplat can also used to make the whole thing a little less painful. Funnily enough, the Perl Advent entry of yesterday features an example that is very close to that very use case: http://www.perladvent.org/2013/2013-12-02.html Joy, `/anick
On 12/03/2013 12:26 PM, WK wrote:
Hi!
I try to build multi-language site, where i could include laguages code into path, like this:
www.exemple.com/en/some_page www.exemple.com/de/some_page www.exemple.com/fr/some_page www.exemple.com/fi/some_page www.exemple.com/ru/some_page etc
I want to handle part of the path after language abbreviation in one route for every language and only render page with lang-specific template.
For regular users, you just could pick up the browser reference. If they change their language, save that in the session. So you don't need this links above. If you really want them, you might do this on domain level, e.g. de.exemple.com or exemple.de. Regards Racke -- Perl and Dancer Development Visit our Open Source conference on E-commerce: http://www.ecommerce-innovation.com/
2013/12/3 Stefan Hornburg (Racke) <racke@linuxia.de>:
For regular users, you just could pick up the browser reference. If they change their language, save that in the session.
I thought on this solution too, but i feeled having lang-code in URL a bit cleaner. -- Wbr, Kõike hääd, Gunnar
2013/12/3 Stefan Hornburg (Racke) <racke@linuxia.de>:
For regular users, you just could pick up the browser reference. If they change their language, save that in the session.
Forgot one reason more -- having lang in URL has better for site indexing by search engines. Subdomains may be also a solution. -- Wbr, Kõike hääd, Gunnar
On 12/03/2013 02:21 PM, WK wrote:
2013/12/3 Stefan Hornburg (Racke) <racke@linuxia.de>:
For regular users, you just could pick up the browser reference. If they change their language, save that in the session.
Forgot one reason more -- having lang in URL has better for site indexing by search engines.
Yeah, that's true. Regards Racke -- Perl and Dancer Development Visit our Open Source conference on E-commerce: http://www.ecommerce-innovation.com/
Il giorno Tue, 3 Dec 2013 13:26:50 +0200 WK <wanradt@gmail.com> ha scritto:
Hi!
I try to build multi-language site, where i could include laguages code into path, like this:
www.exemple.com/en/some_page www.exemple.com/de/some_page www.exemple.com/fr/some_page www.exemple.com/fi/some_page www.exemple.com/ru/some_page etc
I want to handle part of the path after language abbreviation in one route for every language and only render page with lang-specific template.
If you're still looking for a solution for your problem, give a try to my Dancer Plugin for multilanguage: https://github.com/cym0n/Dancer2-Plugin-Multilang I'd love to have feedback about it! -- Cymon http://perlishscrewdriver.blogspot.com
2013/12/15 Cymon <cymon.ML@gmail.com>
If you're still looking for a solution for your problem, give a try to my Dancer Plugin for multilanguage: https://github.com/cym0n/Dancer2-Plugin-Multilang I'd love to have feedback about it!
Your plugin seems to do exactly i wanted, except i wanted it for Dancer 1. Good work, i will use it with D2 when needed. Does before hook execute twice in D2 too? -- Wbr, Kõike hääd, Gunnar
participants (5)
-
Cymon -
Matt Mallard -
Stefan Hornburg (Racke) -
WK -
Yanick Champoux