This is support in need of support
I’ve got the following language setup:
c::set('languages', array(
'en' => array(
'name' => 'English',
'code' => 'en',
'locale' => 'en_US',
'default' => true,
'url' => '/',
),
'de' => array(
'name' => 'Deutsch',
'code' => 'de',
'locale' => 'de_DE',
'url' => '/de',
'default' => false
)
));
The default language does not have a language code.
This is my pretty simple route, which is supposed to redirect to the first child if a page has subpages:
c::set('routes', array(
array(
'pattern' => '(:any)',
'action' => function($uid) {
$page = page($uid);
if($page->hasVisibleChildren()) {
$page = $page->children()->visible()->first();
}
else {
$page = $page;
}
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
),
));
This works perfectly fine for the default language, but the non-default language throws an error, because $uid == de
is obviously not a page.
Now I could change my language setup and give the default language a language code as well like this:
c::set('languages', array(
'en' => array(
'name' => 'English',
'code' => 'en',
'locale' => 'en_US',
'default' => true,
'url' => '/en',
),
'de' => array(
'name' => 'Deutsch',
'code' => 'de',
'locale' => 'de_DE',
'url' => '/de',
'default' => false
)
));
Then the following route works for both languages:
c::set('routes', array(
array(
'pattern' => '(:any)/(:any)',
'action' => function($lang, $uid) {
$page = page($uid);
if($page->hasVisibleChildren() && !($page->is(page('home')) || ($page->is(page('news'))))) {
$page = $page->children()->visible()->first();
}
else {
$page = $page;
}
if(!$page) $page = site()->errorPage();
return site()->visit($page, $lang);
}
),
));
Where is the problem, then? Well, I don’t want to use the language code for the default language.
I have also tried to get this working with some regex, but to no avail, because if the non-default language has a language code and the other one does not, I can’t fetch the page UID for the non-default language because the language code is sort of “in the way”. What am I overlooking? Any suggestions?
Thanks.