Is it possible to tell Kirby to send a response in a specific language for an API GET Request? I’d like to fetch content either in German or in English, depending on the the selected language in the frontend. Can’t find anything in the docs.
Is there an easy way to include this in a custom endpoint response? It’s currently only working for requests to the /api
endpoint.
Here is my config:
<?php
return [
'debug' => true,
'languages' => true,
'api' => [
'basicAuth' => true
],
'routes' => [
[
'pattern' => 'rest/(:all)',
'method' => 'GET',
'env' => 'api',
'action' => function ($path = null) {
$kirby = new Kirby([
'roots' => [
'index' => dirname(dirname(__DIR__)) . '/public',
'base' => $base = dirname(dirname(__DIR__)),
'content' => $base . '/content',
'site' => dirname(dirname(__DIR__)) . '/site',
'storage' => $storage = $base . '/storage',
'accounts' => $storage . '/accounts',
'cache' => $storage . '/cache',
'sessions' => $storage . '/sessions',
]
]);
$request = $kirby->request();
$render = $kirby->api()->render($path, $this->method(), [
'body' => $request->body()->toArray(),
'headers' => $request->headers(),
'query' => $request->query()->toArray(),
]);
$decoded = json_decode($render, true);
// Kirbytags
function kt($array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = kt($value);
} else {
$array[$key] = kirbytags($value);
}
}
return $array;
}
$decoded = kt($decoded);
return $decoded;
}
]
]
];
You can check for the header as well and then set the language accordingly.
$kirby = new Kirby([...]);
if ($languageCode = $request->header('x-language')) {
$kirby->setCurrentLanguage($languageCode);
}
My kirby instance within my endpoint doesn’t recognise my multilang setup and thus can’t setCurrentLanguage
. Is there any way to force the kirby instance to run as multilang?
I think you have to set up multilang then when creating your custom Kirby instance within your endoint.
<?php
return [
'debug' => true,
'languages' => true,
'api' => [
'basicAuth' => true
],
'home' => 'index',
'routes' => [
[
'pattern' => 'rest/(:all)',
'method' => 'GET',
'env' => 'api',
'action' => function ($path = null) {
// Include languages
$languages = glob(dirname(dirname(__DIR__)) . '/site/languages' . '/*.php');
$langArray = array();
foreach($languages as $language) {
$langArray[] = include $language;
}
$kirby = new Kirby([
'roots' => [
'index' => dirname(dirname(__DIR__)) . '/public',
'base' => $base = dirname(dirname(__DIR__)),
'content' => $base . '/content',
'site' => dirname(dirname(__DIR__)) . '/site',
'storage' => $storage = $base . '/storage',
'accounts' => $storage . '/accounts',
'cache' => $storage . '/cache',
'sessions' => $storage . '/sessions',
],
'languages' => $langArray
]);
$request = $kirby->request();
// Language
if ($languageCode = $request->header('x-language')) {
$kirby->setCurrentLanguage($languageCode);
}
$render = $kirby->api()->render($path, $this->method(), [
'body' => $request->body()->toArray(),
'headers' => $request->headers(),
'query' => $request->query()->toArray(),
]);
$decoded = json_decode($render, true);
// Kirbytags
function kt($array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = kt($value);
} else {
$array[$key] = kirbytags($value);
}
}
return $array;
}
$decoded = kt($decoded);
return $decoded;
}
]
]
];
Solved for now. Wondering if there is an easier way though
Look at Languages::load()
: https://getkirby.com/docs/reference/objects/languages/load
1 Like