0

I have referred this answer Python google-trans-new translate raises error: JSONDecodeError: Extra data:

I did the same thing to solve the problem

enter image description here

but the same problem occurs.

here is my code:

from google_trans_new import google_translator

translator = google_translator()  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en') 
print(translate_text)

output:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
Input In [25], in <cell line: 3>()
      1 from google_trans_new import google_translator
      2 translator = google_translator()  
----> 3 translate_text = translator.translate('สวัสดีจีน',lang_tgt='en') 
      4 print(translate_text)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\google_trans_new\google_trans_new.py:188, in google_translator.translate(self, text, lang_tgt, lang_src, pronounce)
    186                         return [sentences, pronounce_src, pronounce_tgt]
    187             except Exception as e:
--> 188                 raise e
    189     r.raise_for_status()
    190 except requests.exceptions.ConnectTimeout as e:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\google_trans_new\google_trans_new.py:152, in google_translator.translate(self, text, lang_tgt, lang_src, pronounce)
    150 try:
    151     response = (decoded_line + ']')
--> 152     response = json.loads(response)
    153     response = list(response)
    154     response = json.loads(response[0][2])

File ~\AppData\Local\Programs\Python\Python310\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    341     s = s.decode(detect_encoding(s), 'surrogatepass')
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:
    348     cls = JSONDecoder

File ~\AppData\Local\Programs\Python\Python310\lib\json\decoder.py:340, in JSONDecoder.decode(self, s, _w)
    338 end = _w(s, end).end()
    339 if end != len(s):
--> 340     raise JSONDecodeError("Extra data", s, end)
    341 return obj

JSONDecodeError: Extra data: line 1 column 368 (char 367)

my environments:

Python 3.10.6
google-trans-new==1.1.9

I just mentioned the above settings in this question but still if more code is required then tell me I'll update my question with that information. Thank you

daylyroppo3
  • 79
  • 1
  • 9
  • Did you check the discussion in the git issue reffered in the other answer? Especially https://github.com/lushan88a/google_trans_new/issues/36#issuecomment-871110918 – buran Sep 01 '22 at 06:29
  • Also the traceback shows you didn't fix the line 151: `151 response = (decoded_line + ']')` – buran Sep 01 '22 at 06:30

1 Answers1

1

I think your problem is about the google translate library. Sometimes google translate libraries give an error like this error. I have a suggestion for the library. It is working successfully.

Try install this library :

pip install googletrans==3.1.0a0

Then I tried a sample code for you :

from googletrans import Translator

translator = Translator()
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en').text

print(translate_text)

and the result : enter image description here

Have good work :)

eminaruk
  • 60
  • 5