Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mir86's avatar
Level 1

a way to change Localization in Laravel 9 without session()->put('locale', $locale); ?

Greetings everyone, I wonder if there is a way in Laravel 9 to figure out how change localization without implementing it in a session ? session()->put('locale', $locale);

0 likes
11 replies
Niush's avatar
// In some middleware..
use Illuminate\Support\Facades\App;

App::setLocale(auth()->user()?->locale ?? 'en');
mir86's avatar
Level 1

@Niush thank you for your time and reply. Still you are using: auth()->user()?->locale...

What i am trying to implement is change the local globally without session() auth() etc.

Thx again

tykus's avatar

@mir86 if you do not use the Session (Cookie, Session, Auth), then how can you allow change to the locale for each user individually?

mir86's avatar
Level 1

thank you for your time and reply. Good question ? SO the localisation by default must use sessions right ? There is no way to implement it without ? I was thinking there is a way to implment the localisation change globally without using sessions

tykus's avatar

@mir86 Usually we would put it into the Session so it follows the User during their visit. Why don't you want to use the Session?

Otherwise, you could set the locale for the current request using a URL segment (/en/products/1) if you prefer; but you need somewhere to store this so you can build the correct URLs for internal links (so every link on your site has the locale included) specific to the current User. You could put to a temporary cache (array driver) to persist the value for the duration of the current request; then repeat for each subsequent Request.

1 like
mir86's avatar
Level 1

thank you for your time and reply. Actually in my project case there is no need to implement sessions and users. It s a card website.

mir86's avatar
Level 1

@tykus set the locale using a URL segment: it would be something like this in ...blades.php:

is that what you mean ?

P.s. @mir86 there still is a Session so no way to implement without sessions right ?

tykus's avatar
tykus
Best Answer
Level 104

@mir86 the locale would be a wildcard parameter, e.g.

Route::prefix('{locale}')->group(function () {
    Route::get('contacts', [ContactController::class, 'index'])->name('contacts.index');
});
route('contacts.index', ['locale' => 'en']);

You can add a route middleware to grab the locale from the URL; and therefore set the locale for the Request

By default, you always have a Session started when you visit a Laravel application; but this does not mean you have authenticated users (Session !== Authentication)

1 like
melissa354's avatar

hello First make your locale folder inside \resources\lang\YOUR_LOCALE_NAME . Then add file messages. php or as per your need. ... Then go to \config\app. php and set the locale as your LOCALE_FOLDER_NAME . Remember to set fallback_locale in app. php which will be used in case YOUR_LOCALE_NAME is not found. I do not know it will helpful or not just trying to help you so please let us know . https://www.benefitscal.biz/

1 like
mir86's avatar
Level 1

@melissa354 thank you for your time and reply. "Set the locale as your LOCALE_FOLDER_NAME " didn t get it. I have this at config/app.php ... 'locale' => 'en','fr','esp', ... 'fallback_locale' => 'en',

Not sure how i point to local folder name :(

Please or to participate in this conversation.