Laravel lang slug in url

Asked by At

I've followed this article to add multi language into my app and it's working fine, the only issue i need to solve is that languages slug won't add to the url.

Example

based on the article above i get my page in English like:

www.myapp.co/my-slug

and in another language the same (however translation will placed)

what i want is like:

www.myapp.co/en/my-slug

&

www.myapp.co/id/my-slug

etc.

Any idea?

4 Answers

0
mafortis On Best Solutions

Solved

I used this tutorial and it gets me what I needed

0
sykez On

Perhaps you can try my answer here, where I used mcamara's Laravel Localization package to achieve this. It was painless for me :)

0
Jamsheer Mohammed On

Change your route using prefix

Route::prefix('{locale}')->group(function ($locale) {

    if (array_key_exists($locale, Config::get('languages'))) {
        Session::put('applocale', $locale);
    }
    // else return 404 
    Route::get('your-slug', function () {    
    });
});
0
Prafulla Kumar Sahu On

May be you can use App::getLocale() as route group prefix and use that to choose language .

In routing

Route::middleware(App::getLocale())->group(function () {
    Route::get('/', function () {
        // Uses first & second Middleware
    });

    Route::get('user/profile', function () {
        // Uses first & second Middleware
    });
});

Retrieve the request parameters in middleware and if require In your middleware you can put something like a conditional and reconstruct the url if required and then redirect it to the newly constructed url.

$locale = $request->segment(1);

if (in_array($locale, config('app.locales'))) {
     \App::setLocale($locale);
            return $next($request);
} else {
    //
}