lushan88a / google_trans_new

A free and unlimited python API for google translate.
MIT License
394 stars 171 forks source link

Escaped backslash causing json.decoder.JSONDecodeError #14

Closed njlester closed 3 years ago

njlester commented 3 years ago

A json.decoder.JSONDecodeError exception is raised when I try to detect or translate text containing an escaped backslash:

translator = google_translator() translator.detect(b'\xe7\x9b\xae\\\xe5\xb9\xb4'.decode('utf-8')) json.decoder.JSONDecodeError: Invalid \escape: line 1 column 30 (char 29)

I think it is because of the newline replace in google_trans_new.py on line 210. For example "\n" is replaced in "Mù\\nián" leaving "Mù\ián" which raises the exception.

A workaround is to replace "\\" before translating or detecting.

lushan88a commented 3 years ago

Thanks for you problem. This is exactly what I want to update next :) I thought of replacing some special symbols with placeholders, and then replace them after the translation.

lushan88a commented 3 years ago

I have fixed this problem. You can try it. : )

njlester commented 3 years ago

Thanks for looking into this problem. It seems to be fixed for translating but still raises the following exception for detecting the language: json.decoder.JSONDecodeError: Invalid \escape: line 1 column 30 (char 29)

Also, I noticed one of the backslashes was escaped after I posted my original comment. The actual text causing the problem is: translator.detect(b'\xe7\x9b\xae\\\xe5\xb9\xb4'.decode('utf-8'))

njlester commented 3 years ago

This has now been fixed. Thanks!