Change language in Laravel 5
The resources/lang
folder contains localization files. The file name corresponds to the view that it will be used. In order to get a value from this file, you can simply use the following code:
Lang::get('localization_file_name.variable_name');
If you want to realize the possibility of language selection, you only need a few simple steps to apply:
In
config/app.php
add this code:'locale' => 'ru', 'locales' => ['ru', 'en'],
The name of the locale can be any.
In
app/Http/Middleware
create a new file namedLocale.php
. The contents of the file should be something like this:<?php namespace App\Http\Middleware; use Closure; use App; use Config; use Session; class Locale { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { //$raw_locale = Session::get('locale'); $raw_locale = $request->session()->get('locale'); if (in_array($raw_locale, Config::get('app.locales'))) { $locale = $raw_locale; } else $locale = Config::get('app.locale'); App::setLocale($locale); return $next($request); } }
In
app/Http/Kernel.php
in$middlewareGroups=[ ... ]
add the following line:\App\Http\Middleware\Locale::class,
In
routes/web.php
add:Route::get('setlocale/{locale}', function ($locale) { if (in_array($locale, \Config::get('app.locales'))) { session(['locale' => $locale]); } return redirect()->back(); });
Try this!
{{ @lang('messages.login') }}
Now Add login key with it's value under language file as below
return['login'=>'Login']; // write inside messages file
and Set your APP Config Local Variable Like 'en','nl','us'
App::setLocale(language name); like 'en','nl','us'