Translations for Virtual Pages

Hey guys,

I have set up my virtual pages and have tried translating them, but I can not seem to get the syntax right. I have looked through the forums and the docs as there is an example there, but none really apply to my use case. In the API, each bonus is duplicated multiple times with a different translation in each, hence why I am checking by country code.

However, in the panel, I am getting this error:

The section ā€œpublishedā€ could not be loaded: Invalid language:

Here is my way of checking translations:

$children = array_map(function ($bonus) {

            $lang_code = ''; 

this is required as there are many countries being pulled in and I only need a few
            if($bonus['country'] == 'CA') {
                $lang_code = 'ca';
            } elseif ($bonus['country'] == 'NZ') {
                $lang_code = 'nz';
            } else {
                $lang_code = 'gl';
            }

            $slug  = Str::slug($bonus['brand']);
            $page = $this->subpages()->find($slug);

            return [
                'slug'     => $slug,
                'num'      => 0,
                'template' => 'bonus',
                'model'    => 'bonus',
                'translations' => [
                    $lang_code => [
                        'code' => $lang_code,
                        'content' => [
                            'title'    => $bonus['brand'],
                            'seoTitle'   => $page ? $page->seoTitle()->value() : null,
                // //    'seoDescription'   => $page ? $page->seoDescription()->value() : null

                            'featured'   => $page ? $page->featured()->value() : null,
                            'ref_url'   => $page ? $page->ref_url()->value() : null,
                   
                        ]
                    ]
            ];
        }, $results);

    return Pages::factory($children, $this);

What could be causing this, or how can the syntax/logic be set up to work?

One error is thatā€™s you donā€™t filter the array, but only assign a language code to some. So you will still have items without a language code which will not work.

$lang_code defaults to ā€œglā€ if no language code is found. Is that the array you are thinking of?

Ah, ok, Iā€™m not fully awake yet, sorry.

Thatā€™s okay, happens to me all the time :laughing: does the translation syntax look correct to you? Is it possible to have that kind of translation set up on a page model?

Hm, the structure looks the different from here, I think: Database demo, multi-language setup? - #3 by distantnative

You have used an additional array level with the language codeā€¦

I was using the code from here: Simple virtual page | Kirby

This is for a different file but I thought it could transfer over. I will try the one you posted :slight_smile:

Actually, I think the structure will somehow not work out, because inside translations, you need this array for each language

  [
     'code' => 'en',
     'content' => [
       'title' => 'Child',
    ]
],

But in reality, you loop through all resultsā€¦

Iā€™m not sure either, but the problem I see, is what I posted in my last postā€¦

You loop though each result and create a new child for each result, instead of adding languages to a single page-

But all the translations would be saved to the same page as long as the ā€˜numā€™ and ā€˜slugā€™ are the same? I assumed they acted kind of like an ID for the page.

But if the page expects an array of translations, this wonā€™t work out and it obviously doesnā€™t.

I think what you need to do is group your results by translations, then create one page per group, with the slug of the default page and the translations in the translation array.