Method App :: setLocale () in laravel 5.2

0

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
    
asked by anonymous 29.02.2016 / 19:40

2 answers

0

I do not know if this is the problem, but this is wrong:

if (Session::has('app.locale')array_key_exists(Session::get('app.locale'), Config::get('languages'))) {

The broker would be this:

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'));
}

Use a few more local variables to save what you've already done and make it easier to understand what you've done yourself, try this:

$locale = Session::has('app.locale') ? Session::get('app.locale') : false;

if ($locale && array_key_exists($locale, Config::get('languages'))) {
    App::setLocale($locale);
} else {
    App::setLocale(Config::get('app.fallback_locale'));
}
    
29.02.2016 / 20:13
0

Here is the solution to the problem discussed above. After a few days of temptation, I was fortunate to get round the situation as follows: 1. I created a varial in the view that would receive a value returned from the controller. 2. Testo whether the same variable received the value or not. 3. If the variable receives a value, I call in the view the method App :: setLocale ($ variable) and pass the varial as a method parameter, which in this case contains the key of one of the languages and the page is translated into the selected language .

Here is the code, there may be a better way to do it, but the important thing is that it works just the way I want it to.

  class ControllerL extends BaseController
  {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
        public function choose($lang)
        {
           return view('welcome',['Lang'=>$lang]);
        }
   }

routa

  Route::get('/{lang}','ControllerL@choose');

Variable that receives the value in the view

  {{isset($Lang) ? App::setLocale($Lang) : false}}

In short, I think the setLocale () method works in real-time this and more for the client-side. At least that's what you can tell.

    
02.03.2016 / 21:54