Mutate icon indicating copy to clipboard operation
Mutate copied to clipboard

can't use translation

Open steermomo opened this issue 8 years ago • 8 comments

When i typing in translate windows,it has no response. I think that translate.google.cn isn't blocked in China. Just want to know whether it works well

steermomo avatar Mar 17 '16 11:03 steermomo

@hakale , I have the same problem. Google services are working good. Russia, Ubuntu 15.10 x64.

ivanpetrov16130 avatar Mar 18 '16 13:03 ivanpetrov16130

I tried to run the translate script manually, and get an error:

Traceback (most recent call last):
  File "./google_translate.py", line 53, in <module>
    if 'dict' in get_page.keys() and 'entry' in  get_page['dict'][0]:
AttributeError: 'unicode' object has no attribute 'keys'

It seems the script cannot get translate results. I know that Google Translate API isn't available as a free service, you need to purchase it. The script is a little old. Maybe it DID some hack, but however it doesn't work now. I think you can write a script by yourself that uses other services like Bing Translate or Youdao Translate(for Chinese and English only). Baidu Translate is also good.

WuTianming avatar Aug 29 '16 12:08 WuTianming

Hey guys, I made a simple Youdao translate script. The API Key is grabbed from https://github.com/felixonmars/ydcv, and you'd better get one by yourselves.

Preview: screenshot from 2016-08-29 21-46-32

Code:

#!/usr/bin/python2
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from __future__ import print_function
import re
import urllib
import urllib2
import json
import sys
from argparse import ArgumentParser
from subprocess import check_output
from time import sleep
from urllib import quote
from urllib2 import urlopen
import re
reload(sys)
sys.setdefaultencoding('utf-8')

API = 'YouDaoCV'
API_KEY = '659600698'

def create_item(title, command, icon, subtext):
    print('[' + title + ']')
    print('command=' + command)
    print('icon=' + icon)
    print('subtext=' + subtext)

def print_explanation(data):
    result = ''
    _d = data

    query = _d['query']

    if 'basic' in _d:
        _b = _d['basic']
        for i in _b['explains']:
            create_item(i, 'copy', '', query)

    elif 'translation' in _d:
        for i in _d['translation']:
            create_item(i, 'copy', '', query)

    else:
        create_item('No related', '', '', 'Error')

def lookup_word(word):
    word = quote(word.encode('utf-8'))
    try:
        data = urlopen(
            'http://fanyi.youdao.com/openapi.do?keyfrom={0}&'
            'key={1}&type=data&doctype=json&version=1.2&q={2}'
            .format(API, API_KEY, word)).read().decode('utf-8')
    except IOError:
        create_item('Network is unavailable', '', '', 'Error')
        exit()
    else:
        print_explanation(json.loads(data))

query = sys.argv[1].split(' ')  # split the input into words. maybe not needed

for i in query:
    if i == u'':
        query.remove(i)         # remove empty strings

if len(query) != 1:             # translate the whole input string when there is only one word
    lookup_word(sys.argv[1])

for i in query:                 # get explanation of each word. maybe you don't need this
    lookup_word(i)

P.S. I'm just a python newbie and don't know much about python2. Please tell me if I made mistakes.

WuTianming avatar Aug 29 '16 13:08 WuTianming

The code above is mainly grabbed from https://github.com/felixonmars/ydcv and I changed it a little for using with Mutate.

WuTianming avatar Aug 29 '16 13:08 WuTianming

But note that you can use the API for 1000 times per hour only, but Mutate will run the script again every time you enter a letter to get results updated. That means, you can only type less than 1000 letters in an hour to translate using this script.

WuTianming avatar Aug 29 '16 14:08 WuTianming

How to make youdao translate from english to other language.

azwarain avatar Dec 21 '16 06:12 azwarain

@azwarain seems not possible.

WuTianming avatar Dec 24 '16 16:12 WuTianming

@azwarain @WuTianming Definitely we can do this, but we need to alter the existing API calls. This feature is not yet available in the mutate though.

samundra avatar Dec 24 '16 16:12 samundra