Is there a REST best practice for GETting resources in different languages. Currently, we have
www.mysite.com/books?locale=en
I know we can use the accept-language header but is it better for us to do
www.mysite.com/books/en or www.mysite.com/books.en
or does it not matter?
Relative vs Absolute. It is strongly recommended that the URLs generated by the API should be absolute URLs. The reason is mostly for ease of use by the client, so that it never has to find out the correct base URI for a resource, which is needed to interpret a relative URL.
The REST API supports Uniform Resource Locators (URLs) with a length of up to 6000 characters. To avoid exceeding this limit, it is important to be aware of URL encoding.
If you're trying to have your server return different translations or localized versions of the same books (in other words, the same resource from a RESTful perspective), then use Accept-Language because the resource is the same but the representation is different based on the client's needs.
However if you're trying to return completely different books based on the client's locale (say, returning books written in French if you know that the user is in France) then the URIs should be different since different resources would be returned. At this point, you're talking more about a query request more than anything else. For what it's worth, the /books/en
approach sounds reasonable. Another approach would be to add the locale or language as a resource parameter to GET as /books?lang=en
.
I think best way would be to implement this following way:
In other words you can accept language from both sources. Implementation will be following:
With this approach you can make sure that a) path routing will be language agnostic and your system will work uniformly with paths; b) language handling/negotiation will be completely separated from your scripts. You can use language information in your scripts without even knowing what was the source and how it was requested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With