Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Adding Localization Support to Jetstream and Fortify Routes in Laravel

When building a multi-language Laravel application with Jetstream and Fortify, you might notice that the authentication, profile, and team management routes don't automatically inherit your localization configuration. Here's how to fix that and ensure all your routes support both language URL prefixes and localization middleware.

The Challenge

By default, Jetstream and Fortify automatically register their routes during the application bootstrap process. This means these routes are registered before you can wrap them in your localization group. As a result, URLs like /login, /register, /user/profile, and team management pages won't have language prefixes like /en/login or /de/register, nor will they be processed by your localization middleware.

The Solution

The solution involves three simple steps:

  1. Tell Jetstream and Fortify not to register their routes automatically
  2. Import their route files manually
  3. Wrap them in a route group with both your language prefix and localization middleware

Step 1: Disable Automatic Route Registration

First, modify your JetstreamServiceProvider and FortifyServiceProvider to prevent automatic route registration:

// app/Providers/JetstreamServiceProvider.php
public function register()
{
    Jetstream::ignoreRoutes();
}

// app/Providers/FortifyServiceProvider.php
public function register()
{
    Fortify::ignoreRoutes();
}

Step 2: Import Routes Manually

Now, update your routes/web.php file to import the Jetstream and Fortify routes within a route group that includes both the language prefix and localization middleware:

<?php

use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

Route::group([
    'prefix' => LaravelLocalization::setLocale(),    // Adds language code to URL
    'middleware' => ['localize']                     // Handles locale detection and setting
], function () {
    // Import Jetstream routes
    require base_path('vendor/laravel/jetstream/routes/inertia.php');

    // Import Fortify routes
    require base_path('vendor/laravel/fortify/routes/routes.php');

    // Your other routes...
});

The Result

Now all your routes, including authentication, profile management, and team management pages, will have:

  1. Proper language prefixes in the URL (e.g., '/en', '/de')
  2. Full localization middleware support

For example:

  • /en/login
  • /de/register
  • /fr/user/profile
  • /es/teams/create
What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Subscribe to my newsletter

Get new posts when I publish them.

I respect your privacy. Unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *