Pimcore ajax set correct locale

How do you set the correct language in ajax requests (for the Translator class)? I have ajax requests which return certain labels for the frontend but even if requested from a german document the english translations are returned (obviously Pimcore can not know which language to use within the route).

What is the correct way to achieve localization in ajax requests?

Hi @Blackbam,

I usually add the locale to the route like so:

app_api_myendpoint:
    path: /api/{_locale}/my-endpoint
    defaults: { _locale: 'en' }

Then I inject the locale into my route in JavaScript like so:

const url = `/api/${document.documentElement.lang}/my-endpoint`;

This is just an example and Iā€™m sure there are multiple other ways to achieve the same. Hope this helps anyways!

1 Like