1

I am using the Googletrans API for python, which Google translates any given input string with optional specification of target and source languages.

My problem: after using it as a part of a code to process many lines of dialogue transcription line by line, I seem to have broken something that has to do with Json and now the API refuses to run.

An example piece of code, which ran perfectly before but now throws the following error:

from googletrans import Translator
translator = Translator()
translator.translate('안녕하세요.')
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-1-2a9f8e95ca66> in <module>
      1 from googletrans import Translator
      2 translator = Translator()
----> 3 translator.translate('안녕하세요.')

/usr/local/anaconda3/lib/python3.7/site-packages/googletrans/client.py in translate(self, text, dest, src)
    170 
    171         origin = text
--> 172         data = self._translate(text, dest, src)
    173 
    174         # this code will be updated when the format is changed.

/usr/local/anaconda3/lib/python3.7/site-packages/googletrans/client.py in _translate(self, text, dest, src)
     79         r = self.session.get(url, params=params)
     80 
---> 81         data = utils.format_json(r.text)
     82         return data
     83 

/usr/local/anaconda3/lib/python3.7/site-packages/googletrans/utils.py in format_json(original)
     60         converted = json.loads(original)
     61     except ValueError:
---> 62         converted = legacy_format_json(original)
     63 
     64     return converted

/usr/local/anaconda3/lib/python3.7/site-packages/googletrans/utils.py in legacy_format_json(original)
     52             text = text[:p] + states[j][1] + text[nxt:]
     53 
---> 54     converted = json.loads(text)
     55     return converted
     56 

/usr/local/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/usr/local/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/usr/local/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What might have happened and what might be done to fix it?

Rethliopuks
  • 125
  • 6
  • 1
    I just used your code and it worked for me (Python 3.7, googletrans 2.4.0). – Tupteq Feb 19 '20 at 09:44
  • @Tupteq I imagine it must work elsewhere, because it was working for me (Python 3.7, googletrans 2.4.0) until it was somehow broken, and the code here in this question is the first part of the example code in the documentation. – Rethliopuks Feb 19 '20 at 09:45
  • The error messagis typical if it is attempted to decode empty data. – Klaus D. Feb 19 '20 at 09:59

1 Answers1

1

I read the PyPI documentation of googletrans 2.4.0

It mentions the following

The maximum character limit on a single text is 15k.

Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times (so please use this library if you don’t care about stability).

Probably the text you are translating is too long. Thus, throwing the error.

Community
  • 1
  • 1
Abercrombie
  • 1,012
  • 2
  • 13
  • 22
  • I have reinstalled and updated my Python 3.7, reinstalled Googletrans, and restarted Anaconda. My problem now is that it seems unable to translate *anything* and consistently throws the error no matter the input. I wonder if there's something that I need to reset. – Rethliopuks Feb 19 '20 at 09:50
  • Are you getting the same error, or is it a new error? – Abercrombie Feb 19 '20 at 09:51
  • Are you able to translate a smaller sentence? (like the one you provided in the snippet). I am to run the code on my side though. – Abercrombie Feb 19 '20 at 09:53
  • No, nothing would be translated, including the one provided above. It must be able to run because it's the first part of the example code in the documentation. I don't doubt that the code is designed to work, but what I'm not sure is if I've broken something on my computer somehow (and if that's the case, if anything can be done to fix this apparently local issue). – Rethliopuks Feb 19 '20 at 09:56
  • 1
    does this [answer](https://stackoverflow.com/a/57025015/11430726) help? – Abercrombie Feb 19 '20 at 10:04
  • That makes a lot of sense if Google has an IP restriction on their end! I'll try it out once I get back to my computer. Thanks for the link. – Rethliopuks Feb 19 '20 at 10:06
  • Forgot to say -- that was exactly the problem. Thank you! – Rethliopuks Feb 22 '20 at 08:11