API and Web Application Interactions
There are three ways for communicating the user locale from the web application to the API:
- Requests with
Accept-Language
HTTP header for validation error messages. - Query parameters when you cannot set the previous HTTP header (links, for instance).
- Update the user's locale via a GraphQL request if authenticated.
- Update the user's locale on login if the web application locale is not the same as the locale from the API
#
HTTP HeaderUse case: translating the validation error messages.
Each time the user changes its locale on the web application, the src/webapp/plugins/i18n.js plugin will
update the HTTP header Accept-Language
for the next GraphQL requests with the new value.
The LocaleSubscriber
class from the API reads the value of this HTTP header to set the locale on its side.
#
Query ParametersIn some use cases, you cannot set an HTTP header. For instance, when the user clicks on a link, you will have to use query parameters:
In the Symfony Boilerplate, we use this solution for XLSX exports.
#
Authenticated user's localeAs explained before, whenever an authenticated user changes the locale on the web application, we run the
updateLocale
GraphQL mutation.
In the API, the UpdateLocale
use case updates the locale
property of this user.
note
📣  This property helps to know in which locale the API has to translate emails for this user.
We also call the updateLocale
GraphQL mutation on page src/webapp/pages/login.vue
in the specific scenario where the web application locale is not the same as the user's locale from the API.