Exemplo n.º 1
0
 def post(self):
     if is_admin:
         parm = utf8(self.request.get('parm'))
         value = utf8(self.request.get('value'))
         Settings.save(parm, value)
         self.redirect('/settings')
     else:
         self.error(401)
         self.response.out.write('Access Denied')
Exemplo n.º 2
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))
Exemplo n.º 3
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')
Exemplo n.º 4
0
def transifex_tm(text, sl, tl, apikey='', project='', collection='', transifex_server='https://www.transifex.net', request_translation=True):
    """
    Dummy query handler for the Transifex localization and translation management platform. Once implemented will
    query the public translation memory to see if there is an exact match for the request string. If request_translation is True,
    will queue up a request for translation by authorized users, or via translation agencies connected to Transifex. returns
    'hello world for now'.
    
    Note to developers: assume the main g() function has already done a cache test, and wants to look for a translation
    in the Transifex server (which could be hosted at transifex.net or the customer's own system. Also assume that the
    requester only wants the best available/exact match (no fuzzy matching).
    
    @John K - let me know when you have a chance to implement this. Thanks -B
    """
    return utf8('')
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def hashkey(text):
    m = md5.new()
    m.update(utf8(text))
    return str(m.hexdigest())
Exemplo n.º 7
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