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?
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.
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.
You may have received a notice from Google (pictured below) that its very popular Google Translator Toolkit has been discontinued, effective December 4th, 2019.
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.
Got it working by replacing \r\n
with <br>
in the input string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With