Giter Site home page Giter Site logo

google_trans_new's People

Contributors

aaryanverma avatar cergo666 avatar falconeagletest avatar fawazahmed0 avatar lushan88a avatar sayonari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

google_trans_new's Issues

PyPi release is outdated which causes JSON decode error on Google Colab

Google trans new does not work in Google Colab when installed through pypi

!pip install google_trans_new

input:

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

error:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-7-a6eb592f82a8> in <module>()
      2 
      3 translator = google_translator()
----> 4 translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')
      5 print(translate_text)
      6 

3 frames
/usr/local/lib/python3.7/dist-packages/google_trans_new/google_trans_new.py in 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:

/usr/local/lib/python3.7/dist-packages/google_trans_new/google_trans_new.py in 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])

/usr/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/lib/python3.7/json/decoder.py in 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
    342 

JSONDecodeError: Extra data: line 1 column 339 (char 338)

I think the package is not updated on pypi, the latest release dates from dec 2020, while your latest commit is from 17 days ago.

This can be temporarily solved by doing

!git clone https://github.com/lushan88a/google_trans_new.git
from google_trans_new.google_trans_new import google_translator  
  
translator = google_translator(url_suffix='nl',timeout=9)  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')  
print(translate_text)
>>> Hello Chinese

A new pypi release might fix all the issues

Spanish translation

Hi!

I was trying to get Spanish translations of English words. For example:
translator = google_translator()
translator.translate('children',lang_tgt='es')

But I got a TypeError: 'NoneType' object is not iterable

I manually checked the ones that didn't work. It seems that all of their Spanish translations have two forms, a feminine form and a masculine form.

If that is the reason, is it possible to return both forms?

Thanks!

Report an error when the translated content is a url link

When I am using the code, url_suffix="com"
Translated content: https://www.hkcnews.com
It will pop up the following error:
`
Traceback (most recent call last):
File "C:\Users\xxx\Envs\xxx_venv\lib\site-packages\google_trans_new\google_trans_new.py", line 153, in translate
pronounce_src = (response[0][0])
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:
`

is this really free and unlimited?

Can i make unlimited requests for getting translated text and won't be blocked by google?

how is this different from googletrans 3.0.0 package?

google trans_new has suddenly stopped working about 10 days ago..

google trans_new has suddenly stopped working about 10 days ago..
Driving me crazy!!!

def translate1(translatethis,langdest):

translator = google_translator()
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')
print (' in new translate routine***' )
print ( translate_text)
translate_text = translator.translate('muy bien1',lang_tgt='en')
print ( translate_text)
vr_response = 'muy bien2'
target_language = 'en'
translate_text   = translator.translate(translatethis,lang_tgt=langdest)
print ( translate_text)
return ()

output:
in new translate routine***
2021-07-09 11:34:45 ERROR
2021-07-09 11:34:45 ERROR
2021-07-09 11:34:45 ERROR

Consider adding a simple example at https://github.com/lushan88a/google_trans_new#translate

For example something like this:

from google_trans_new import google_translator  
  
translator = google_translator()  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en',lang_src='th' )  
# <Translate text=สวัสดีจีน lang_tgt=th lang_src=en>  
#  default parameter : lang_src=auto lang_tgt=auto 
#  API can automatically identify the src translation language, so you don’t need to set lang_src
print(translate_text)

Why am I saying this?
The user will first copy and paste the code in python IDE,to see if it runs fine , the example will break because of proxies.

You can specify url_suffix ,proxies etc example after basic example.

Thanks

cannot import name '__title__' from 'requests.__version__

Hi,
I installed google_trans_new, six, requests with pip3, my python version is 3.7.7
but I get the following error:

Traceback (most recent call last):
File "/Users/toraj/Projects/GitProjects/python/translate/app.py", line 3, in
from google_trans_new import google_translator
File "/Users/toraj/Projects/GitProjects/python/translate/venv/lib/python3.7/site-packages/google_trans_new/init.py", line 7, in
from .google_trans_new import google_translator
File "/Users/toraj/Projects/GitProjects/python/translate/venv/lib/python3.7/site-packages/google_trans_new/google_trans_new.py", line 4, in
import json, requests, random, re
File "/Users/toraj/Projects/GitProjects/python/translate/venv/lib/python3.7/site-packages/requests/init.py", line 129, in
from .version import title, description, url, version
ImportError: cannot import name 'title' from 'requests.version' (/Users/toraj/Projects/GitProjects/python/translate/venv/lib/python3.7/site-packages/requests/version.py)

google_new_transError: 404 (Not Found) from TTS API. Probable cause: Unknown

test code:

from google_trans_new import google_translator

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

======================================
error:

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

google_trans_new.google_trans_new.google_new_transError: 404 (Not Found) from TTS API. Probable cause: Unknown

Escaped backslash causing json.decoder.JSONDecodeError

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.

requests.exceptions.ConnectTimeout

hello,
i get a problem.
When i use the library,
the error:

requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='translate.google.cn', port=443): Max retries exceeded with url:

support for proxies?

is there proxy support for this library as Google may block for too many requests?

the translator doesn't work. Try to solve it and record it

install

pip3 install google-trans-new

translate

root@xxx:/opt# python3 google_translate_util.py 
Traceback (most recent call last):
  File "google_translate_util.py", line 29, in <module>
    print(translate(text, "zh"))
  File "google_translate_util.py", line 19, in translate
    return translator.translate(text, lang_tgt=lang_type)
  File "/usr/local/lib/python3.6/dist-packages/google_trans_new/google_trans_new.py", line 188, in translate
    raise e
  File "/usr/local/lib/python3.6/dist-packages/google_trans_new/google_trans_new.py", line 152, in translate
    response = json.loads(response)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3085 (char 3084)

modify

/usr/local/lib/python3.6/dist-packages/google_trans_new/google_trans_new.py` line 151

response = (decoded_line + ']') modify to response = (decoded_line)

Test

root@xxx:/opt# python3 google_translate_util.py 
工具

Typo in code in Readme

Hi I think that there is a typo in the code in the readme section:

In the Pronounce section:
Pronounce = translator.transalate('สวัสดีจีน',lang_src='th',lang_tgt='zh',pronounce=True)

It should be translator.translate(..)

src always 'en'

Hi, I wrote this little script for a bigger one and I'm already blocked because when I try to translate something the source is always English so translate() do not translate my string even if I change manually the source and destination.

My script:

from googletrans import Translator
translator = Translator()
print(translator.detect("Ceci est un essai"))
Detected(lang=en, confidence=1) # This is in French not in English...
print(translator.translate("Ceci est un essai"))
Translated(src=en, dest=en, text=Ceci est un essai, pronunciation=Ceci est un essai, extra_data="{'translat...") # It does not translate it...

If someone can help me I would be really grateful!
Thanks.

Warning: Can only detect less than 5000 characters

In general, when working with Google translation services, I prefer to let Google detect the source language. So my original pull looked like this:

postTextTrans = translator.translate(postText, lang_tgt='en')

This works great, except when the source text exceeds 5000 characters (and I started seeing the subject error).

So I changed my call to this:

postTextTrans = translator.translate(postText, lang_src='pt', lang_tgt='en')

It seems like specifying the source language should bypass the detection step and avoid the error but I'm still getting the same error. Which probably means that either the error is not really indicating it's complaint, or the lang_src parameter isn't working properly.

Has anyone else run into this? Or found a solution?

I'm probably going to work on implementing some code to break the text into smaller chunks and then reassemble the results, but would prefer not to have to do that.

No real way to translate a list

While I tried several method of separating lines, no matter what I do, whether it be \n, , \n\n\n\n, many sentences gets merged and thus unable to truly distinguish the translation

python 3.9 object NoneType can't be used in 'await' expression

I am new to programming, so please take this into account

Traceback:
Google_translator.py", line 6, in translator
text = await g.translate(f"{text}", locale)
File "C:\Python39\lib\site-packages\async_google_trans_new\main.py", line 241, in translate
await s.close()
TypeError: object NoneType can't be used in 'await' expression

code:
**
from aiogram.types.base import String
import async_google_trans_new

async def translator(text: String, locale: String):
g = async_google_trans_new.AsyncTranslator()
text = await g.translate(f"{text}", locale)
return text
**

I use a library function call from the asynchronous code of the telegram bot, need translation.

Is there a way to get the variations of a translated sentence/word?

I have been translating words and small phrases into several languages. But sometimes the given translation is not the best translation, but a more appropriate one is in the variations provided in google translate. Is there a way to get these variations? Specially with part of speech?

Example:
When translating the word slash from English -> Spanish. It translates as barra obliqua, but in my case, I prefer the result as like acuchillar or atacar. I'd like to receive all possible translations.

Thanks for the time

return ERROR string on some translation

Hi ! Thank you for your great work, I can't deny the fact that this is so useful on the project I am trying on :) I highly recommend this work of yours.

I just have encounter this return ERROR string, I hope to resolve or pin point what might be wrong while translating the text

I'm actually running 38K+ translation in 4 threads'

google_translator(timeout=10,proxies={'http':'159.203.61.169:8080'})

lang_src='zh' to lang_tgt='en'

text: 這是……<size=24>欸欸欸——??</size>\n難道你想和我<color=#CA2E0AFF>在噴泉前留下紀念照</color>嗎?這主意太棒了,如此就能帶走最喜愛的風景了!我先去噴泉前面等你~快,快!

result : ERROR

EDIT:

Upon checking I think it's the \n that cause the error.

work around : just put some wrapper in it to be able to pass the translation

Need for pronunciation

I used to use the pronunciation feature a lot in the previous library but now that the library is redundant, I came here thanks to a comment. The API works fine but is there any way for get the pronunciation.

TypeError: replace() takes at least 2 arguments (1 given)

Hi, I tried the library but got this error:

Traceback (most recent call last):
File "translate_to_english.py", line 128, in
main()
File "translate_to_english.py", line 114, in main
result = translator.translate(str)
File "translate_to_english.py", line 44, in translate
translate_text = translator.translate(str, lang_src='sq', lang_tgt='en')
File "C:\Users\Win10\Anaconda3\lib\site-packages\google_trans_new\google_trans_new.py", line 117, in translate
text = str(text.replace('\n',"").replace('\t','').replace('\"',''))
TypeError: replace() takes at least 2 arguments (1 given)

My code is this:

> import google_trans_new 
> class TranslatorWrapper():
>     def __init__(self):
>         # self.translator = googletrans.Translator()
>         self.translator = google_trans_new.google_translator()      
>    
>     def translate(self, text):
>         translator = self.translator
>         # translated = translator.translate(str, src='sq', dest='en')        
>         # return translated.text
>         translate_text = translator.translate(str, lang_src='sq', lang_tgt='en')
>         return translate_text
> translator = TranslatorWrapper()
> ....
> result = translator.translate(str)

I created the wrapper class because I changed the code from googletrans to this library. Please help me, thank you very much.

Too Many Requests

Hi, i am getting this message:

429 (Too Many Requests) from TTS API. Probable cause: Unknown

How can avoid this? Do I need to wait? How long? or Do I need to do something else?
Many thanks.

error

It says not group match blabla

sample code has error

from google_trans_new import google_translator

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


error : "name": "JSONDecodeError",
"message": "Extra data: line 1 column 375 (char 374)",
"stack": "\u001b[0;31m

Python: google_trans_new - does not translate as correctly as https://translate.google.com/

hello, I use the library of google_trans_new and other google-translate libraries, such as googletrans or PYGOOGLETRANSLATION to translate multiple webpages.

from google_trans_new import Translator

translator = Translator()

The problem is that this library does not translate as correctly as https://translate.google.com/

For example, this line, that I translated from english to belarusian:

Show the superiority of your conscience, by removing the destructive influences of your own feelings' oscillations.

With https://translate.google.com/ the translation is correct:

Пакажыце перавагу свайго сумлення, выдаючы разбураныя павышаныя ваганні ўласных пачуццяў.

but using google_trans_new library, I get a pretty bad translation:

Пакажыце перавагу выкарыстання сум, шляхам выдачы разбуральных уздзеянняў вашых уласных пачуццяў вагаў.

I don't know what can be the problem??? Maybe you must update something... ?

How to deal with multiple returned translations? How to get 1st. translatin only?

Sometimes the translator returns a list sometimes just a text.
Steps to reproduce:

from google_trans_new import google_translator
translator = google_translator()

print (translator.translate('ALL', lang_tgt='fr'))
produces: ['TOUT', 'TOUTE']

while:
print (translator.translate('In Progress', lang_tgt='fr'))
produces: "En cours"

Is there a way for more consistend handling (e.g. by default allways return 1st and have alternative translations only on demand)?

Thx & best regards,

Ben

Text language detected as 'rw', but that's not a valid language?

Hi there,

I'm encountering a KeyError where it looks like translator.detect() is returning 'rw' as the language of my text but then that's not a valid key in LANGUAGES.

The text that it is failing on is:
"[image: Inline image 1]\n\n2017-06-25 0:47 GMT-04:00 Mariana Matranga :"

This is the error message:

` KeyError Traceback (most recent call last)
in
----> 1 lang, translation = lang_detect(newbody)

in lang_detect(text)
3 translation = ''
4
----> 5 lang = translator.detect(text)[0]
6
7 if lang != 'en':

~\Anaconda3\lib\site-packages\google_trans_new\google_trans_new.py in detect(self, text)
240 raise Exception
241 # data_got = data_got.split('\"]')[0]
--> 242 return [detect_lang, LANGUAGES[detect_lang.lower()]]
243 r.raise_for_status()
244 except requests.exceptions.HTTPError as e:

KeyError: 'rw' `

How do i translate a list?

If i do translator.translate(item,lang_tgt='en') for every item in my list, I guess i may soon reach the quota of google translate.

HTTPSConnectionPool(host='translate.google.cn', port=443)

it goes well for a few days before. at this moment, it suddenly gets this error, and the error comes over always, besides, I set random choose proxies(about 200 diffrent ip)
HTTPSConnectionPool(host='translate.google.cn', port=443): Max retries exceeded with url: /_/TranslateWebserverUi/data/batchexecute (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa880f52860>, 'Connection to translate.google.cn timed out. (connect timeout=5)'))

google trans new not working, causing jsonDecodeError

it was working around one month ago. but suddenly it is showing json loading error


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

~\anaconda3\lib\site-packages\google_trans_new\google_trans_new.py in 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:

~\anaconda3\lib\site-packages\google_trans_new\google_trans_new.py in 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])

~\anaconda3\lib\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

~\anaconda3\lib\json\decoder.py in 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
342

JSONDecodeError: Extra data: line 1 column 343 (char 342)

Useless space in translation

This is not a huge problem, but has anybody else noticed the useless space at the end of the string returned by calls to translator.translate(text, lang_tgt = translated_to)?

space issue github

"at the back :", rather than "at the back:"

I'm sure that if I looked through the source code I could resolve it myself, but I'm feeling lazy...

How to set translation model

Hi

In google translate API it is possible to specify translation model and set it to NMT (neural mt) or PBT(phrase based MT). Is it possible to set translation model in this API?

Thanks

is it unlimited characters supported?

Hi, is this library is forked from googletrans? ....
I am asking because that library sometimes does not support bulk requests from same IP.
I want to know if this library supports bulk queries.

Bug on google_trans_new.py - google_translator.translate

Hi!

I've been experiencing the following error:
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 340, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 499 (char 498)

raised by:
response = (decoded_line + ']')
response = json.loads(response)

within the translate function of google_translator class.

I found a solution that works for me that is skipping the first line where the error occurs and adds a ']' and directly go with the decoded_line so that:
`[...]

if "MkEWBc" in decoded_line:
try:
response = json.loads(decoded_line)
response = list(response)
response = json.loads(response[0][2])
response_ = list(response)
response = response_[1][0]

[...]`

Is this solution for the bug valid?

Many thanks!

Language names & lang_tgt + lang_src

Hello,
Your library is awesome 😄 I'd like to submit a request or bounce an idea off of you. Currently if you input the full language name into lang_tgt then it will not translate as it seems to not check or validate the lang_tgt input at all.

I see there is a dictionary named LANGUAGES in constants.py, could you bounce lang_tgt off of there to get a full language name's short language code and if it isnt the correct language return incorrect language? Also, it doesnt seem as if lang_src is used as even if I set the lang_src to something incorrect it translates correctly which thus means it is always using auto and auto is not always so reliable...

Translation Limit per day and usage of Proxy with request

Is there any limit per day/week/month for using this API? Because at google_trans there is a way to specify proxy wich is not possible here it seems.

Actually in a nutshell , need to know , whether this will get blocked by Google , if I put around 10k-20k strings (containing 3-5 words) per day.

If this new one handles proxies internally , happy to use this :).

this does not work at all, JSONDecodeError when parse the response

here is the error

translator = google_translator()
translator.translate("test", lang_tgt="zh")
 File "/lib/python3.8/site-packages/google_trans_new/google_trans_new.py", line 188, in translate
    raise e
  File "/lib/python3.8/site-packages/google_trans_new/google_trans_new.py", line 152, in translate
    response = json.loads(response)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2384 (char 2383)

google_new_transError: Failed to connect. Probable cause: timeout

Hi @lushan88a , google_trans_new is great. I've used it a few days ago. I tried today but I have an error.

My purpose is to translate from id to en

from google_trans_new import google_translator

translator = google_translator()

df["desc"] = df["uraian"].apply(translator.translate, lang_tgt="en")
df

The error
image
image
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.