line-breaksgoogle-translate

Unable to preserve line breaks in Google Translate response


I have a problem of not having line breaks in the translated text from Google Translate API.

I have a raw query string like this:

RELATED WORK .

Studies of group work have shown the importance of

I did a URL encode for the query string and it shows this:

RELATED%20WORK%20.%0D%0A%0D%0AStudies%20of%20group%20work%20have%20shown%20the%20importance%20of

The problem is when being submitted to Google Translate API:

https://www.googleapis.com/language/translate/v2?key=<key>&source=en&target=ja&q=RELATED%20WORK%20.%0D%0A%0D%0AStudies%20of%20group%20work%20have%20shown%20the%20importance%20of

I only get a response in one line (no line breaks):

{
    "data": {
       "translations": [
          {
            "translatedText": "関連作業 。グループワークの研究は、"
          }
       ]
    }
 }

My ultimate goal is to parse the translated text line by line for proper rendering.

I'm just showing the URL for even by just accessing it via browser, it doesn't show the line breaks in the response.

Any ideas?


Solution

  • The translate api has a parameter format_ which you can set to text. This will preserve line breaks. See this link for reference.

    Update Added underscore in format_ parameter.