I use Kirby as a headless CMS to produce content for my web app as JSON from custom API endpoints. I build the JSON responses using all the normal Kirby page and field methods that you would on front end templates.
I am updating to a multi language app and I have my translated .txt files for the content. How can I select different languages for the return values when am not using Kirby front-end templates, and therefore a URL scheme to identify the language as per documentation?
e.g. Is there a method to set the language I am interested in when getting values from $site or $page? Couldn’t see anything in the Reference docs.
Many thanks in advance.
I should add, I have tried the $page->translation() method but it produces an error:
$myPage = $pages->find('myPage')->translations()
returns what I’d expect:
Kirby\Cms\Collection Object (
[0] => de
[1] => en
[2] => es
[3] => fr
[4] => it
[5] => po
)
But, if I try to select one of those translations with:
$content = $myPage->translation('es')
And then return a translated field with:
$content->myField()->toString()
I get an undefined method error:
Call to undefined method Kirby\\Cms\\ContentTranslation::myField()
Did you try with content()
method like that:
$myField = $myPage->content('es')->myField()->value();
Thanks! That works
Didn’t see in the docs that you set desired translation with content()
method.