Multilanguage - auto detect disables language switching on home

Hey there.
I’m currently setting up multilanguage support for a site and I’am having a little problem on the homepage: when I’ve enabled the auto language detection I’am not able to switch the language anymore.
I’ve set up everything just the normal way as suggested in the kirby docs here: https://getkirby.com/docs/languages/setup

On all subpages everything works fine and I’m able to switch the language with:

<nav class="languages" role="navigation">
  <ul>
    <?php foreach($site->languages() as $language): ?>
    <li<?php e($site->language() == $language, ' class="active"') ?>>
      <a href="<?php echo $page->url($language->code()) ?>">
        <?php echo html($language->name()) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ul>
</nav>

But on the homepage I’am stuck with the German version, if German is set as the preferred language in the browser. If I click on the link for the English version it just reloads the German version.
I can fix it with changing 'url' => '/', to 'url' => '/en', – but I don’t really want the language string in all urls for the primary language, if I can avoid it.

I guess most users won’t switch from German to English if their browser is set to prefer German, but it kinda bugs me to have link to a english version that simply does nothing on the main page.

Any ideas?

Could it be that your browser doesn’t submit the Referrer header?

Without it, Kirby won’t be able to distinguish users who want to change the language back to the default language from users coming from another website that linked to the homepage of the default language.
So if you don’t send the Referrer header, Kirby will think that you just came to the homepage and keep redirecting you to the detected version.

Thanks for your answer!

As far as I’m aware, I did not mess with the referrer header anywhere. I’ve tested it in the latest version of Chrome and Safari and I’ve just tried it with having all plugins disabled. I’ve also tried adding <meta name="referrer" content="always"> to the site header. Did not change anything. Neither did switching from a local to an online testserver nor getting completely rid of all all meta information and java script.
Is there something else I can do to check if this is what causes the problem?

Also: If I do set English as preferred language in the browser (which is also set as default language in the kirby config) I am able to switch the language. Wouldn’t a missing referrer header cause the same problem here?

Could you test if you experience the same problem with a freshly installed langkit (and only setting the autodetect feature to true)?

the langkit installation worked perfectly fine so I replaced the multilanguage setup code in my config with the one from langkit and it worked.

I had:

c::set('languages', array(
  array(
    'code'    => 'en',
    'name'    => 'En',
    'default' => true,
    'locale'  => 'en_US',
    'url'     => '/',
  ),
  array(
    'code'    => 'de',
    'name'    => 'De',
    'locale'  => 'de_DE',
    'url'     => '/de',
  ),
));

and I replaced it with:

c::set('languages', array(
  array(
    'name'    => 'En',
    'code'    => 'en',
    'locale'  => 'en_US.utf-8',
    'default' => true,
    'url'     => '/'
  ),
  array(
    'name'    => 'De',
    'code'    => 'de',
    'locale'  => 'de_DE.utf-8',
    'url'     => '/de'
  )
));

Guess I did get some stupid fault into the first one, but I can’t see the problem. Either way, it works now, so thank you very much!

I’ve got a question about that.
At first I thought I had the same problem. Then I installed the langkit and tried with that. Funny thing is: it worked, but only until I set up my dev environment with gulp and browsersync and stuff. Since then it’s just the same as with the site I’m working on (description of my problem below).
Does anybody have a clue as to why this happens?

On my setup this problem still exists.
I’ve got it the other way round, with German as the default language, but my config-file uses the same syntax as @kiedan’s:

  array(
    'code'    => 'de',
    'name'    => 'Deutsch',
    'default' => true,
    'locale'  => 'de_DE.utf-8',
    'url'     => '/',
  ),
  array(
    'code'    => 'en',
    'name'    => 'English',
    'locale'  => 'en_US.utf-8',
    'url'     => '/en',
  )
)); ````

But still with c::set('language.detect', true); I can’t switch back to the default language from the home page. Clicking my “Deutsch”-link while on myurl.com/en always leads to myurl.com/en again. Everything works when I’m on a subpage, though. So it’s no problem to get from myurl.com/en/whatever to myurl.com/whatever.

The preferred language in my browser is German. Kirbys version is the most recent: 2.3.2

Would be grateful for a clue as to what to do.

I am running into a similar problem @texnixe @lukasbestle. I am running 2.4.1 and i want to create a website with two domains. (i had the website running with only one domain, but two languages before , therefore i only switched / and /en with the urls.)

example.com
example.ch

ONLY when switching the language on the homepage/landing/root-page i get this error, every other page works:

 The example.com page isn’t working

 example.com redirected you too many times.
 Try clearing your cookies.
 ERR_TOO_MANY_REDIRECTS

also the url switches to: example.com/?language=switch .

I copied this from langkit:

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

Again, all pages work fine, except the landing / home page. I can also go to example.com or example.ch and it works.

EDIT:

When inspecting the root/homepage:

example.com/?language=switch is the url my language switch points to, even though it should be example.com

EDIT 2:

I am not sure why, but my old search doesn’t work either. (It works on all pages, but the home page … ). It probably has something todo with the redirect, but i haven’t figured out what yet.

EDIT 3:

Probably has something to do with this (kirby/branches/multilang/page.php).

  // append a query param if the new language is on another domain
  if($this->site->language->host() !== $this->site->language($lang)->host()) {
    $url = url::build(['query' => ['language' => 'switch']], $url);
  }

I am not sure what this function is for, but removing this, solves my problem. Can someone tell me what this is for? Thanks.

EDIT 4 :

As previously pointed out, switching language.detect to false also solved the problem.

Philipp

?language=switch is a special value that is needed for cross-domain language switching. It was actually added by me to prevent endless redirection loops with language detection enabled (otherwise different language session vars for the different domains will redirect back and forth, the query param is supposed to disable that). So it’s strange that it works if you remove it.

Thanks Lukas,

Appending a slash (/) at the end of the url partly solved my problem!

http://example.ch   --> http://example.ch/
http://example.com --> http://example.com/

Urls now have two trailing slashes e.g. http://example.com//blablub

We just realized in a DM conversation that this does not fix the issue but introduces others. This is a bug and will be fixed in the next version:

https://github.com/getkirby/kirby/issues/539

1 Like