Cannot get correct current language on subpages

I’m using $kirby->language()->code() to get the current language code inside a site controller.
On the front-/homepage, it returns the correct code:

$currentLang = $kirby->language()->code();
file_put_contents('TEMP/TEMP.txt', print_r($currentLang, true));

Checking curr. language on /de frontpage

Screenshot 2023-09-20 at 20.22.06
Screenshot 2023-09-20 at 20.23.18

Checking curr. language on /en frontpage

Screenshot 2023-09-20 at 20.24.01
Screenshot 2023-09-20 at 20.25.04

However, on all subpages, the default language (de) is returned, regardless of which language is prefixed in the URL:

Checking curr. language on some /en subpage

Screenshot 2023-09-20 at 20.26.14
Screenshot 2023-09-20 at 20.26.37

Any idea why this is happening and how to resolve it?

Cheers

Have the languages been enabled in config?

config/config.php

return [
'languages' => true,
...

Yes.

Screenshot 2023-09-20 at 20.39.06

In the content folder do you have two txt.files each with the language code, for example:

default.de.txt
default.en.txt

or is there only one .txt?

default.txt

Also yes.

Screenshot 2023-09-20 at 20.45.49

Btw, I’m currently using this workaround to get the current lang. from the URL:

function get_currentLangFromURL() {
  $currentLang = null;
  //////
  $url         = $_SERVER['REQUEST_URI'];
  $url         = explode('/', $url);
  $url         = array_filter($url);
  $url         = array_values($url);
  //////
  if($url[0] === 'en') $currentLang = 'en';
  else                 $currentLang = 'de';
  //////
  return $currentLang;
}

Unfortunately, I’m not sure, where the devil is in the detail.
But someone here will quickly suggest a solution to you.

What’s your Kirby version?
What is the default language?
Please post your language configuration?
Are you using any plugins/routes/custom code that might cause this?

I cannot reproduce this behavior in a fresh 3.9.6.1 Starterkit with languages enabled.

The project concerned is running Kirby 3.9.2

I think I’ve found where the issue lies. There is some custom routing in the site config; for routes meeting a certain condition I need altered content to be returned, for all routes not matching the condition the regular page content should be returned. I’ve implemented this as follows (reduced version):

[
  'pattern' => '(:any)/(:any)',
  'action'  => function($lang, $targetRoute) {
    $routeMatchesCondition = ...
    if($routeMatchesCondition) {
      $customContent = ...
      return $customContent;
    } else {
      return page($targetRoute);
    }
  }
],

When removing this routing pattern, all subpages return the correct current language.

In my understanding, page($targetRoute) would simply return the called page just as if it had been accessed without any special routing rules in place, which it does, just in the default language i.o. the actual current language. I naively thought, including the $lang string in the URL passed to the page() method would fix this…

[
  'pattern' => '(:any)/(:any)',
  'action'  => function($lang, $targetRoute) {
    $routeMatchesCondition = ...
    if($routeMatchesCondition) {
      $customContent = ...
      return $customContent;
    } else {
      return page($lang . '/' . $targetRoute);
    }
  }
],

…which it doesn’t, it just breaks the route entirely, causing the error page to be returned.

So how do I make sure that the page returned in the routing pattern is in the current language?

In a multi-lang context, your route should use the language key:

'routes' => [
	[
		'pattern' => '(:any)',
		'language' => '*',
		'action' => function ($language, $slug) {

			$condition = false;
			if($condition) {
			    return 'whatever';
			} else {
			    return page($slug);
			}
		}
	],
]
1 Like

Ah, I did not know that, thank you for pointing it out. Including the language key fixes the problem, all routes now return the actual current language.