Skip to content

Instantly share code, notes, and snippets.

@d4rkb1ue
Created August 21, 2018 02:13
Show Gist options
  • Save d4rkb1ue/49619cdd9a3742e93610ebcb00275f2b to your computer and use it in GitHub Desktop.
Save d4rkb1ue/49619cdd9a3742e93610ebcb00275f2b to your computer and use it in GitHub Desktop.
albert translate 中文 <-> English
# -*- coding: utf-8 -*-
"""Translate text using Google Translate.
Usage: tr <src lang> <dest lang> <text>
Example: tr en fr hello
Check available languages here: https://cloud.google.com/translate/docs/languages"""
import json
import urllib.parse
import urllib.request
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Google Translate"
__version__ = "1.0"
__trigger__ = "ff "
__author__ = "Manuel Schneider"
__dependencies__ = []
ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36"
urltmpl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s"
iconPath = iconLookup('config-language')
if not iconPath:
iconPath = ":python_module"
def handleQuery(query):
if query.isTriggered:
fields = query.string.split()
item = Item(id=__prettyname__, icon=iconPath, completion=query.rawString)
# if len(fields) >= 3:
if len(query.string) > 0:
# src = fields[0]
src = 'en'
# dst = fields[1]
dst = 'zh-ch'
# txt = " ".join(fields[2:])
txt = query.string
url = urltmpl % (src, dst, urllib.parse.quote_plus(txt))
req = urllib.request.Request(url, headers={'User-Agent': ua})
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode('utf-8'))
result = data[0][0][0]
item.text = result
url = urltmpl % (dst, src, urllib.parse.quote_plus(txt))
req = urllib.request.Request(url, headers={'User-Agent': ua})
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode('utf-8'))
result = data[0][0][0]
item.text = item.text + ',' + result
item.subtext = "%s-%s translation of %s" % (src.upper(), dst.upper(), txt)
item.addAction(ClipAction("Copy translation to clipboard", result))
return item
# else:
# item.text = __prettyname__
# item.subtext = "Enter a query in the form of \"&lt;srclang&gt; &lt;dstlang&gt; &lt;text&gt;\""
# return item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment