-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translation to Chinese doesn't work #6
Comments
I'm not planning on creating a fork or PR for this because it's not really a clean solution, but if you want a hotfix which works good enough, replace all the content inside # Utilized resources
# https://github.com/YogurtTheHorse/ulauncher-translator
# https://github.com/mouuff/mtranslate
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
import textwrap
import re
import requests
import html
import urllib.request
import urllib.parse
agent = {'User-Agent': "Mozilla/5.0 (Android 9; Mobile; rv:67.0.3) Gecko/67.0.3 Firefox/67.0.3"}
def translate(to_translate, to_language="auto", from_language="auto", wrap_len="80"):
base_link = "https://translate.google.com/m?tl=%s&sl=%s&q=%s"
to_translate = urllib.parse.quote(to_translate)
link = base_link % (to_language, from_language, to_translate)
response = requests.get(link, headers=agent)
raw_data = response.content
data = raw_data.decode("utf-8")
expr = r'class="result-container">(.*?)<'
re_result = re.findall(expr, data)
result = html.unescape(re_result[0]) if len(re_result) > 0 else ""
return ("\n".join(textwrap.wrap(result, int(wrap_len) if wrap_len.isdigit() else 80 )))
class TranslateExtension(Extension):
def __init__(self):
super(TranslateExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
argument = (event.get_argument() or '').strip()
if len(argument) == 0:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='No input',
on_enter=HideWindowAction())
])
from_language = extension.preferences.get("mainlang", "auto")
to_language = extension.preferences.get("otherlang", "auto")
query = argument
language_string = argument.split(" ")[0]
language_list = language_string.split(":")
if ":" in language_string and not any(len(x) > 5 for x in language_list):
from_language = language_list[0] or from_language
to_language = language_list[1] if len(language_list) > 1 else to_language
query = " ".join(argument.split(" ")[1:])
ceviri = translate(query, to_language, from_language, extension.preferences["wrap"])
items = [
ExtensionResultItem(icon='images/icon.png',
name=query.replace("\n",""),
description=translate(query, to_language, from_language, extension.preferences["wrap"]),
on_enter=CopyToClipboardAction(ceviri))
]
return RenderResultListAction(items)
if __name__ == '__main__':
TranslateExtension().run() It only works with the right capitalization, haven't tested it very thoroughly for other use cases |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's supposed to work either with
zh
orzh-CN
, but neither work. The first yields results in English, and the second gets split in the dash.The text was updated successfully, but these errors were encountered: