4
After organizing my code, I tested to see if it worked for real-time language changes with the method App::setLocale()
doesn’t work, but when I change the file app.php
the locale it works perfectly, this translating my site in the language I change manually.
Could someone explain to me why I can’t change from the code?
Controller
class LangController extends Controller
{
public function switchLang($lang)
{
if(array_key_exists($lang, Config::get('languages')))
{
Session::set('app.locale', $lang);
}
return Redirect::back();
}
}
Filing cabinet Widdleware Language.php
class Language
{
public function handle($request, Closure $next)
{
if (Session::has('app.locale')array_key_exists(Session::get('app.locale'), Config::get('languages'))) {
App::setLocale(Session::get('app.locale'));
}
else
{ // This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
}
return $next($request);
}
}
Route
Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LangController@switchLang']);
Kernel file updated with my Middleware
\Birth\Http\Middleware\Language::class,
Arquivo languages.php que retorna os idiomas
return [
'en' => 'EN',
'fr' => 'FR',
'ru' => 'РУ',
'pt' => 'PT',
'es' => 'ES',
];
View
<a href="#" class="dropdown-toggle active" data-toggle="dropdown">
{{ Config::get('languages')[App::getLocale()] }}
</a>
@foreach (Config::get('languages') as $lang => $language)
@if ($lang != App::getLocale())
| <a class="link-a" href="{{ route('lang.switch', $lang) }}">{{$language}}</a>
@endif
@endforeach
A link to the solution : http://mydnic.be/post/laravel-5-and-his-fcking-non-persistent-app-setlocale Another thing to note is that in.php kernel it should look like this : Illuminate Session Middleware Startsession::class, Http App Middleware Language:class:,
– user42810