codezero-be / laravel-localized-routes

⭐️ A convenient way to set up and use localized routes in a Laravel app.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I find the current language?

perffy opened this issue · comments

Hi guys,

how get I find the current language of the page?

I have tried with this first Lang::getLocale() and then app()->getLocale() but both give me the default language instead of the selected. How can I solve this?

Hello,

If you apply the SetLocale middleware to a route, it will update the app locale, so you can retrieve it with app()->getLocale(). That code needs to run after the middleware has run. I think ServiceProviders etc run before the middleware, so there, the locale will not be updated yet.

the problem was that I was calling getLocale() in the __construct() in it seems there is still the default language. Do you know why is that?

Do you mean n the controller construct?
The constructor is called before the middleware I believe, because you can also define middleware there.
Until the SetLocale middleware has run, the default app locale remains unchanged.

yes, that makes sense.

Thank you :)