Using the JSON content api in combination with multilanguage sites

Hello,

i am using the Kirby JSON Content api pretty much as described here (https://getkirby.com/blog/json). This works like a charm generally, yet i wonder how i would modify this if you need to support multiple languages. At the moment the content folder looks like this home/api/homeapi.txt in combination with home/home.txt which features the usual content.
Now when i include more than one language i have to add a home.en.txt for instance which will then be resolved for the EN language tag. So generally i would like to know how i can make use of the json api for every language because this structure will always point to the home.txt ignoring all other content files of the same page. In my head it would be something like home/en/api/homeapi.txt, yet that doesn’t see to be too helpful.

Thanks in advance,
Simon

That tutorial is over four years old now and was referring to Kirby 1.

Today you can achieve similar results waaay easier using a route like this:

c::set('routes', array(
  array(
    // Example: /api/en/projects/project-a
    'pattern' => 'api/(:any)/(:all)',
    'action'  => function($lang, $uri) {
      site()->visit($uri, $lang);
      
      return response::json(page()->toArray());
    }
  )
));

Of course you will need to tweak the code to include the fields you actually need in the frontend. This short example returns the whole $page object.


PS: You shouldn’t have both a home.txt and home.en.txt file. Please see the multilang setup docs.

1 Like

Thanks for your quick response. Ah yes of course, the language code should always be included in the txt files name. My last multi language kirby project was too long ago it seems. This works like a charm, thank you very much! And also saves a lot of useless files.