Multilingual REST Resources - URL Tips

Is there a best practice of REST for getting resources in different languages. We currently have

www.mysite.com/books?locale=en 

I know that 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?

+6
source share
3 answers

I think the best way is to implement this as follows:

  • HTTP-Accept-Language Header
  • A language prefix in a URI such as / en / books / ...

In other words, you can adopt a language from both sources. The implementation will be as follows:

  • Check if the Accept-Language header is enabled and save it in a variable;
  • If the URI request starts with / en, / fr or other known language codes (supported by your system) Overwrite the language variable with this new value, separate it with the URI, i.e. if the URI is / en / books , you will get / books .
  • If there is no language, save the default language in a variable, for example, "en"

With this approach, you can make sure that a) route routing will be language agnostic, and your system will work evenly with paths; b) the processing / coordination of languages ​​will be completely separate from your scripts. You can use language information in your scripts without even knowing what the source was and how it was requested.

+6
source

If you are trying to return different translations or localized versions of the same books to the server (in other words, the same resource from the RESTful point of view), use Accept-Language because the resource is the same, but the presentation is different from the client's needs.

However, if you are trying to return completely different books based on the client 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 will be returned. At this stage, you are most likely to talk about the query request. For what it's worth, the /books/en approach sounds reasonable. Another approach would be to add a locale or a language as a resource parameter in GET like /books?lang=en .

+20
source

I agree with the comment of manuel-aldana in the answer to the question RESTful URL: where should I put the locale? example.com/en/page vs example.com/page?locale=en

First check the parameter (e.g. locale = en) to allow the client to explicitly specify the language returning to Accept-Language

0
source

Source: https://habr.com/ru/post/901957/


All Articles