How to avoid automatic fallback for translations

Hey there.

I’ve a 2-lang website (FR and EN), the majority is in FR without translations. I don’t want that Kirby automatically shows FR articles when in EN.
I can’t find any method to get available languages of a page. Is there ?

I find this Find() page in specific language but

$page->content('en')

always returns true, even if only FR translation file is present.

$page->language()

is not documented and returns … nothing.

How am I supposed to do that ?

You can check if content is available like this:

 <?php if(site()->language()->code() == $page->language()->code()) { 
//do stuff, e.g.
echo $page->title() ;} 
?>

To filter articles based on available languages, you can use a custom filter:

$articles = $page->children()->visible()->filter(function($child) {
     return site()->language()->code() == $child->language()->code();
});

This will only work for Kirby 2, btw. almost all solutions in this forum relate to Kirby 2.

Yes, I am in Kirby 2. :smile:
But,

$page->language()->code()

doesn’t work. Always return empty string.

I noticed that with Kirby 1, this method used to work: https://www.texniq.de/en/texniq-u/kirby-multilingual-site

Something like:

$currentLang = c::get('lang.current');
if ($page->content($currentLang)) {
    // do stuff
}

Yes, you are right, $page->language()->code() does not work anymore, but

<?php echo $page->content()->language()->code() ?>

does. I guess this was changed, because in an older installation, the first bit of code does in fact still work.

Thanks, but I get this error:

Fatal error: Call to a member function code() on boolean

However $page->content()->language() seems to work, but only on translated article and returns an empty string in other cases. I would expect to return the default lang?

I suppose I could deal with that though…

I managed to do it. Something like:

// get own language of page
$ownLang = $child->content()->language();
// if empty string, use default language
if ($ownLang == '') {
    $ownLang = $site->defaultLanguage()->code();
}
// page doesn't exist in current language
if ( $site->language()->code() != $ownLang ) {
    // do stuff
}

Thanks @texnixe. :wink: