5

I've just began building a multilingual REST API and are unsure on if there's any convention to follow regarding how I should integrate the multilinguality properly.

Below is a list of alternatives I have come up with, not knowing which makes most sense.

Option 1:
Language-variable in URI: http://myapi.com/en/users/john

Option 2:
Returning only error codes for translation client side: GET http://myapi.com/users/john => HTTP 404 {status: false, error_code: "321"}

Option 3:
Returning in all available languages: GET http://myapi.com/users/john => {status: false, error_en: "User not found", error_sv: "Anvandaren finns inte"}

Industrial
  • 41,400
  • 69
  • 194
  • 289
  • Please describe what 'the multilinguality properly' is in your domain. Can there be different users `john` for different languages? –  Nov 06 '12 at 11:17
  • No, Data is the same for all languages along with resource names. Only human-readable strings should change – Industrial Nov 06 '12 at 12:15

1 Answers1

10

For content negotiation as for negotiating the natural language of a representation, HTTP provides the request header Accept-Language:

Accept-Language: da, en-gb;q=0.8, en;q=0.7

If possible, the server replies to this request with a response header Content-Language:

Content-Language: da

Only if the resources are different resources for different languages, the language should be part of the URI. If not, content negotiation should be used.

  • In this case, content negotiation would be used to negotiate the language of the human-readable strings in the response only. Non-strings, or strings not intended for people (such as object keys) would not be translated. – Donal Fellows Nov 06 '12 at 11:48
  • 1
    @DonalFellows Yes. From the question I am not sure if this is what is meant by 'a multilingual REST API'. But when in doubt, use the standard :) –  Nov 06 '12 at 11:50