Demers94
1 year ago

Localized routes/URLs

Posted 1 year ago by Demers94

Hi everyone,

I'm working on a bilangual app in both English and French. I would like to customize the route URL (that appears in the browser's address bar) so that it's localized based on the user's current language.

For example, the URL to access the contact page would be /contact-us in english and /nous-joindre in french.

I'm not sure what's way to approach this.

I tried defining custom config files and setting up the routes like that:

// EN config file
return [
    'routes' => [
         'contact' => '/contact-us',
    ]
];

// FR config file
return [
    'routes' => [
         'contact' => '/nous-joindre',
    ]
];

I then load the proper config file on a LanguageServiceProvider and put the result in the global config() store like so :

$configFile = require(base_path() . "/config/{$language}/app.php");
config(['configFile' => $configFile]);

This works for views, but whenever I try to access the config from the routes/web.php file I get null, it's not defined.

Is what I'm trying to achieve possible? Is there a better way to handle localization in routes URL?

Thanks for the help!

Please sign in or create an account to participate in this conversation.