# Cannot get correct current language on subpages

**URL:** <https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541>\
**Category:** Questions\
**Tags:** v3\
**Created:** [September 20, 2023, 6:27pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541 "2023-09-20T18:27:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [September 20, 2023, 6:27pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/1 "2023-09-20T18:27:30Z")

</div>

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:

```php
$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](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/5/572df1366d2c31a1c4717d5cc731121051819d83.png)  
 ![Screenshot 2023-09-20 at 20.23.18](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/b/b33eb9151be844f5cdeb553b8d02dbd5e0be1cce.png)

_Checking curr. language on /en frontpage_

![Screenshot 2023-09-20 at 20.24.01](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/285005381c9419607f5ffd2937a3ab2ad56bcc66.png)

 ![Screenshot 2023-09-20 at 20.25.04](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/0/00114d024ebafff1764ee25cce8fe4c41fd96d52.png)

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](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/5/5bc5f18ca365212343eb5f1c80af9751c5169852.png)  
 ![Screenshot 2023-09-20 at 20.26.37](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/d/d693b2cf71e251e8f5f4caac9a98f462ba269d8f.png)

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

Cheers

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [September 20, 2023, 6:36pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/2 "2023-09-20T18:36:36Z")

</div>

Have the languages been enabled in config?

**config/config.php**

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

```

---

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [September 20, 2023, 6:39pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/3 "2023-09-20T18:39:20Z")

</div>

Yes.

![Screenshot 2023-09-20 at 20.39.06](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/1/1747343659a74f45dd5171e30ebca779944b3ec1.png)

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [September 20, 2023, 6:42pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/4 "2023-09-20T18:42:40Z")

</div>

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

```auto
default.de.txt
default.en.txt

```

or is there only one .txt?

```auto
default.txt

```

---

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [September 20, 2023, 6:46pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/5 "2023-09-20T18:46:06Z")

</div>

Also yes.

![Screenshot 2023-09-20 at 20.45.49](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/b/b6d4793856c4a5cb3c99878655fa42111f525fbe.png)

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

```php
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;
}

```

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [September 20, 2023, 6:50pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/6 "2023-09-20T18:50:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 20, 2023, 8:19pm UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/7 "2023-09-20T20:19:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [September 21, 2023, 6:19am UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/8 "2023-09-21T06:19:44Z")

</div>

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):

```php
[
  '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…

```php
[
  '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?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 21, 2023, 7:53am UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/9 "2023-09-21T07:53:34Z")

</div>

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

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

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

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

```

> **[Routing](https://getkirby.com/docs/guide/routing#multi-language-setup)**
>
> Use Kirby's router to modify URLs or provide extra functionality when the user calls an given URI pattern.

---

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [September 21, 2023, 8:26am UTC](https://forum.getkirby.com/t/cannot-get-correct-current-language-on-subpages/29541/10 "2023-09-21T08:26:15Z")

</div>

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.
