nhayder's avatar
Level 13

Change app default locale dynamically

i'm using mcamara localization package https://github.com/mcamara/laravel-localization in app an with default language set to english as per default laravel installation config/app.php


/*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'en',

i need to give the user ability to change the default language dynamically using a from, ...

when i don this

    $locale = 'es'; // passed from the form

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

        app()->setLocale(Session::get('locale'));

after submitting the form i can see the locale is changing but mcamara is not picking up the changes, It still seed the default locale = 'en'; as shown above,

not sure if there is another way to do this but i think if i can change the locale in the config/app.php file dynamically and (i mean access the file and change the locale value in php) will do the job.

Is that possible ???? is there is any better way to do this??

Any ideas ?

0 likes
9 replies
Snapey's avatar

isn't the config read into memory during Bootstrap?

Do you need to change the config values in memory?

1 like
nhayder's avatar
Level 13

I've been investigating what you wrote and it looks like you are correct.

the App::setLocale($locale); function is changing the locale of the app but not on the config/app.php file (in memory not on local desk).

Unfortunately the package that i'm using need to read the locale value in config/app.php file to render the correct translation on the page.

In this case i think i need to change the locale value on config/app.php dynamically using my a form input then laravel will take care of the reset on first refresh,

BUT not sure how to do that???? how the code will look like ???

NOTE: I'm totally open for better ideas.

Cronix's avatar

basically have a middleware that gets the locale, and sets it in config. It's good for that current request only. Next request comes in, same thing...middleware sets locale in config. You can also dynamically change any config setting this way.

  1. set locale in session
  2. create middleware to set locale, read value from session or use app default
  3. each request goes through middleware, so locale is dynamically set on each request
app()->setLocale(Session::get('locale'));
6 likes
nhayder's avatar
Level 13

@CRONIX - i already have that implemented but its not working for me because the locale is basically changing in memory not on the actual config.php.

The localization package i'm using basically reads the default language locale from config.php file and compare it with request to determine weather to show or not the default language locale.

i was able to solve the problem simply by changing the config.php default locale value manually and the problem was solved. But its manually not dynamic ???

Not sure how to do it dynamically ???

Cronix's avatar
Cronix
Best Answer
Level 67

No, it won't change it permanently in config.php. Nothing will do that unless you write something to manually do alter the file and resave it, which I wouldn't suggest.

I told/showed you how to change it dynamically. It will only work for the current request only, which is why you use it in middleware and setting this value dynamically on every request. Every user could have their own locale if they wanted to, or whatever you're doing. Middleware executes before it hits your app code (controllers, etc)...so the config will be dynamically set on each and every request.

Then, when laravel retrieves the locale from config (which is in-memory), it will retrieve the new value instead of the one hardcoded in your /config/app.php file, because it overwrote that value in memory.

4 likes
DanielJohan's avatar

To set the locale dynamically works fine for me but..

I set the locale according to the URL structure e.g. public/es/dashboard or public/en/dashboard. I extract the segment corresponding to the locale and set it in the app.

But since I am using jetstream, there are pages like the login page, that already have set URL's which do not contain the locale. I also did not want to change these URL's since it caused problem.

Now, each time I visit /public/login, the default locale (defined in /app.php) is automatically set. I don't know what code segment in Laravel, or fortify, or Jetstream does that. I wish it would just stay with the locale defined in dynamically with the last route containing the locale. But it seems that if you don't have a locale in the URL it simply changes back to the default.

Anyone has an idea how to handle this?

Snapey's avatar

You have to remember that every single request is individual and if you need to set the locale you must store it in session and restore it from session with middleware at the start of each request cycle. If you want a fuller answer post your own question

1 like

Please or to participate in this conversation.