Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bihacsy's avatar

Laravel 11 Inertia localization url.

Hello. I need a little help. An inertial app is indicated, in which it would be important to change the language. From an SEO point of view, it would be essential to display the current language in the url as well. But no matter how I try, I can't find a solution.

I tried to solve it with basic tools, but the locale default variable that I created is. But the route is generated sooner and the locale parameter, which you should search for automatically, is not created.

I'm at a loss.

I already tried the laravel-localization package, but I still didn't get anything like url/locale/admin etc. I try to call routes and this is exactly what I experience. this is what my service provider mapWebRoutes looks like. Does anyone have anything to say. (sorry for the grammar, I use a translator)

            Route::middleware(['web','localeSessionRedirect', 'localizationRedirect', 'localeViewPath'])->prefix(LaravelLocalization::setLocale())->group(function (){
                Route::middleware('webAdmin')->prefix('admin')->name('adminWeb.')->group(module_path('Admin', '/routes/webAdmin.php'));
            });

Thank you in advance for your answers.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve URL localization in a Laravel 11 application using Inertia.js, you can follow these steps to ensure that the locale is included in the URL. This approach will help you manage language-specific routes effectively.

Step-by-Step Solution

  1. Install and Configure Laravel Localization Package: Ensure you have the mcamara/laravel-localization package installed. If not, you can install it via Composer:

    composer require mcamara/laravel-localization
    

    Publish the configuration file if you haven't already:

    php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
    
  2. Update the Routes: Modify your web.php routes file to include the locale in the URL. You can use the LaravelLocalization facade to set the locale prefix.

    use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
    
    Route::group(
        [
            'prefix' => LaravelLocalization::setLocale(),
            'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localeViewPath']
        ],
        function () {
            Route::middleware('webAdmin')->prefix('admin')->name('adminWeb.')->group(function () {
                require module_path('Admin', '/routes/webAdmin.php');
            });
        }
    );
    
  3. Middleware Configuration: Ensure that the middleware localeSessionRedirect, localizationRedirect, and localeViewPath are correctly set up in your Kernel.php file. These middleware help manage the session and view paths based on the locale.

  4. Locale Configuration: In your config/laravellocalization.php, ensure you have the supported locales defined. For example:

    'supportedLocales' => [
        'en' => ['name' => 'English', 'script' => 'Latn', 'native' => 'English'],
        'fr' => ['name' => 'French', 'script' => 'Latn', 'native' => 'Français'],
        // Add other locales as needed
    ],
    
  5. SEO Considerations: Ensure that your application generates the correct localized URLs for links and redirects. You can use the LaravelLocalization::localizeURL() method to generate URLs with the locale prefix.

  6. Testing: Test your application by accessing URLs with different locale prefixes, such as /en/admin or /fr/admin, to ensure that the localization is working as expected.

By following these steps, you should be able to include the locale in your URLs, which is beneficial for SEO and user experience. If you encounter any issues, double-check your middleware and route configurations.

Please or to participate in this conversation.