grolds's avatar

Change locale language in run time

Hi guys, I'm trying to change locale in my lumen application but it looks like don't persist what I set. I have my url, example.com and it can change to example.com/es and example.com/en. I have a LanguageController where I set the app('translator') to the language according the url, but when I got out of controller, my app('translator')->getLocale() don't persist what was set. Do you have any idea what it can be? Thanks!

0 likes
4 replies
lostdreamer_nl's avatar

Because you're not persisting it, only changing it for the current request.

You could have the LanguageController set a session variable, and use that within a ServiceProvider to actually set the language.

So in your LanguageController, where you now have something like:

app('translator')->setLocale($locale);

You would replace it with:

\Session::put('locale' , $locale);

and in your app\Providers\AppServiceProvider.php:


    public function register()
    {
        // grab locale from session, or use the default....
        $locale = \Session::get('locale',  \Config::get('app.locale'));
        app('translator')->setLocale( $locale  );
    }
grolds's avatar

Hi @lostdreamer_nl, thanks for answered. So, I alredy tried to save a variable in session but it doesn't persist either. I've alredy enabled session in my config file and then tried to use app('session')->get('locale') and it returns empty.

lostdreamer_nl's avatar

What version of Lumen are you using (I just noticed that Lumen >= 5.2 has no session support anymore)?

If you're using < 5.2

Did you also set it up in your bootstrap.php? https://lumen.laravel.com/docs/5.1/session and with which configuration

If it's >= 5.2 you're using, you should probably install the session provider first.

grolds's avatar

I'm using Lumen 5.5, I've alredy installed illuminate/session package and made the configurations to enable but I still with this problem.

Please or to participate in this conversation.