Reputation: 67
I am currently working on a translator in Python 3.8.5 but I am not getting an appropriate result. My code thus far is
from googletrans import Translator
text = '''Vous êtes français'''
translator = Translator()
dt = translator.detect(text)
print(dt)
In my terminal, I should be getting the source and the destination
which is fr for Source and en for dt
but instead what I am getting is this error I have no idea how to fix.
Traceback (most recent call last):
File "c:/Users/Akash/Desktop/Learning python/Project10-MegaProject-Translator.py", line 5, in <module>
dt = translator.detect(text)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\client.py", line 255, in detect
data = self._translate(text, 'en', 'auto', kwargs)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\gtoken.py", line 194, in do
self._update()
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googletrans\gtoken.py", line 54, in _update
r = self.client.get(self.host)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 755, in get
return self.request(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 600, in request
return self.send(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 620, in send
response = self.send_handling_redirects(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 647, in send_handling_redirects
response = self.send_handling_auth(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth
response = self.send_single_request(request, timeout)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpx\_client.py", line 714, in send_single_request
) = transport.request(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection_pool.py", line 152, in request
response = connection.request(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection.py", line 65, in request
self.socket = self._open_socket(timeout)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_sync\connection.py", line 85, in _open_socket
return self.backend.open_tcp_stream(
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_backends\sync.py", line 139, in open_tcp_stream
return SyncSocketStream(sock=sock)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\Akash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions
raise to_exc(exc) from None
httpcore._exceptions.ConnectTimeout: _ssl.c:1106: The handshake operation timed out
PS C:\Users\Akash\Desktop\Learning python>
Upvotes: 4
Views: 12385
Reputation: 7287
The googletrans API that you are using is the same service as translate.google.com. Unfortunately if you send to much requests using googletrans, your IP will be blocked by google. As this service is not intended to translate a bulk of data. Also in in the documentation of googletrans API it mentioned the following problems.
Note on library usage:
- 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.)
- If you want to use a stable API, I highly recommend you to use Google’s official translate API.
- If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
I suggest to use the official Translate API for you to utilize the full potential of the API.
Upvotes: 3