Add default language to route, when no language defined

routes
multilang
multi-lang

#1

Hi,
I would like to add the default language to the route if no language is specified in the url.
So when you open “siteurl.com” it changes to “siteurl.com/en”.

I was trying to achieve this in the config.php with something like this, but it doesn’t work.

c::set(‘routes’, array(
array(
‘pattern’ => ‘(?:(en|fr)/?)?(:any)’,
‘action’ => function($lang, $uri) {
if(!$lang) {
$lang = ‘en’;
return site()->visit($page, $lang);
}
)
));

Thank you
Ben


#2

What does your language setting look like? This should be automatic if you have set the language to be used in the URL

c::set('languages', [
  [
    'code'    => 'en',
    'name'    => 'English',
    'default' => false,
    'locale'  => 'en_US',
    'url'     => '/en',
  ], [
    'code'    => 'de',
    'default' => true,
    'name'    => 'Deutsch',
    'locale'  => 'de_DE',
    'url'     => '/de',
  ],
]);

#3

Thanks for the quick response. The mistake was in the languages setup indeed, I had set ‘url’ => ‘/’.
Thanks again!