Switching languages

Vanguard - Advanced PHP Login and User Management Vanguard - Advanced PHP Login and User Management March 25, 2021
Login to reply
Milos Stojanovic Support Agent
2 years ago

Hey Michel,

Of course, it's possible and the principle is the same as in any other Laravel application.

For example, if you keep the selected language in the session, then you can simply create a middleware that will run on every request and update the app locale. Here is an example middleware that you can use as a guideline for implementing this:

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Carbon;
class SetLocale
     * Handle an incoming request.
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
    public function handle($request, Closure $next)
    { // get locale from the session or default to "en"
        $locale = session()->get('locale', 'en');
        return $next($request);

Of course, you will need to add this middleware to the "web" middleware group in the "app/Http/Kernel.php" file so it can run on every request.

Michel M
2 years ago


I know the APP_LOCALE setting in the .env file to set a language. But if I want 2 languages on the same website, is it possible ? Is there another way to set the language on the fly ?