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

LeoNils's avatar

Translating route

Good afternoon.

I actually work on a multilingual website. I really enjoy how laravel deals with that. It's make things really pleasant. However, I'm facing difficulties to translate my routes. I would like url as

  • mywebsite.com/en/applications in english
  • mywebsite.com/fr/candidatures in french
  • ...

So I write files in lang/fr/routes.php, and so in languages I want to support :

return [
    'applications'  => 'candidatures',
];

Then in my RouteServiceProvider, il add in mapWebRoutes()

Route::middleware('web', 'setlocale')
            ->namespace($this->namespace)
            ->prefix('{locale}')
            ->where(['locale' => '[a-zA-Z]{2}'])
            ->group(base_path('routes/web.php'));

With setlocale a middleware containing:

public function handle($request, Closure $next)
    {
        app()->setLocale($request->segment(1));
        return $next($request);
    }

So now, I would espect use the routes

Route::resource(Lang::get('routes.applications'), 'ApplicationController');

As I already verified than Lang::get('routes.applications') give me "candidature" if I am on french part. But there, I have a 404 error I cant explain. If I add this to my web.php :

Route::get('/', function () {
	    var_dump(Lang::get('routes.applications'));
	});

It give me "candidatures", and Route::resource('candidatures', 'ApplicationController'); lead me at the right place. But Route::resource(Lang::get('routes.applications'), 'ApplicationController'); don't...

Thank you a lot for your help !

0 likes
6 replies
LeoNils's avatar

Great ! Thank you. I will try it now.

1 like
piljac1's avatar

@michaloravec is right. We have done multiple FR/EN websites with this package. Easy to setup at tons of useful features. Also, most importantly, we haven't yet encountered a case where it was restrictive !

Enjoy :)

LeoNils's avatar

Yeh, it seems to do great job. However, I think I will need to write some helper function : LaravelLocalization::getURLFromRouteNameTranslated(LaravelLocalization::getCurrentLocale(), 'routes.applications'); for route('applications') is really too long ! Have you any other idea ?

MichalOravec's avatar

@leonils You don't have to write routes like you mention, so just use classis route like

Route::prefix(LaravelLocalization::setLocale())->middleware('localize')->group(function () {
    Route::get(LaravelLocalization::transRoute('routes.projects'), 'ProjectsController@index')->name('projects.index');
});

In view just

{{ route('projects.index') }}

Middleware localize is from that package and this LaravelLocalization::transRoute('routes.projects') it's same like your routes.applications so in resources/lang folder.

LeoNils's avatar

Much better indeed. Thank you a lot for your help. I think I can close this subject now. Have a nice day.

Please or to participate in this conversation.