How to select pages of particular language in multilingual website?

Hiya,

I have a multilingual website in that sense that English is my default language (I had to pick one), but my site also has pages in German, French and Dutch. But not every page exists in all languages, some even don’t exist in English.

Now on the English part of the site I want to display all English articles, but not the other languages.

I tried the Cookbook (Quicktip: Filtering by language), but I couldn’t get it up and running - the page still displayed the non-English articles.

So, two questions:

  • when does a page qualify as “translated” (so the Cookbook works)?
  • If I want to use a field called “Language” and define the value as “EN”, “DE”, “FR” or “NL”, then this should work, right?:
$listing->add(page('blog')->children()->listed()->filterBy('language', $kirby->language()->code()));

But it doesn’t. Any idea why not?

Thanks in advance for your help!

All the best,
Stephan

A page is regarded as translated when a content file exists in the file system. That will always be true for the default language, because the Panel always creates this file, even if you try to create a new page from a non-default language.

So that means that filtering by the method in the Cookbook recipe won’t work for the default language.

It also means that when you store an empty translation or a translation that only has for example the title translated, will be regarded as an existing translation.

To make sure that a page can be regarded as fully translated, it would therefore make sense to implement a toggle field that marks a translation as translated. Then you can to filter by this field.

Then you should be able to use a filter like this (untested, assuming a field named isTranslated of type toggle:

$translatedPages = page('somepage')->children()->filter(function ($child) {
  return $child->content(kirby()->language()->code())->get('isTranslated')->bool();
});

The callback should then also return false if the field is empty.

That worked perfectly! Apologies for the late reply.

Thanks for all the support!

All the best,
Stephan