1

I am struggling to find a way to input multiple texts in Google Translate API. My setup includes the following things.

My Question : How to add multiple texts to the input. ? Because the following code is not making any sense to me.

data = {'q':'cat', 'q':'dog','source':source,'target':target,'format':'html'}

This is my code.

data = {'q':'This is Text1', 'q':'This is Text2', 'q':'This is Text3', source':source,'target':target,'format':'html'}
_req = urllib.request.Request("https://translation.googleapis.com/language/translate/v2?key="+API_KEY)
_req.add_header('Content-length', len(data))
_req.data = urllib.parse.urlencode(data).encode("utf-8")
response = Connector._get(_req,_session)

Connector._get() is in some other file and it internally calls urllib.request.build_opener with data.

Thanks!

5
  • @Nadan Bhat Can you also show some of of python code you use to build your request to google api ? Commented Mar 3, 2018 at 11:10
  • @Hugo I have updated the question. Commented Mar 3, 2018 at 11:27
  • what solution did you found finally, can you share? Commented Feb 16, 2019 at 14:07
  • 1
    @T.Todua {'q':strings, 'source':source,'target':target,'format':'html'} where strings is a list. ['my text', 'second text'] Commented Feb 18, 2019 at 4:57
  • @NandanBhat ok thnx,. you could have posted that as answer. Commented Feb 18, 2019 at 6:52

2 Answers 2

1

To post multiple parameters (with the same name) in Python for an HTTP request, you can use a list for the values. They'll be added to the URL like q=dog&q=cat.

Example:

headers = { 'content-type': 'application/json; charset=utf-8' }
params = {'q': ['cat', 'dog'],'source':source,'target':target,'format':'html'}
response = requests.post(
            "https://translation.googleapis.com/language/translate/v2?key=",
            headers=headers,
            params=params
        )

Specifically, params = {'q': ['cat', 'dog']} is relevant to your question.

Sign up to request clarification or add additional context in comments.

Comments

1

I do not have tested by myself, but it seems that you should build the data string to give as data argument to your python urllib.request method. So something like data = "{\n \'q\':{}\n \'q\':{} {} etc.".format(qstr,qstr, etc...)

After that you could want to make it more painfull to have several qs. You could make a loop and building your string with += operations.

2 Comments

Yeah ! But what I was thinking, As I have mentioned in the question, in Google Translate API they have showed the implementation. And also it works when I try from REST client (PostMan). I think that shouldn't be that complicated. There must be some easy workaround.
Ok I have tried just to make q a list of str in data dictionnary and printing _req.data is not ugly, hahaha. Maybe try it data = { 'q': [str1,str2,str3] , etc. }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.