Get pages per language

I’m currently working on a multilanguage website (german, english) where I want to integrate algolia for search. I was able to hack a solution where i was able to create a index per language.

But the way I did it supposes that I switch the default language for indexing.

Now I ask you if somebody had to do a similar task with kirby or how to get all pages from a certain language?

Note that this doesn’t work for the default language, because the Panel always creates a page in the default language.

You can adapt this to run through all existing languages and create an index for each language.

Thanks Sonja for the quick response, but I still have problems.

For testing purposes I tried your suggestions on the sitemap but I still just get the pages for the default language.

I tried it this way.

site()->index()->filter(function ($child) {
    return $child->translation(kirby()->language()->code())->exists();
});

I was thinking of something like this:

<?php

foreach ($kirby->languages() as $language) {
    $index_{$language->code()} = site()->index()->filter(function ($child) {
        return $child->translation($language->code())->exists();
    });
}

(not tested).

I’ve tried this

<?php

foreach ($kirby->languages() as $language) {
    $index[$language->code()] = $pages->filter(function ($child) use 
    ($language) {
        return $child->translation($language->code())->exists();
    });
}

but still get only the pages for the default language

Hm, I have to check this…

Hm, it works for me (at least if I loop through the index, because I was lazy and only “translated” a single subpage with the following result in a Starterkit:

Array
(
    [de] => Kirby\Cms\Pages Object
        (
            [0] => notes/exploring-the-universe
        )

    [en] => Kirby\Cms\Pages Object
        (
            [0] => photography
            [1] => photography/animals
            [2] => photography/trees
            [3] => photography/sky
            [4] => photography/ocean
            [5] => photography/desert
            [6] => photography/mountains
            [7] => photography/waterfall
            [8] => photography/plants
            [9] => photography/landscape
            [10] => notes
            [11] => notes/across-the-ocean
            [12] => notes/a-night-in-the-forest
            [13] => notes/in-the-jungle-of-sumatra
            [14] => notes/through-the-desert
            [15] => notes/himalaya-and-back
            [16] => notes/chasing-waterfalls
            [17] => notes/exploring-the-universe
            [18] => about
            [19] => error
            [20] => home
            [21] => sandbox
        )

)

I gave it another shot and now it works but I’m not shure what I did wrong in the first place.

Thanks for helping out!