New .tr syntax for translations
parent
b2fe5b31e2
commit
e3633957a9
|
@ -280,6 +280,7 @@ tock.priority = 'high'
|
|||
|
||||
def npl(phenny, input):
|
||||
"""Shows the time from NPL's SNTP server."""
|
||||
# for server in ('ntp1.npl.co.uk', 'ntp2.npl.co.uk'):
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
client.sendto('\x1b' + 47 * '\0', ('ntp1.npl.co.uk', 123))
|
||||
data, address = client.recvfrom(1024)
|
||||
|
|
|
@ -20,6 +20,8 @@ class Grab(web.urllib.URLopener):
|
|||
|
||||
def google_ajax(query):
|
||||
"""Search using AjaxSearch, and return its JSON."""
|
||||
if isinstance(query, unicode):
|
||||
query = query.encode('utf-8')
|
||||
uri = 'http://ajax.googleapis.com/ajax/services/search/web'
|
||||
args = '?v=1.0&safe=off&q=' + web.urllib.quote(query)
|
||||
handler = web.urllib._urlopener
|
||||
|
|
|
@ -12,6 +12,11 @@ import re, urllib
|
|||
import web
|
||||
|
||||
def translate(text, input='auto', output='en'):
|
||||
raw = False
|
||||
if output.endswith('-raw'):
|
||||
output = output[:-4]
|
||||
raw = True
|
||||
|
||||
import urllib2, json
|
||||
opener = urllib2.build_opener()
|
||||
opener.addheaders = [(
|
||||
|
@ -31,7 +36,10 @@ def translate(text, input='auto', output='en'):
|
|||
result = result.replace(',,', ',null,')
|
||||
data = json.loads(result)
|
||||
|
||||
try: language = data[-2][0][0]
|
||||
if raw:
|
||||
return str(data), 'en-raw'
|
||||
|
||||
try: language = data[2] # -2][0][0]
|
||||
except: language = '?'
|
||||
|
||||
return ''.join(x[0] for x in data[0]), language
|
||||
|
@ -61,10 +69,46 @@ def tr(phenny, context):
|
|||
phenny.reply(msg)
|
||||
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
||||
|
||||
tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
|
||||
tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$')
|
||||
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
|
||||
tr.priority = 'low'
|
||||
|
||||
def tr2(phenny, input):
|
||||
"""Translates a phrase, with an optional language hint."""
|
||||
command = input.group(2).encode('utf-8')
|
||||
|
||||
def langcode(p):
|
||||
return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()
|
||||
|
||||
args = ['auto', 'en']
|
||||
|
||||
for i in xrange(2):
|
||||
if not ' ' in command: break
|
||||
prefix, cmd = command.split(' ', 1)
|
||||
if langcode(prefix):
|
||||
args[i] = prefix[1:]
|
||||
command = cmd
|
||||
phrase = command
|
||||
|
||||
if (len(phrase) > 350) and (not context.admin):
|
||||
return phenny.reply('Phrase must be under 350 characters.')
|
||||
|
||||
src, dest = args
|
||||
if src != dest:
|
||||
msg, src = translate(phrase, src, dest)
|
||||
if isinstance(msg, str):
|
||||
msg = msg.decode('utf-8')
|
||||
if msg:
|
||||
msg = web.decode(msg) # msg.replace(''', "'")
|
||||
msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
|
||||
else: msg = 'The %s to %s translation failed, sorry!' % (src, dest)
|
||||
|
||||
phenny.reply(msg)
|
||||
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
||||
|
||||
tr2.commands = ['tr']
|
||||
tr2.priority = 'low'
|
||||
|
||||
def mangle(phenny, input):
|
||||
phrase = input.group(2).encode('utf-8')
|
||||
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
||||
|
|
Loading…
Reference in New Issue