I've been searching for this issue for a while now and finally found the answers.
Most of the articles describing how to configure a Middleware for setting the app locale, fail to mention an important part. That is, addressing the redirect when login, logout, register, reset password and verify email routes. These all will fallback to the fallback_locale.
Many found their way of working around this issue, by saving the locale in the session and access this variable in different Middlerwares, Controllers, etc... Not too bad of a workaround, however, I believe for an intuitive framework like Laravel there must be a better way, so I kept searching...
Here is the answer:
You might want to add a redirect rule to each of them, so when that route is called, it will apply the locale you have set for the rest of the application. (Multi-Language Routes and Locales with Auth)
The solution would be the add a redirect method to them:
- App\Http\Controllers\Auth\LoginController.php
- App\Http\Controllers\Auth\RegisterController.php
- App\Http\Controllers\Auth\ResetPasswordController.php
- App\Http\Controllers\Auth\VerificationController.php
Add the redirect method to each of those:
public function redirectTo()
{
return app()->getLocale() . '/home';
}
Almost all works except one, the logout. This route is also handled by the LoginController, however, our redirect rule doesn't apply for it. You might need to override the logout() method. (How to set laravel 5.3 logout redirect path?)
App\Http\Controller\Auth\LoginController.php
// use AuthenticatesUsers;
use AuthenticatesUsers {
logout as performLogout;
}
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function redirectTo()
{
return app()->getLocale() . '/home'; // works for login
}
public function logout(Request $request)
{
$this->performLogout($request); // call the original code
return redirect(app()->getLocale() . '/home'); // add the Locale fix for logout
}
I am using Laravel 6, but the solutions were discussed for earlier versions