[dancer-users] playing with path_info before matching routes

Matt Mallard mqm at Q-Technologies.com.au
Tue Dec 3 12:23:07 GMT 2013


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?
>



More information about the dancer-users mailing list