Failed to preserve line breaks in google translate response

I have a problem with missing line breaks in translated text from the Google Translate API.

I have a raw query string:

RELATED WORK .

Studies of group work have shown the importance of

      

I did url encoding 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 sending the 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 the answer on one line (no line break):

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

      

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

I just show the url even by simply accessing it through the browser, it doesn't show line breaks in the response.

Any ideas?

+5


source to share


4 answers


The translate api parameter has a parameter format_

that can be set to text

. This will preserve line breaks. See this link for reference.



Update Added underline in parameter format_

.

+8


source


Got the job replacing \r\n

with <br>

in the input string.



+4


source


Replacing \r\n

with <br>

does works, but it seems to be the end of the sentence and therefore limits the length of the translation evaluation, resulting in a less optimal translation. Also the first character of the line becomes a capital letter, which was the key for me.

What I did was replace \r\n

with <code>0</code>

and then go back after translation - this gave a good translation as he did not see him <code>0</code>

contribute to the proposal. Not perfect, but gives a better translation.

+2


source


Low tech workaround:

A simple text program like microsoft write or note pad or libre office writer. Write there and then copy / past to google translate. Line breaks are preserved.

0


source







All Articles