Hello all,

I have been developing a code based on Google Translate Python Wrapper. I have done it all, perfectly, except for one thing, I will explain it by pointing to code:

there is the defined global languages list, and there are two main functions (Translate(), and UserSubmitted() )

now the languages list is on the form of:

_languages = {'en':'English', 'fr':'French', etc...}

The translate function ( which can be found on the web) is like this (AND PLEASE FOCUS ON THE ARGUMENT WHERE IT SAYS to='en'):

def translate(text,src='', to='en'):

    params = ({'langpair': '%s|%s' % (src, to),
             'v': '1.0'
             })
    retText=''
    for text in getSplits(text):
            params['q'] = text
            resp = simplejson.load(urllib.urlopen('%s' % (baseUrl), data = urllib.urlencode(params)))
            try:
                    retText += resp['responseData']['translatedText']
            except:
                    raise
    return retText

The UserSubmitted() function simply takes the user's input, and enters a conditional statement where, if the input equals to any language in the _languages, it prints a translated welcome message in the chosen language. DONE

If the input equals to = '/?', it prints some help hints. DONE


Else, the program prints the exact user's input.

as follows:

if text in _languages:
         msg = "You have choosen the " + _languages[text]
         blip.CreateChild().GetDocument().SetText(translate(msg, '', text))
         Templng=text
    elif CMD_HELP in text:
         blip.CreateChild().GetDocument().SetText('''Please Enter the Language Code (Example: 'en' for English).BLA BLA BLA''')
    else:
         doc.AppendText( '\n' + translate(text, '' ,Templng))

NOW, forget about the blip, doc, etc.. because they are for Gwave.

the Templng is a global variable defined as

Templng = ''

which I assumed, it will be filled ONCE when the user input some language code (i.e. en) and will be used forever UNTIL the user inputs a new language code.

My frustrating problem is that the program works perfectly, it takes the language code first time, example, fr, and it prints the welcome message in French. And the program takes the '/?' and prints the help hints. But when I try to write normally, it translates nothing (Although I already entered the language code before).

And I have tried to change the argument I pointed before to '', or null but never changed a thing.


Please code you help in this because it is driving me crazy
What Am I doing wrong here ?? What should I do to keep the value of Templng fixed all the time until the next change? or what should I do in general ???


Thanks and sorry for the long post :)

Any one please ?

It's hard to tell whether you did this or not since you didn't give us all your code, but I'm assuming that before assigning to Templng you used global Templng to let the local scope know you meant to use the global variable and not just create a new local one?

Or maybe that's the whole problem.

It's hard to tell whether you did this or not since you didn't give us all your code, but I'm assuming that before assigning to Templng you used global Templng to let the local scope know you meant to use the global variable and not just create a new local one?

Or maybe that's the whole problem.

Thanks for the reply jlm, sorry I can't provide you the whole code at the moment.

As for your question, yes this is what I meant, however, I have done a small version of the whole code as a sample to see if I can assign the global variable with a value from the local function scope, and it worked.

Then I read somewhere that using a class and defining a variable in the class instead of using global variable could work, it worked for the sample code but never for my code. This has led me that there must be something to be done in the google's translate code, the function "translate", I mean the argument to ...

So you want to use the value of Templng for the default value of the to parameter in the translate function?

So you want to use the value of Templng for the default value of the to parameter in the translate function?

Yes, sort of "overwriting" the to value each time the user enters a new language code.

Because this is what I get at the moment:

User: fr
Robot: Vous avez choisi le français (which means you have chosen French)
User: Test
and then nothing is translated to french.

if the user again, chooses another language, the robot will greet him in the language he has chosen, but again, nothing after that.

Which I assume, the first if statement doesn't change the templang variable (nor the to) argument.

What do you think I could do to solve this ?

thanks for your replies

Problem solved in a strange way:

I put the global variable in another file, imported the file and bingo, it worked !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.