Hi all,
I am working in arabizing, SDL based games, to test my code i need to have a
translation, considering that the game using gettext, so I wrote this script
that read en.po, file and take the msgstr and send it to google translate
then consrtuct the ar.po automatically, I find it useful if i share it with
you guys.
Important: do not use this script for production it will help you with the
new files carefully revise the tranlsation after using it, because automatic
translators is really bad.
Best regards,
Ahmed Sayed
#################################################
# Developed By : Ahmed Sayed Hassan
# Email : [EMAIL PROTECTED]
#################################################
import re, string, urllib, urllib2
magic_start = '# # #'
magic_end = '$ $ $'
__languages = { 'english' : 'en',
'french' : 'fr',
'arabic' : 'ar',
}
def clean(text):
return ' '.join(string.replace(text.strip(), "\n", ' ').split())
def translate(phrase, from_lang, to_lang):
phrase = clean(phrase)
try:
from_code = __languages[from_lang.lower()]
to_code = __languages[to_lang.lower()]
except KeyError, lang:
raise Exception(lang + 'Not Available')
url = 'http://translate.google.com/translate_t'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
params = urllib.urlencode( {
'text' : phrase,
'langpair' : from_code + '|' + to_code } )
req = urllib2.Request(url, params, headers)
try:
response = urllib2.urlopen(req)
except IOError, what:
raise Exception("Couldn't talk to server: %s" % what)
except:
print "Unexpected error:", sys.exc_info()[0]
html = response.read()
return clean(html[html.rfind(magic_start) + len(magic_start):html.rfind(magic_end)])
def google_translate(phrase, from_language, through_language):
phrase = clean(phrase)
phrase = magic_start + ' ' + phrase + ' ' + magic_end
phrase = translate(phrase, from_language, through_language)
return phrase
if __name__ == '__main__':
import sys
if len(sys.argv) < 4:
print "Usage: translate <filename> <from_language> <to_language>"
fp = sys.stdout
f = open(sys.argv[1])
all = f.readlines()
for st in all:
if st.find('msgstr') > -1:
st = st[st.find('\"')+1:st.rfind('\"')]
st = google_translate(st, sys.argv[2], sys.argv[3])
fp.write('msgstr ' + '\"'+ st +'\"')
else:
fp.write(st)
fp.close()
_______________________________________________
Developer mailing list
[email protected]
http://lists.arabeyes.org/mailman/listinfo/developer