Method App::setLocale() in Laravel 5.2

Asked

Viewed 1,676 times

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:,

2 answers

0

Here is the solution of the problem mentioned above. After a few days of temptation, fortunately I managed to get around the situation as follows: 1. I created a variable in the view that would receive a value returned from the controller. 2. I test whether the same variable has received the value or not. 3. If the variable receives a value, I call in the view the method App::setLocale($variavel) and step the variable as parameter of the method, which in this case contains the key of one of the languages and the page is translated to the selected language.

Here is the code, it may even be that there is a better way to do it, but the important thing is that it works right as I wanted.

  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 might notice.

0

I don’t know if this is the problem, but this if 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 store what you’ve already done and make it easier to understand what you’ve done yourself, try this way:

$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'));
}
  • I made the change as indicated above, but it remains the same. Does not report any error, it does the page load, but does not translate the page. I think I must be missing some setting with setLocale() on my machine or something like that, because getLocale() it automatically picks up and displays the value in my view, only it is not set in real time the selected language. With the syntax and logic I think it’s all right, I just don’t know what is the problem that is preventing setLocale() from settling the value of the locale.

  • @Franciscobizi the folder structure is correct? How are you accessing the urls?

  • This is the structure: config/ app.php folder, app/http/middleware/Language.php folder, config/ Languages.php folder I tested the variable that my controller receives as parameter and present in another view is working correctly. If the user clicks on a language the parameter variable receives the key of the same language, but by including the variable in Session::set('app.locale', $lang) it does not modify anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.