Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 411
Joseph D. Avatar asked Jun 08 '17 20:06

Joseph D.


People also ask

What is the problem with Google Translate?

Google Translate often produces translations that contain significant grammatical errors. This is due to the fact that Google's translation system uses a method based on language pair frequency that does not take into account grammatical rules. Google Translate does not have a system to correct for translation errors.

How do I save a Google Translate phrase?

Once you've entered your phrase and received a translation you like, hit the star icon ⭐ at the bottom of the translation window and it is automatically saved to Phrasebook. To view your Phrasebook, just click on the little notebook icon 📓in the top right corner of Google Translate.

Is Google Translate being discontinued?

You may have received a notice from Google (pictured below) that its very popular Google Translator Toolkit has been discontinued, effective December 4th, 2019.


3 Answers

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.

like image 113
Daniel Mecke Avatar answered Oct 27 '22 12:10

Daniel Mecke


Got it working by replacing \r\n with <br> in the input string.

like image 27
Joseph D. Avatar answered Oct 27 '22 13:10

Joseph D.


Replacing \r\n with <br> does work, but it seems to think that its the end of a sentence, and so limits the stretch of the translation evaluation, resulting in a less-than-optimal translation. Also the first character of the line becomes a capital letter, which is what was the clue for me.

What I did was to replace \r\n with <code>0</code> and then back again after translation - this gave a good translation, as it did not see the <code>0</code> as contributing to the sentence. Not ideal, but gives a better translation.

like image 44
Hendrik Avatar answered Oct 27 '22 12:10

Hendrik