Small question here concerning languages in a custom route. I have a Custom Route defined in a Blueprint that accepts some data sent from a user and returns a small string with either an error or a success message saved in my language variables as well as the title of a specific page. All of the things returned to the user should be returned in the right language.
The route itself is hardcoded and its pattern is not language specific. So right now the route doesn’t know anything about the language its being accessed in. I figured, when I send the language code from the client side that I will be able to activate that language inside my route – but I have found no documentation on how to “activate” a language.
Kirby::plugin('MMS/fc-surveyresponse', [
'routes' => [
[
'pattern' => 'my-api',
'method' => 'POST',
'action' => function () {
$kirby = kirby();
// get request data
$request = $kirby->request();
$method = $request->method();
$header = $request->headers();
$data = $request->body()->toArray();
if(isset($data['user_lang'])){
// activate the prefeerred language – but how … ?
}
…
return [
'message' => t('my-success-message'),
'title' => $kirby->page('somepage')->title()
];
}
]
]
]);
While I do understand that I can just request the specific locale in the t() function, I’m looking for a more general and sturdy solution that will work across language strings, field values and page titles.