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

JerryBels's avatar

Localize app with jetstream

Hello,

I'm trying to start a project with jetstream, on Laravel 8, while handling multiple languages. All views in Jetstream are already set to be translated - so it's fine here. But how would you go about letting the user choose his language?

My first idea was to go like mylaravel.com/en/somepage. I tried to implement it using https://github.com/mcamara/laravel-localization but it won't work with jetstream - or at least I didn't successfully make it work. I didn't find how I could force jetstream (and fortify) to take a prefix and set the language according to it. My experiment about this after some suggestions from a user on SO got like this. I tried 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)

So I saved this experiment on a branch and got back to my previous commit, without https://github.com/mcamara/laravel-localization. I had an idea I wanted to explore, and I found something very close to it here : https://learninglaravel.net/forum/laraveltutorials/how-to-use-multiple-languages-in-your-laravel-5-website

i tried to implement it but now everything is returning a 404... Is it a difference between Laravel 5 and 8?

Another approach I'm yet to try would be to have a specific button dedicated to set the language on the website, without using the url at all, and trying to set the default language if the user never chose one by detecting the browser language. Saving this to session and cookies I guess.

What do you think?

0 likes
3 replies
JerryBels's avatar
JerryBels
OP
Best Answer
Level 1

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.

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.

3 likes
Glenas7's avatar

Thank you so much for sharing, worked great for me! Hope they make this easier in the future...

1 like
marcellferenc's avatar

@jerrybels it seems adding an additional middleware to config/fortify.php does not take effect, even if we change the order. For me the workaround was the following:

  1. Added 'path' => '{locale}/my-secret-path' to config/fortify.php
  2. Added my middleware before \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class to the web $middlewareGroups
  3. Where my middleware set the locale app()->setLocale($locale); and the default {locale} url parameter URL::defaults(['locale' => $locale]); before passing the request deeper into the application.

Considering Jetstream I had to apply the same steps as @jerrybels you did, exept I didn't copy the jetsream/livewire routes but used the following inside the route group:

require base_path('vendor/laravel/jetstream/routes/livewire.php');

Now I can access {locale}/my-secret-path/login where {locale} is a supported locale for my site.

1 like

Please or to participate in this conversation.