0

I am developing a multi-language web app using Laravel framework. So in this app, I have a special condition to do multi-language feature as below.

  • a user can select from some flags and change the language manually. It changes his URL to /{lang} .. so, for example, webapp.com/cs - so he will see everything that in Czech language. webapp.com/en - see everything in English.
  • Chosen localization should be persistent so would not disappear when user change page or something - it should always be in the URL.

I created Route to set locale in session as follows.

    Route::get('/{locale}', function ($locale) {
    session()->put('locale', $locale);
    return back();
  });

And created middleware and added it to $middlewareGroups in the http\Kernel as well.

Below is my middleware.

public function handle($request, Closure $next)
    {
        if (session()->has('locale')) {
            app()->setLocale(session('locale'));
        }

        return $next($request);
    }

Localization is going well and it gives correct translations and everything. But what I need is to show in the URL what is the language is. as example webapp.com/cs,webapp.com/en. It would be great if anyone can help me with this problem.

Thanks.

Deemantha
  • 433
  • 9
  • 30

1 Answers1

0

You can try this,

In your every route you can append current local language like this

Route::get('/home/'.app()->getLocale(),'HomeController@index');

and one more thing you should see this

Laravel app()->getLocale() inside routes always print the default "en"

Hope this helps :)

Maulik Shah
  • 1,040
  • 1
  • 7
  • 17
  • The link you provided, guided me to the correct answer. which is about mcamara/laravel-localization plugin. Thanks – Deemantha Apr 08 '19 at 13:48