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

Sdiri's avatar
Level 1

Translating URLs using array keys of lang ressource

Hello, I am trying to create dynamically localized and named routes in my Laravel application using translation keys as PHP arrays. But they are not working as expected.

Locales :

// In app.php
    'locale' => 'en',
    'available_locales' => ['en','fr','ar','de'],
    'fallback_locale' => 'en',
// In web.php route
	Route::prefix('/{locale}', function ($locale = null) {
    if (isset($locale) && in_array($locale, config('app.available_locales'))) {
        app()->setLocale($locale);
    } else {
        app()->setLocale(config('app.fallback_locale'));
    }
    Route::get('/'.__('url.home'), function () {
        return ('url.home_message');
    });   
});

Route::get(__('url.home'), function () { 
    Route::get(__('url.home'), function () {
        return ('url.home_message');
    });   
});

When i prefix the url with lang code laravel always return page not found, exemple url like site.com/en/homa When i remove the language code site.com/homafrom the parameters laravel return blank page with favicon but only and only if the URI match with the locale in app.php locale en: site.com/home <- blank page site.com/accueil <- 404 site.com/en/home <- 404 site.com/fr/accueil <- 404 lang/fr/url.php:

return [
    "home"=> "accueil",
    "home_message" => "Bienvenue :) ",
];

lang/en/url.php:

return [
    "home"=> "home",
    "home_message" => "Hi :) ",
];

How can I correctly set up route localization so that URLs change based on the user's locale like in CodeIgniter4 ?

0 likes
2 replies
Kortez12's avatar

This is insane, never do that. URL should practically never change they should be static, not dynamic like you trying to do.

Sdiri's avatar
Level 1

@Kortez12 Thank you for your response Actually this is not a choice or an option, this method is for SEO and localized information, Also it’s provide a good user experience. I'm new in laravel and I don't know exactly how it's load the bundles. Any way to load lang system earlier before processing the routes at lest ?And

Please or to participate in this conversation.