how to make multilingual (localization) in Laravel? [duplicate]

泄露秘密 提交于 2019-12-04 21:01:01

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!