Contao changelanguage on root level

Contao changelanguage on root level

When using multiple languages with Contao on one domain you always define one fallback language which is used when you visit the website through the root url. If you've implemented a language switch with the changelanguage extension it won't work on the root page. That's because the router part in Contao will move you too the root page for the root link which in turn will move you to the fallback language.

You can adapt the extension through overwriting the following file:

system/models/changelanguage/ModuleChangeLanguage.php

$objTemplate = new FrontendTemplate($this->navigationTpl);
$objTemplate->level = 'level_1';

/**
 * Changes to also change language on root page
 */
foreach ($arrItems as $key => $item) {
    if ($item['href'] == '') {
        $item['href'] = sprintf('/%s/', $item['language']);
        $arrItems[$key] = $item;
    }
}

$objTemplate->items = $arrItems;

This way you will always be redirected to the /{language}/ root instead of the domain root and the redirect will also work on the root level.