Maybe I’m trying to do too many things at once.
Starting over and reading this and other threads I have solved the articles problem using these routes and a model:
// site/config/config.php
'routes' => [
[
'pattern' => '(:any)',
'language'=> 'fr',
'action' => function($language, $uid) {
$page = page($uid);
if(!$page) $page = page('articles/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
],
[
'pattern' => 'articles/(:any)',
'language' => 'fr',
'action' => function($language, $uid) {
go($uid);
}
],
// Route for English articles (news)
[
'pattern' => '(:any)', // Match any UID for English
'language' => 'en',
'action' => function($language, $uid) {
$page = page($uid); // Check for a direct page match
if (!$page) $page = page('news/' . $uid); // Check for English articles
if (!$page) $page = site()->errorPage(); // Fallback to the error page if not found
return site()->visit($page); // Visit the found page
}
],
[
'pattern' => 'news/(:any)',
'language' => 'en',
'action' => function($language, $uid) {
go($uid);
}
],
]
// site/models/article.php
use Kirby\Cms\Page;
class ArticlePage extends Page {
public function url($options = null): string {
$languageCode = $this->kirby()->language() ? $this->kirby()->language()->code() : '';
// because my website is configured not to have the /fr/ for the default language
if ( $languageCode == 'fr') {
return url( $this->slug( ) );
} else {
return url( $languageCode . '/' . $this->slug( ) );
}
}
}
Now I still could not manage to route the /products/ urls and have another bug, the language switcher link populated with this code does not work anymore on articles/ or /news/ pages: the link sends to the current page.
<a class="languageswitcher"
href="<?php e($page->translation($language->code())->exists(), $page->url($language->code()), page('error')->url($language->code())) ?>">