Setting runtime values ​​for APP_LOCALE defined in .env file in Lumen

I recently started looking into the Lumen microstructure and I need to change at runtime the value for the APP_LOCALE key defined in my .ENV file. My goal is to switch the language at runtime to print different translations of a specific string defined in my languages ​​files.

The Lumen documentation says:

To set config values ​​at runtime, pass an array to config

So, I tried a sample in a test controller:   

use Illuminate\Support\Facades\App;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController{
    public function show_test(){
        echo(trans('testfile.greetings'));
        config(['app.LOCALE' => 'en']);
        echo(trans('testfile.greetings'));
    }
}

      

In my .env file, my "default" APP_LOCALE is set to "fr" and the result of calling this controller prints my string in French twice instead of the expected time in French and then once in English.

How do I change the value at runtime?

+3


source to share


2 answers


Apparently someone else posted the same question a few days after me and got an answer that:



app('translator')->setLocale('en');

      

+3


source


The key is config/app.php

- locale

, not locale

, which is why you want config(['app.locale' => 'en'])

. The name in is .env

not necessarily the name in the folder files config

.



+1


source







All Articles