Search function for multi-language sites

Hi. I’m new to Kirby and I’m just trying to figure some things out.

First, congrats! I had a multi-language test site up and running like a charm in under 5 minutes… Then I added a search function, which also works, but only for the English site (English is set as standard language).

i.e. When I have one page open in another language and start a search, it throws back a “no results for …”.

Is there a way that Kirby (1) loops through all languages and shows the results, or (2) only returns results of the currently active language?

(By the way… Does this affect also tag functionality for a blog in multiple languages? I haven’t tried it yet…)

Thanks for your hints, links, solutions!

Hi,

I have the same problem with a multi-language search function.

$site->index()->visible()->search($query, 'title|text'); 

How can I filter the current language before pagination?

Thank you very much!

Julien

1 Like

This is what I learned from user FabianSperrle:

foreach ($page->children()->filterBy('languages', $site->language()->code(), ',') as $child) {

Source: Filter child pages by available translations

Where do I find out what can be filtered by filterBy function? The example above is not mentioned in the docs.
Is this a working example? Sadly, it does not work for me when applying the filterBy function to foreach function.

I also tried to filter after index()

$results = $site->index()->filterBy('languages', $site->language()->code(), ',')->visible()->search($query, 'title|text');

but did not succeed.

EDIT: I now understand that this example of FabianSerrle only works when adding custom fields to the text files.
Is there any other way to filter by language? This should be a basic function of a CMS, I guess.

1 Like

You could use a custom filter like this:

1 Like

The filterBy method can filter by basically everything you can call as method on a page, be it a field or something like url().

1 Like

That’s interesting! I did not know there is another filter method besides filterBy. This one gives me the opportunity to create my own “comparator”…

I will try this soon.

Thank you for your help! :smile:

But this time it’s even in the docs: http://getkirby.com/docs/cheatsheet/pages/filter

1 Like

Great, I will try this! Thank you!

Hi texnixe,

I still have an issue with understanding

$child->language()->code()

Is language() returning a native attribute of the child page or just the function to retrieve the custom language field I have to use in my text file?

The filter method you mentioned only works when explicitly adding a custom field named “language” to the text file.

Isn’t there a way to easily filter by file name ending? (like *.en.txt or *.de.txt). I would expect this kind of filter to be included as it already has to be used by the core itself!?

1 Like

Yes, the code above requires a special language field.

Otherwise you have to use a custom filter that checks if the page is available in the respective language, like in the switch example: Language switcher example creating invalid URLs for missing translations