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.