After organizing my code, I tested to see if it worked for real-time language change with the App::setLocale()
method does not work, but when I make the change in the file app.php
o locale it works perfectly, this translating my site in the language that I change manually.
Could someone explain to me why I can not 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();
}
}
File 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']);
Updated kernel file 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