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

Kris01's avatar

Get Locale in Laravel problem.

Hi, I am in a situation in which I have a main layout in which then yield multiple views. OK so, this main layout needs some data, and to avoid always getting the same data for the main layout again and again when calling the yielded view, I simply am getting the data for the layout in the ContentServiceProvider.php . There's a problem, when i change the prefix on the rout for the language (fr, en, it ...) and I do app()->getLocale() in the ContentServiceProvider.php it always returns the default one which is set in the config.php even tho everyelse it returns the right locale. What could be the problem?

This is the way in which I change the locale from the front:

{{route(Route::currentRouteName(), 'en')}} {{route(Route::currentRouteName(), 'IT')}} {{route(Route::currentRouteName(), 'de')}} {{route(Route::currentRouteName(), 'fr')}}

0 likes
14 replies
tykus's avatar

The ServiceProvider (register and boot methods) is likely too early in the Request lifecycle; how is the current locale being set?

Kris01's avatar

@tykus I am setting the locale with the routes I wrote above

Kris01's avatar

@tykus no, I use 'app()->getLocale()' and it returns the current language. Actually I don't use "app()->setLocale()" anywhere to set the locale, so apparently route(Route::currentRouteName(), 'de') is enough to change the locale, maybe? Anyway, no, I don't read it from the url, I use 'app()->getLocale()'

Kris01's avatar

@tykus As you said, the problem is that the serviceProvider runs early. I just checked by putting some dd's and that in the serviceprovider is the one that comes first

tykus's avatar

@Kris01

so apparently route(Route::currentRouteName(), 'de') is enough to change the locale, maybe?

I wouldn't think so. Do you have a locale wildcard segment on your route definitions?

Kris01's avatar

@tykus This is how my routes look like :

Route::redirect('/' , '/FR');

Route::group(['prefix' => '{language}', 'middleware' => [adminCheck::class]], function(){
	//routes
})
tykus's avatar

@Kris01 okay, so there is the language prefix on every route - next question is where is this read? Do you have a route Middleware class that reads the language from the URL and sets the app locale?

Kris01's avatar

@tykus Oh ok, so I was wrong, I totally forgot that I have a middleware 'setLanguage'.

In the 'setLanguage' middleware:

public function handle(Request $request, Closure $next)
    {
       
        \App::setLocale($request->language);
      
        return $next($request);
    }

in the kernel:

 protected $middlewareGroups = [
        'web' => [
   			......
         \App\Http\Middleware\SetLanguage::class,
        ],
tykus's avatar

@Kris01 okay that all makes sense now. So, how are you using the locale inside the ServiceProvider?

Also, you probably want to ensure that the language segment is one of the supported languages!

1 like
Kris01's avatar

@tykus This is inside the content service provider 'boot' method, but the app()->getLocale() dosen't return the current locale, instead it returns the default one. I also tried 'app()->currentLocale()' but it is the same.

$contents = CategoryDifferentLang::whereHas('language',function ($q){
            $q->where('slug', app()->getLocale());
        })->with('category_relation')->get();
 view()->composer('front.layout', function($view) use ($contents) {
            $view->with(['contents' => $contents]);
        });
tykus's avatar
tykus
Best Answer
Level 104

@Kris01 how about putting the app()->getLocale() call inside the Closure instead - then it will be evaluated whenever the view is about to render rather than whenever the framework is booting:

view()->composer('front.layout', function($view) {
    $view->with([
        'contents' => CategoryDifferentLang::whereHas('language',function ($q){
            $q->where('slug', app()->getLocale());
        })->with('category_relation')->get()
    ]);
});
Kris01's avatar

@tykus Hey man, thank you so much for your help and patience. Is it anything more valuable than the 'best answer' that I could give you? some rating, points... I don't know I am new here, and if there is something please tell me I would be happy to do so :D

1 like

Please or to participate in this conversation.