Search the $pages collection on multilanguage websites

Im having trouble understanding why I cannot use features like ->search(), filterBy() or find() for pages that are multilanguaged.

For example: If I have one page but two translations for it one is called “Zielorte” and one is called “Destinations”. They are the same page just transalated. German beeing the default language and english beeing my secondary language.

If I try to use the following snippet I will get a result returned like I would assume it to happen:

$data = $pages->search("zielorte", 'title');

When doing this though I will not get any results:

$data = $pages->search("destinations", 'title')

I come across the same problem with every method i’ve tried for the $pages collection that should help find specific pages.

You could just do something like:

$data = $pages->search("zielorte", 'title'):
echo $data->content('en')->field();

But not in my case, because whatever will be inserted into search() will be provided by the user, so I have to be able to search my $pages collection in other languages than the default language.

$pages contains all the pages in the current language of kirby.

$pages->search() is intended to be exposed to let users actually search for text in the current language version of your content.

As a user I’ld actually would find it very confusing to have returned results in different languages?
If a user wants to search in English instead of the default German; he/she should switch to Kirby in English globally.

You are completly right.

But how do I go about setting the current language which should be used to search the $pages collection.
Because all my my code for this is inside a router and the route only functions as a ajax endpoint. So theres no direct “current language”.

Aha, gotcha.

Try running site()->visit(site()->homepage(), $lang); at the top of your route handler where $lang is e.g. en (or de).

1 Like

Works like a charm! Thx