Ejemplo n.º 1
0
 def get(self):
     src_lc = self.request.get('src_lc')
     tgt_lc = self.request.get('tgt_lc')
     text = utf8(self.request.get('text'))
     if len(text) < 1:
         self.response.out.write('<form action=/q method=get>')
         self.response.out.write('<table>')
         self.response.out.write('<tr><td>Source Lang Code</td><td><input type=text name=src_lc></td></tr>')
         self.response.out.write('<tr><td>Target Lang Code</td><td><input type=text name=tgt_lc></td></tr>')
         self.response.out.write('<tr><td>Text</td><td><input type=text name=text></td></tr>')
         self.response.out.write('<tr><td colspan=2><input type=submit value="Get Translation"></td></tr>')
         self.response.out.write('</table></form>')
     else:
         guid = hashkey(src_lc + '/' + tgt_lc + '/' + text)
         translation = memcache.get('/ht/' + guid)
         if translation is not None:
             self.response.out.write(translation)
         else:
             translation = memcache.get('/mt/' + guid)
             if translation is not None:
                 self.response.out.write(translation)
             else:
                 if async_query:
                     p = dict(
                         src_lc = src_lc,
                         tgt_lc = tgt_lc,
                         text = text,
                     )
                     taskqueue.add(url='/worker', params=p)
                     translation = google_mt(text, src_lc, tgt_lc, apikey=config.google_apikey)
                     memcache.set('/mt/' + guid, translation, 600)
                     self.response.out.write(translation)
                 else:
                     self.response.out.write(_(text, src_lc=src_lc, tgt_lc=tgt_lc))
Ejemplo 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