Page::create() translations array

This has been asked but, as far as I’ve seen, not throughly answered

When using page::create() or createChild() the page is always created in the default language as per this comment.

But as part of the $props parameter, we can include a ‘translations’ array, which in the reference page is described as Create the translations collection from an array.

This sounds as possibly related to providing translations of fields when programatically creating a page, so we can create the page on all the necessary languages at once, but I can’t find any documentation or examples on it.

Can someone shed light on the topic ?

Thank you

No, Kirby only creates the page in the default language and for the translations you have to update the page. You will see that when you have a look at the source code for the method.

Ok.

But what is the ‘translations’ array ?

Also can we chain update after create?

Thanks

The translations array would look like this for example:

$translations = [
  [
    'code'    => 'en',
    'content' => [
      'title' => 'The ocean is green',
      'tags'  => 'green, blue',
    ]
  ],
  [
    'code'    => 'de',
    'content' => [
      'title' => 'Der Ozean ist grün',
      'tags'  => 'grün, blau',
    ]
  ],
];

You can then create the new page and after that loop through the translations to update:

$newPage = Page::create([
  'parent'       => page('notes'),
  'slug'         => 'green-ocean-water',
  'template'     => 'note',
  'translations' => $translations,

]);
foreach($newPage->translations()->filterBy('code', '!=', $kirby->defaultLanguage()->code()) as $translation) {
  $newPage->update($translation->content(), $translation->code());
}

Ok thank you.

But… then the translations array does absolutely nothing on page creation ?

Is its purpose solely to be used in a posterior update as in the code above?

Also… if I am doing this in a loop, for regular pages with few fields, can I assume page creation is instantaneous, so for something like the code above I don’t have to worry about the page being created when running the foreach ?

Thank you

Well, in the full code you would do this in a try/catch block to catch exceptions when the page cannot be created and maybe an additional if statement to make sure $newPage exists.

Well, in the example above, $newPage now contains the translations you can loop through, so while this doesn’t create the translations immediately, they are available in the variable.

Ok, but… usually the translations property contains a content object per each available language for that page is that correct ?

And what you are doing here is kind of squatting that variable with the content you will later use on the update.

If the above is correct It is perhaps a bit confusing that in page::create we can provide a translations array -which is usually the array that contains the page translations for a page that has translations in file- but in this case these translations are not applied to the created page, but just sit there.

Am I making sense?

thank you

I’d put that differently: they are applied as properties to the page object, but not saved to disk.

1 Like