Googletrans API AttributeError

Question:

I keep getting "AttributeError: ‘NoneType’ object has no attribute ‘group’ " error even after changing gtoken on googletrans stopped working with error 'NoneType' object has no attribute 'group' but I got __init__() got an unexpected keyword argument 'client' error instead

here my main.py

from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn
from googletrans import Translator
#init
app = FastAPI(debug=True)

templates = Jinja2Templates(directory="template")


#route
@app.get('/')
def home(request: Request):
    text = request.get('text')
    lang = request.get('lang')
    #print('text:',text,'lang:',lang)

    #connect the translator
    translator=Translator()

    #detect langguage
    dt = translator.detect(text)
    dt2 =dt.lang

    #translate the text
    translated = translator.translate(text, lang)
    tr =translated.text

    return templates.TemplateResponse({"request": request},"translates.html",{'translated':tr,'u_lang':dt2,'t_lang':lang})

#def translator(request):


if __name__=="__main__":
    uvicorn.run(app,host="127.0.0.1",port=8000)

and here where my translate.html execute the translate within site

<form action="" method="get">
    <br>

<div class="form-input">
    <center><label for="TextareaInput">Enter Text </label></center>
    <center><textarea class="form-control" value="text" id="TextareaInput" rows="3"></textarea></center>
</div>
<div class="ui divider"></div>
<div class="form-selection">
  <center><label for="languages">Choose Langguage:</label></center>
  <center><select name="trans" id="languages">
    <option value="en">English</option>
    <option value="ms">Malay</option>
    <option value="zh-cn">Mandarin</option>
    <option value="ko">Korean</option>
      <option value="ja">Japanese</option>
      <option value="vi">Vietnamese</option>
      <option value="th">Thailand</option>
  </select></center>
</div>
<div class="ui divider"></div>
<div>
   <center> <button class="ui button">Translate</button></center>
</div>
<div class="ui divider"></div>
<div class="form-output">
    <div class="container">
        <br><br>
        <h1>Text succes translated {{u_lang}} to {{t_lang}}</h1>
        <center>
            <h1>{{translated}}</h1>
        </center>
    </div>
</div>
</form>

I already hit wall because this error keep popping

Asked By: Fahmi

||

Answers:

A new alpha version with a fix was released a few minutes ago.

Install the alpha version like this:

pip install googletrans==3.1.0a0

Important thing to note:
You have to specify a service url, otherwise the same error still occurs. So this should work:

from googletrans import Translator
translator = Translator(service_urls=['translate.googleapis.com'])
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

But his still returns the error (at least for me):

translator = Translator()
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

See the discussion here for details and updates: https://github.com/ssut/py-googletrans/pull/237

See also this discussion: googletrans stopped working with error 'NoneType' object has no attribute 'group'

Answered By: Moritz

I encountered the same issue until I discovered the module google_trans_new. You should try it:

pip install google_trans_new

For the translation part:

from google_trans_new import google_translator  
  
translator = google_translator()  
translate_text = translator.translate('首先感谢我的父母他们对我的关爱',lang_tgt='en')  

which return:

'First of all thank my parents for their love'

For detection:

from google_trans_new import google_translator  
  
detector = google_translator()  
detect_result = detector.detect('首先感谢我的父母他们对我的关爱')

which gives

['zh-CN', 'chinese (simplified)']

Uninstall your googletrans and then install the updated version:
pip install googletrans==3.1.0a0

It will fix the issue.

Answered By: Ali
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.