JSON representation multilanguage


I’m following this cookbook article on generating JSON with Kirby: https://getkirby.com/docs/cookbook/templating/generating-json for creating an AJAX-based website.

Simple question:

On a multilanguage page (D/E), instead of having only one template /site/templates/blog.json.php, to make it work, would it be
and /site/templates/blog.de.json.php?

With the two json files containing something like:

$json[] = [
    'title' => (string)$article->title("de"),

and something like:

$json[] = [
    'title' => (string)$article->title("en"),

So there would be: blog.de.txt and blog.en.txt but I can’t really figure out how the multi-language AJAX call in Kirby would look like. Up until now, I only scripted single-language AJAX-based Kirby websites which call a PHP file like:

<?php if(kirby()->request()->ajax()) {

Thanks for any input!

I think it depends on the language in the URL you’re using in the AJAX call. E.g. mydomain.com/en/blogpost-a.json should resolve to English, whilst mydomain.com/de/blogpost-a.json should resolve in German (depending on how you’ve setup kirby multilang).

No need to do language specific things in your content representation /site/templates/blog.json.php: this should resolve just fine:

$data = [
    'title' => (string)$page->title(),
echo json_encode($data);

More info: https://getkirby.com/docs/guide/templates/content-representations

1 Like