Search code examples
laravellocalizationlaravel-8laravel-localizationlaravel-jetstream

Laravel localization and routes from Jetstream / Fortify


I have this new Laravel project to work on. We would like to make it available in multiple languages.

I started the project with JetStream. Routes for authentication and such are automatically handled by JetStream / Fortify. I then added https://github.com/mcamara/laravel-localization to handle the localization. it works fine for the routes I created myself :

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
    ], function()
{
    Route::get('/', function () {
        return view('welcome');
    });

    Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');
});

But how can I set the group, prefix and middleware on the routes handled by Jetstream and Fortify?

[EDIT]

So after some suggestions from @TEFO, I'm trying to add a middleware to handle setting the locale. Added :

Fortify.php :

    'path' => '{lang}',
    'middleware' => ['web', 'setLang']

new middleware setLang :

class SetLang {
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(\Illuminate\Http\Request $request, Closure $next) {
        // $lang = 'en';
        // $request->attributes->add(['lang' => 'en']);
        $request->route()->setParameter('lang', 'en');
        // $request->request->set('lang', 'en');

        return $next($request);
    }
}

Added the middleware to $routeMiddleware.

I'm receiving this error when trying to reach http://mylaravel/en/login :

ErrorException
Missing required parameters for [Route: login] [URI: {lang}/login]. (View: /var/www/resources/views/auth/login.blade.php)

Solution

  • Finally successfully nailed this. I simply disabled routes from Fortify and Jetstream, copied them over and shoved them inside my grouped prefix routes. Still using https://github.com/mcamara/laravel-localization but it should work anyway you want it - make your own system or whatever, as long as you control the routes you're good to go.

    In JetstreamServiceProvider :

    public function register() {
            Jetstream::ignoreRoutes();
        }
    

    In FortifyServiceProvider :

    public function register() {
            Fortify::ignoreRoutes();
        }
    

    And copy over routes from Fortify vendor/laravel/fortify/routes/routes.php and Jetstream vendor/laravel/jetstream/routes/livewire.php (I guess adapt to Inertia if you're working with this) over to your web.php file, inside a route group with the prefix you need.