Esempio n. 1
0
 def requesthandler(self):
     src_lc = self.request.get('src_lc')
     tgt_lc = self.request.get('tgt_lc')
     text = utf8(self.request.get('text'))
     guid = hashkey(src_lc + '/' + tgt_lc + '/' + text)
     translation = gengo_tm(text, src_lc, tgt_lc, public_key=config.gengo_public_key, private_key=config.gengo_private_key, machine=True)
     if len(translation) > 0:
         memcache.set('/ht/' + guid, translation, 600)
     self.response.out.write('ok')
Esempio n. 2
0
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