nidhaloff / deep-translator

A flexible free and unlimited python tool to translate between different languages in a simple way using multiple translators.
https://deep-translator.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
1.55k stars 177 forks source link

Exception when translate a string with digit #216

Closed vgdh closed 1 year ago

vgdh commented 1 year ago

Description

I pass a string that contain only one number and got this exception.

What I Did

from deep_translator import GoogleTranslator
tr = GoogleTranslator(source="auto", target="german").translate("1")
Exception has occurred: NotValidPayload       (note: full exception trace is shown but execution is paused at: <module>)
1 --> text must be a valid text with maximum 5000 character,otherwise it cannot be translated
  File "/root/pyproj/src/translate_Google.py", line 6, in <module> (Current frame)
    tr = GoogleTranslator(source="auto", target="german").translate("1")
deep_translator.exceptions.NotValidPayload: 1 --> text must be a valid text with maximum 5000 character,otherwise it cannot be translated
Vincent-Stragier commented 1 year ago

Hi @vgdh,

A priori, the issue comes from this module. It raises an exception if the text is not a string or the string is a digit. See:

https://github.com/nidhaloff/deep-translator/blob/4328b37c0a03bc29ba6ed61ff5b1e8082373c0a8/deep_translator/validate.py#L12-L28

I don't know if it is on purpose or a bug… To fix this specific problem, you could change the condition on line 23 from if not isinstance(text, str) or text.isdigit(): to if not (isinstance(text, str) or text.isdigit()): or if not isinstance(text, str) or not text.isdigit(): or simply if not isinstance(text, str):.

Or catch the exception before it occurs and automatically return the digit… well, it is probably not the best solution for languages that do not use Arabic figures.

dinhanhx commented 1 year ago

I think deep-translator should take digits as inputs. Let have a look at google, image image

@vgdh And yes, I also encounter the exact exception.

dinhanhx commented 1 year ago

num2words can be used to address the problem.

nidhaloff commented 1 year ago

@vgdh I think you have a good point there. When I created the module, I thought that it wouldn't make sense to pass a digit as input, but yes I will update this one. At least for the google translator. Thanks for the input