For a client to get a resource in a specific language over HTTP I see the following technical implementations for the API:
- Query Parameter
Accept-Language
Header- URI path segment (e.g. /en/, /de/)
Accept Header
and various representation types- Cookie
- User preferences stored on the server
The stackoverflow post Multi-lingual REST resources - URL naming suggestions discusses some of the above options.
If the translations are considered a representations of the resource and therefore the recommended Accept-Language
header is used I wonder how can this work with HATEOAS?
I can't add the header to the URI. My understanding is that the client should not construct URIs when HATEOAS is used. But this means that there is no option to use header information, like Accept-Language
or Accept Header
. Is there something I miss or didn't understand correctly?
I wanted to use only the Accept-Language
header, but I only can see two option to support HATEOAS in the JSON response to list the links to the available languages: query parameter and URI path segment.
E.g.
{
"className": "Swing Dancing",
"description": "Learn to dance Lindy Hop for beginners.",
language": "en",
"_links": {
"lang-de": "/classes/5/?lang=de",
"lang-fr": "/classes/5/fr"
}
}
Our application supports multi-language for each supported field of a resource. E.g a class can have a title stored only in one language (e.g. English) and the description in multiple languages. It solely depends on the translations the users of the system have entered. The best match for each field according to the user request is returned. E.g. prefered languages in order "FR, DE, EN", Then the result would look like this:
{
"className": {
"value": "Swing Dancing",
"language": "en"
}
"description": {
"value": "Lerne Lindy Hop für Anfänger.",
"language": "de"
}
}
Since the class name in the example is only stored in English it is returned in English even when English is not a preference of the user.
My proposed implementation is to support the following options in this order:
- Query Parameter
Accept-Language
Header- User preferences stored on the server
- Application default
If a query parameter is provided it is used first. This allowes fetching a specific language e.g. for staff to edit the content. Supporting the query parameter also allows to use HATEOAS in the response.
Next the Accept-Language
is checked. If both contain no information the user preferences are loaded. If the user have no preferences the application default is used.
REST calls should be stateless. I'm unsure about the user preferences. They are not session information and only a constant preference that is fetched using the passed userId. Would that still conform to the REST specification?