Skip to content
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

can't use translation #119

Open
steermomo opened this issue Mar 17, 2016 · 8 comments
Open

can't use translation #119

steermomo opened this issue Mar 17, 2016 · 8 comments

Comments

@steermomo
Copy link

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

@ivanpetrov16130
Copy link

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

@WuTianming
Copy link

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
Copy link

WuTianming commented Aug 29, 2016

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
Copy link

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

@WuTianming
Copy link

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.

@azwarain
Copy link

How to make youdao translate from english to other language.

@WuTianming
Copy link

@azwarain seems not possible.

@samundra
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants