1

There is a similar question, where one asked how to change the unauthenticated message (How to change laravel sanctum return "message": "Unauthenticated.").

However, I would like to localize it using my lang/en/auth.php, lang/de/auth.php files. Both contains the following records:

lang/en/auth.php:

'unauthenticated' => 'english unauthenticated.'

lang/de/auth.php:

'unauthenticated' => 'deutsch unauthenticated.'

The current language for the logged in user is stored in the session, like this: "locale";s:2:"de"; (so the current language should be german)

I have tried to modify the register method in the app/Exceptions/Handler.php:


public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });

        $this->renderable(function (AuthenticationException $e, $request) {
            return response()->json([
                'status_code' => 401,
                'success' => false,
                'message' => __('auth.unauthenticated')
            ], 401);
        });
    }

However, it seems that session is not known/ locale is not set here, since the returned message is always the default (en) one (english unauthenticated.) even though I am logged in with the german (de) language.

Tamás H
  • 11
  • 3
  • 1
    Did you try to pass the locale as an argument in `__() ` function ? – xenooooo Feb 24 '23 at 14:57
  • I did, but app->getLocale is always equal to 'en' here even though a middleware already sets it to the corresponding language. session()->get('locale') equals to null here, so 'de' language can't be set. – Tamás H Feb 24 '23 at 15:07
  • did you try changing the middleware priority? it will set app's locale before going through auth middleware – Garry Feb 24 '23 at 16:49
  • 1
    So the problem is that if the user is getting an `AuthenticationException` they aren't logged in so you can't get locale from the session? Modify your middleware to check for the browser language if `session()->get('locale')` returns null. It can be [fairly complex](https://stackoverflow.com/a/3771447/1255289), but can also be [quite easy](https://stackoverflow.com/a/3770616/1255289) if you just want to check the first language specified. – miken32 Feb 24 '23 at 20:46
  • My test scenario: cookie expires while the user is still logged in, and then the logged in user makes a request. He gets back an unauthenticated, which can't be localized in my scenario. Changing the priority order didn't help in my case, putting the Localization middleware first/last didn't solve it. I'll keep experimenting, thank you guys – Tamás H Feb 27 '23 at 07:02

1 Answers1

0

Keeping track of current language in the current session is not easy in all cases, so I chose a different apporach to solve my problem:

I don't set the language in the current session for a logged in user.

I only have english translations / error messages available in the lang/en folder and I return those to the frontend. There I can do the localization based on the default english message that I return, so "Unathenticated" can be translated using your own localiztion method (it is i18n in my case)

Tamás H
  • 11
  • 3