I have tried to create localization in laravel as well as given in laravel official documentation and it worked like charm!! However it works only for single request. I want it to work for all requests until user changes language. how to create multilingual site in laravel not only for single request as given in laravel documentation?
问题:
回答1:
First of all we need to write few anchor tags (use yours: in your home.blade.php page) to switch language like below...
<a href="{{url('change/locale/en')}}">ENGLISH</a>
<a href="{{url('change/locale/fr')}}">FRENCH</a>
<a href="{{url('change/locale/uz')}}">UZBEK</a>
then we need to create following Route like below to accept requests...
Route::get('change/locale/{lang}', function($lang){
Session::put('locale', $lang);
return redirect()->back();
});
next step is to run this command in your terminal to create middleware named LanguageMiddleware
php artisan make:middleware LanguageMiddleware
after that lets open our LanguageMiddleware
that we created into app/Http/Middleware folder and replace this couple of code with handle method
public function handle($request, Closure $next)
{
if ( Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
now add this line
\App\Http\Middleware\LanguageMiddleware::class
into Kernel.php -> protected $middlewareGroups -> web
and create folders called
en
, uz
, fr
into views/lang
folder.
then create messages.php
file in every folder we created above (en,fr,uz)
now your folders must look just like this.
now you can call your dictionary like this
@lang('messages.greeting')
that's end ! now you can check it by clicking on your anchor tags that we created in first step.