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 :)