Activate or set language inside Route

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.

From where is the route called?

It is called from Client Side Javascript in a POST request made via fetch()

Well, usually you can use the language prop in the route definition, but I’m not sure how that would work when you call the route via JS. So the easiest way forward would probably be to pass the current language in your post data?

Thanks for the response. I’m doing that already. Now that I receive the language code from the client side, I’m looking for is a general purpose way of activating that language inside my route code, so that I can access language-correct page-titles, language strings and so on.

You can get content in a given langauge via the content object, e.g.

$page->content('en')->title();

Same for the t() helper: t() | Kirby CMS

Thank you! I was hoping for a way where I would be able to set the language once and then not specifiy it for everthing that I’m accessing – similar to the way we access things in templates. But for now, I can also make this work also :slight_smile:

Ah, ok, what you could try is to use site()->visit(), which expects a page as first param and the language code as second.

This should then set the language context.

This is perfect, thanks!