Reputation: 8482
My laravel web application runs in two different languages and I'm using a route to set it, like this:
Route::get('/br', function (Illuminate\Http\Request $request) {
return Redirect::to('/')->withCookie(cookie()->forever('locale', "pt-br"));
});
This works well, but does not work for all the pages on the website, just for the route itself (myapp.com/br)
I want to send my users links on their language, using a url string to define it, and then redirecting the users to the page itself, which could be any page, for example:
http://www.myapp.com/pt/pricing
This would set language to portuguese, and then redirect them to pricing or any other page on the website that has the /pt in the URL.
Basically I need a wildcard check if "/pt" is in the URL, preferrably through routes.
I know I could do this with a url string, like this:
http://www.myapp.com/pricing?lang=pt
But it seems a bit off my development pattern.
So, I need something like this:
Route::get('/br/%anything', function (Illuminate\Http\Request $request) {
return Redirect::to('/%anything')->withCookie(cookie()->forever('locale', "pt-br"));
});
Is there a way to do it with a route in laravel?
Upvotes: 1
Views: 440
Reputation: 14747
Try to register your languages first:
Route::bind('lang', function($lang){
$langs = [
'br' => 'pt-br',
'pt' => 'pt-pt',
];
return $langs[$lang];
});
Then add a pattern for your pages's identifier:
Route::pattern('page', '[a-z]+');
Now, make a redirection according to lang/page
url:
Route::get('{lang}/{page}', function($lang, $page){
return redirect()
->route($page)
->withCookie(cookie()->forever('locale', $lang));
});
Results:
pt
at cookie)br
at cookie)Upvotes: 1