def gengo_tm(text, sl, tl, tier='standard', auto_approve=True, public_key='', private_key='', sandbox = False, comment='', machine=True, ttl=300, callback_url=''): tt = cacheGet('/gengo/' + sl + '/' + tl + '/' + text) if tt is not None: return tt # # I didn't want to make this utility too App Engine specific, however, this is a place where the task queues come in handy. # You can use taskqueue.add(url=worker_url, params=p) to initiate a background task that queries the Gengo API and requests # a callback when the translation is done. The request handler that processes the callback would, in turn, write to the cache # so subsequent cacheGet() calls would return the now completed translation. I'll add this option in an update in the next week # or so, but am sticking with synchronous polling for now (its simple and doesn't depend on an App Engine specific resources). # if auto_approve: auto_approve = 1 else: auto_approve = 0 if machine: machine = 1 else: machine = 0 #try: gengo = MyGengo( public_key = public_key, private_key = private_key, sandbox = sandbox, # possibly false, depending on your dev needs ) if string.count(callback_url, 'http://') < 1 and string.count(callback_url, 'https://') < 1: response = gengo.postTranslationJob(job = { 'type': 'text', # REQUIRED. Type to translate, you'll probably always put 'text' here. ;P 'slug': 'Single :: Text Translation', # REQUIRED. Slug for internally storing, can be generic. 'body_src': text, # REQUIRED. The text you're translating. ;P 'lc_src': sl, # REQUIRED. source_language_code (see getServiceLanguages() for a list of codes) 'lc_tgt': tl, # REQUIRED. target_language_code (see getServiceLanguages() for a list of codes) 'tier': tier, # REQUIRED. tier type ("machine", "standard", "pro", or "ultra") 'machine': machine, # OPTIONAL. allow machine translation 'auto_approve': 1, # OPTIONAL. Hopefully self explanatory (1 = yes, 0 = no), 'comment': comment, # OPTIONAL. Comment to leave for translator. #'callback_url': '', # OPTIONAL. Callback URL that updates are sent to. # 'custom_data': 'your optional custom data, limited to 1kb.' # OPTIONAL }) else: response = gengo.postTranslationJob(job = { 'type': 'text', # REQUIRED. Type to translate, you'll probably always put 'text' here. ;P 'slug': 'Single :: Text Translation', # REQUIRED. Slug for internally storing, can be generic. 'body_src': text, # REQUIRED. The text you're translating. ;P 'lc_src': sl, # REQUIRED. source_language_code (see getServiceLanguages() for a list of codes) 'lc_tgt': tl, # REQUIRED. target_language_code (see getServiceLanguages() for a list of codes) 'tier': tier, # REQUIRED. tier type ("machine", "standard", "pro", or "ultra") 'machine': machine, # OPTIONAL. allow machine translation 'auto_approve': 1, # OPTIONAL. Hopefully self explanatory (1 = yes, 0 = no), 'comment': comment, # OPTIONAL. Comment to leave for translator. 'callback_url': callback_url, # OPTIONAL. Callback URL that updates are sent to. # 'custom_data': 'your optional custom data, limited to 1kb.' # OPTIONAL }) #try: tt = utf8(response['response']['job']['body_tgt']) if len(tt) > 0: cacheSet('/gengo/' + sl + '/' + tl + '/' + text, tt, ttl) text = tt return tt #except: # tt = text ##except: # return text
def _(text, tm='', tier='standard', auto_approve=True, comment='', machine=True, ttl=300, debug=False, src_lc='', tgt_lc=''): """ The _() function serves as a replacement for the gettext _() function, and can be used to dynamically translate and localize web apps and dynamic content using a combination of machine and human translation. NOTE: the function looks for several global variables, including: sl : source language code tl : target language code gengo_public_key : Gengo public API key gengo_private_key : Gengo private API key Which are loaded from config.py (you'll probably want to modify this to use environment variables or similar) """ if len(src_lc) > 0: sl = src_lc else: sl = config.sl if len(tgt_lc) > 0: tl = tgt_lc else: tl = config.tl translation_order = config.translation_order gengo_public_key = config.gengo_public_key gengo_private_key = config.gengo_private_key tier = config.tier if not cacheRunning(): # if cache service is not running, return original text (translations disabled) # currently only Google App Engine memcache is supported, feel free to add # support for other cache services in cache.py return text if len(sl) < 1 or len(tl) < 1: # requires both sl (source language) and tl (target language) codes # if either is missing, returns the original untranslated text return text tt = cacheGet('/' + tm + '/' + sl + '/' + tl + '/' + text) if tt is None: # if auto_approve is enabled, human translations are automatically # returned, if it is disabled, the user must review pending translations # prior to acceptance (not recommended for automated translation implementations) if auto_approve: auto_approve = 1 else: auto_approve = 0 if len(tm) > 0: translation_order = list(tm) found = False for t in translation_order: tm = t if not found: if tm == 'gengo': tt = utf8(gengo_tm(text, sl, tl, machine=machine, public_key = gengo_public_key, private_key = gengo_private_key, tier=tier)) if len(tt) > 0: found = True #elif tm == 'transifex': # tt = utf8(transifex_tm(text, sl, tl, apikey=transifex_apikey, project='', collection='')) # if len(tt) > 0: found = True elif tm == 'microsoft': tt = utf8(microsoft_mt(text, sl, tl, apikey=microsoft_apikey)) if len(tt) > 0: found = True elif tm == 'google': # add call to Google Translate v2 API here tt = utf8(google_mt(text, sl, tl, apikey=google_apikey)) if len(tt) > 0: found = True else: pass if not found: tt = text return tt