4

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

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Fahmi
  • 115
  • 2
  • 7
  • py-googletrans is basically a "scraping" library around the API. Do not use it for production unless you are willing to upgrade the module every time google makes breaking changes, and you can tolerate some downtime (measured in days). – Iñigo González Dec 01 '20 at 17:55
  • @Iñigo this is for production/client sadly, just need the backend to work at least first. what kind of API I need to use for this translation? – Fahmi Dec 01 '20 at 19:13

3 Answers3

1

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'

Moritz
  • 2,835
  • 2
  • 6
  • 12
  • thanks it worked ..somehow but after 2nd times I try again I got ValueError: context must include a "request" key – Fahmi Dec 02 '20 at 18:04
  • cool that it works. don't know where the second error comes from tough – Moritz Dec 02 '20 at 22:40
1

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)']
  • When I run this I get `JSONDecodeError: Extra data: line 1 column 337 (char 336) ` anyone else come across this? – Maverick Apr 01 '22 at 07:18
0

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

It will fix the issue.

garg10may
  • 5,794
  • 11
  • 50
  • 91
Ali
  • 1
  • 2