Find content in correct language within controller

Heya

I have run into a problem that I can’t seem to fix myself.
I tried looking into the documentation, but so far it yielded no solution.
Maybe I was just looking in the wrong place.

Preset:
I have a site that has different articles (all in document root). These articles each have a category. The categories are set within site as a structure.
On the homepage I am showing all articles, sorted by publication date.
There is also a menu-item, showing just the articles of the chosen category.
This was all setup using german.
Now the client wants to add english as well, and this is where things don’t work out.

Here the original, german only code:

# config.php; show all articles of a category
[
	'pattern' => 'artikel/(:any)',
	'action' => function ($value) {
		return page('home')->render([
			'cat' => $value,
		]);
	}
],
#config.php; show individual article, first placeholder is category slug
[
	'pattern' => 'artikel/(:any)/(:any)',
	'action' => function ($value, $article) {
		if ($page = page($article)) {
			return page($page);
		}
	}
],
# /controllers/home.php
<?php

return function ($page, $cat) {
	$category = null;
	if($cat) {
		$articles = false;
		foreach (site()->categories()->toStructure() as $s) {
			if(Str::slug($s->cat()) === $cat) {
				$category = $s;
				$articles = site()->children()->listed()->filterBy('category', $s->cat())->sortBy(function ($page) {
				  return $page->date()->toDate();
				}, 'desc');;
				break;
			}
		}
		if(!$articles OR count($articles) === 0) {
			go('/');
		}
	} else {
		$articles = site()->index()->filterBy('template', 'article')->listed()->sortBy(function ($page) {
		  return $page->date()->toDate();
		}, 'desc');
	}

	return [
		'articles' => $articles,
		'category' => $category
	];

};

This works fine.

Now I added english. English has it’s own url-prefix (/en), whereas the german prefix is /.

I was not able to figue out the routes correctly, so I duplicated the routes:

'pattern' => 'en/article/(:any)',
(..)
'pattern' => 'en/article/(:any)/(:any)',

The controller did not seem to notice the correct language, so I added a parameter in the routes and used content() to fetch the correct language:

return function ($kirby, $site, $page, $cat, $lang = 'de') {
	$category = null;
	if($cat) {
		$articles = false;
		foreach ($site->content($lang)->categories()->toStructure() as $s) {
			if(Str::slug($s->cat()) === $cat) {
				$category = $s;
				$_articles = site()->index()->filterBy('template', 'article')->listed()->sortBy(function ($page) {
				  return $page->date()->toDate();
				}, 'desc');
				foreach ($_articles as $art) {
					if($art->content($lang)->translated()->toBool() && Str::slug($art->content($lang)->category()) === $cat) {
						$articles[] = $art->content($lang);
					}
				}
				break;
			}
		}
#(...same code as above...)

So now the articles are loaded in the correct language, but all my I18n:: or t() functions don’t translate into english.
Outputting $kirby->language() shows en as language throughout the script.

So my question is: what is the correct way to “tell” the controller, which language is currently selected?
Seeing that I sort of had to hack the language support, I am sure, I missed something :frowning:

Any help appreciated!

Use the language scope instead of the language prefix: https://getkirby.com/docs/guide/routing#multi-language-setup__language-scope

damn! that was easy! thank you :slight_smile: :slight_smile: