Search code examples
jsonapi-designgoogle-translate

What is the advantage of using the below input format in Google Translate API


The Google Translate API right now takes multiple inputs to be translated as below:

--data "{'q': 'Hello world','q': 'My name is Jeff','target': 'de'}"

Instead of using multiple entries like above for 'q' they could have use array like below:

 --data "{'q':['Hello world','My name is Jeff'],'target': 'de'}"

What is the advantage of the 1st format?


Solution

  • I have tested both versions and I got the same result, with the messages translated in German.

    Try this code:

    curl -s -X POST -H "Content-Type: application/json" \   
        -H "Authorization: Bearer "$(gcloud auth print-access-token) \ 
        --data "{'q':['Hello world','My name is Jeff'],'target': 'de'}" \
    "https://translation.googleapis.com/language/translate/v2"
    

    You will get this response:

    {
      "data": {
        "translations": [
          {
            "translatedText": "Hallo Welt",
            "detectedSourceLanguage": "en"
          },
          {
            "translatedText": "Mein Name ist Jeff",
            "detectedSourceLanguage": "en"
          }
        ]
      }
    }
    

    You may need authentication credentials for this to work, follow the steps here to add those credentials and then test the code above.