1

Good day! I've been working on a Laravel project and I'm getting mad with the Language picker.

I have set on my app.php the locale which is the current language and an array named locales where I have all the languages (Spanish and English for now):

'locale' => 'es',

'locales' => [
    'en' => 'English',
    'es' => 'Castellano',
],

I've created a route that the user can access to change the language with the pattern /lang/es or /lang/en this way:

Route::get('/lang/{lang}', ['as'=>'lang.switch', 'uses'=>'HomeController@storeLang']);

And here the function storeLang:

    public function storeLang($lang){
        $cookie = null;

        if (array_key_exists($lang, Config::get('app.locales'))) {
            $cookie = Cookie::forever('locale', $lang);
            //App::setLocale($lang);
            var_dump(Config::get('app.locale'));
            exit();
        }

        if ($cookie) {
            return Redirect::back()->withCookie($cookie);
        }

        return back(); 
    }
}

My idea was to check if the $lang that we're passing is correct and if so, make a cookie which lasts forever with the language stored, but it's not making the locale to change, I suppose that I have to add something because this var_dump isn't returning me the language changed and I don't know how to make the cookie to change the locale.

On the other hand, as you can see I have the App::setLocale($lang) commented. I've used it and it seemed to work because in this case the var_dump was returning me the right language that I wanted to change to, but at the time to return to the page it was all with the old language again.

I'm getting mad with this, would be fabulous to get some help, thank you!

Juli15
  • 411
  • 7
  • 17

1 Answers1

3

App::setLocale() is not persistent and sets locale only for current request.

What you can do is you can set cookie (as you already done) and check it in a middleware. If there is any cookie then switch the application language.

Prisacari Dmitrii
  • 1,985
  • 1
  • 23
  • 33
  • Thank you for the answer! I'm not really understanding why is the middleware necessary to see if there's any cookie if I'm initializing the cookie on the function. Anyway, I'm seeing the language that I want to change on the var_dump that doesn't means that the cookie exists? Maybe you are referring to another thing?. On the other hand I've found this http://glutendesign.com/posts/detect-and-change-language-on-the-fly-with-laravel and they're making a middleware, do you think that it can be useful? Thanks! – Juli15 Sep 10 '18 at 18:22
  • 1
    In post you just provided everything works fine except when session expires, your app forgets users preferences. The cookies are used to remind the app what language user prefers. – Prisacari Dmitrii Sep 10 '18 at 18:27
  • 1
    In your code you set cookies for user and it's fine. The middleware is necessary to switch the language on next request. – Prisacari Dmitrii Sep 10 '18 at 18:28
  • Ok, thanks for still helping me! The thing is, now the user goes to the URL and the cookie is set with the chosen language. But on the middleware, if I try to get the "locale" from the cookie I'll just receive a long string instead of the language (I suppose is the encryption of the cookie itself) and If I make the middleware get the locale from the user HTTP request, it will never be allowed to change the language which is useful for the language but not for the user... – Juli15 Sep 10 '18 at 18:42
  • It was finally working but at update composer I'm getting an error on the decryption of the cookie value of "locale" I'm not finding nothing to decrypt the value properly... – Juli15 Sep 10 '18 at 20:51
  • Are you trying to get cookies exact the same way as it is written in docs? (`$request->cookie('name');`) – Prisacari Dmitrii Sep 10 '18 at 21:08
  • Yes, I was doing it properly, it was about the way that I was decrypting the cookie, at the end I've had to use `$locale = app('encrypter')->decrypt(Cookie::get('locale'), false);` to get no errors with unserialize(); Thank you for all the help! – Juli15 Sep 10 '18 at 21:16