Hello! I'm just beginning with Dancer - looks like a great framework to me - and I have a question. I have the following code: ------------------ package stelage::products; use Dancer ':syntax'; get '/:lang/products/' => sub { return vars->{lang} . ' is the language'; }; ------------------ This works and allows me to use URIs such as: myapp/it/products/ myapp/en_US/products/ etc... I'd like, however, some more automation on this by using the "prefix" keywords, but it doesn't seem to work with prefixes. So, this: ------------------ prefix '/:lang/products'; get '/' => sub { return vars->{lang} . ' is the language'; }; ------------------ doesn't match. I also tried with regular expressions in routes, but didn't work either. Is there a way to make this work, without resorting tu URL rewriting? Thanks in advance, Michele. -- Michele Beltrame http://www.italpro.net/ - mb@italpro.net Skype: arthas77 - Twitter: _arthas
On 19/08/2011 10:16, Michele Beltrame wrote:
Hello!
I'm just beginning with Dancer - looks like a great framework to me - and I have a question. I have the following code:
------------------ package stelage::products; use Dancer ':syntax';
get '/:lang/products/' => sub { return vars->{lang} . ' is the language'; }; ------------------
This works and allows me to use URIs such as:
myapp/it/products/ myapp/en_US/products/
etc...
I'd like, however, some more automation on this by using the "prefix" keywords, but it doesn't seem to work with prefixes. So, this:
------------------ prefix '/:lang/products';
get '/' => sub { return vars->{lang} . ' is the language'; }; ------------------
doesn't match. I also tried with regular expressions in routes, but didn't work either.
Is there a way to make this work, without resorting tu URL rewriting?
Hello At the moment that is not possible at all. But might be an interesting feature to add. Need to look into code to check if it is possible without lots of work :).
I think it can be achieved simply by attaching the prefix at a different time. On Aug 19, 2011 12:50 PM, "ambs" <ambs+dancer@perl-hackers.net> wrote: On 19/08/2011 10:16, Michele Beltrame wrote: > > Hello! > > I'm just beginning with Dancer - looks l... Hello At the moment that is not possible at all. But might be an interesting feature to add. Need to look into code to check if it is possible without lots of work :). _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.o...
On Fri, Aug 19, 2011 at 11:16 AM, Michele Beltrame <mb@cattlegrid.info> wrote:
This works and allows me to use URIs such as: myapp/it/products/ myapp/en_US/products/ etc...
Don't ask me how it should be done, but it seems that this is not how you're supposed to do this language selection stuff: http://blog.steveklabnik.com/2011/07/03/nobody-understands-rest-or-http.html Of course your question is broader and other applications might well deserve this kind of solution! Ciao, Flavio.
Hello!
Don't ask me how it should be done, but it seems that this is not how you're supposed to do this language selection stuff:
"Accept-language" is nice, and I'm using it when a user hits the home page. I.e.: http://www.polettix_sei_avanti.it/ But then I really want to have the language in URI so users can save them, copy and paste them, send them to a friend while keeping the information regarding the language. I do understand this thing can also be seen the opposite way: it might be better to send to a friend a language-agnostic URI and let his browser send the language information to the server. Cheers, Michele. -- Michele Beltrame http://www.italpro.net/ - mb@italpro.net Skype: arthas77 - Twitter: _arthas
On Friday 19 August 2011 10:16:51 Michele Beltrame wrote:
Hello!
I'm just beginning with Dancer - looks like a great framework to me - and I have a question. I have the following code:
------------------ package stelage::products; use Dancer ':syntax';
get '/:lang/products/' => sub { return vars->{lang} . ' is the language'; }; ------------------
This works and allows me to use URIs such as:
myapp/it/products/ myapp/en_US/products/
etc...
I'd like, however, some more automation on this by using the "prefix" keywords, but it doesn't seem to work with prefixes. So, this:
------------------ prefix '/:lang/products';
get '/' => sub { return vars->{lang} . ' is the language'; }; ------------------
doesn't match. I also tried with regular expressions in routes, but didn't work either.
You're right, you won't be able to use a prefix that way. One possible way would be to use a before hook to take the language out of the URL, maybe something like the following (untested): hook before => sub { my $path = request->path_info; my ($lang) = $path =~ s{/(.+)/}{/}; vars->{lang} = $lang; request->path_info($path); } Then, your routes can just use e.g. get '/products' => sub {...}, and the language that was removed from the beginning of the URL will be available in vars->{lang} within the route handler. Cheers Dave P -- David Precious ("bigpresh") http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
Hello!
One possible way would be to use a before hook to take the language out of the URL, maybe something like the following (untested):
hook before => sub { my $path = request->path_info; my ($lang) = $path =~ s{/(.+)/}{/}; vars->{lang} = $lang; request->path_info($path); }
It works with some changes. In particular, since in chances path_info, this hook gets called multiple times and does not work as expected. Anyway, even though I succeeded in changing the code to only execute the first time the hook is called, I think I'll stick to repeating myself in every action for now. I don't know Dancer enough to be able to handle possible side effects of which I'm now unaware. ;) Thanks!! Michele. -- Michele Beltrame http://www.italpro.net/ - mb@italpro.net Skype: arthas77 - Twitter: _arthas
participants (5)
-
ambs -
David Precious -
Flavio Poletti -
Michele Beltrame -
sawyer x