-
Notifications
You must be signed in to change notification settings - Fork 9
Fix to support Javascript and typescript and command line #1
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
Open
heralight
wants to merge
1
commit into
kisom:master
Choose a base branch
from
heralight:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,13 +19,27 @@ | |
| import urllib | ||
| import simplejson | ||
|
|
||
| import json | ||
| import sys | ||
| reload(sys) | ||
| sys.setdefaultencoding('utf-8') | ||
|
|
||
| try: | ||
| from urllib import urlencode | ||
| except: | ||
| from urllib.parse import urlencode | ||
|
|
||
| try: | ||
| import urllib2 | ||
| except: | ||
| import urllib.request as urllib2 | ||
|
|
||
| ### globals ### | ||
|
|
||
| # variables from halotis' code | ||
| baseUrl = "http://ajax.googleapis.com/ajax/services/language/translate" | ||
| lang = 'en' | ||
|
|
||
| lang = 'en' | ||
| src_lang = 'zh-CN' | ||
| # misc vars | ||
| trace = False # enable debugging output | ||
| ext = '.en' # extension of translated | ||
|
|
@@ -45,9 +59,35 @@ | |
| scrub_scomments = re.compile(r'#\s*(.+)', re.U & re.M) | ||
|
|
||
| # extensions for valid source files | ||
| source_exts = { 'c-style':[ 'c', 'cpp', 'cc', 'h', 'hpp' ], | ||
| source_exts = { 'c-style':[ 'c', 'cpp', 'cc', 'h', 'hpp', 'js','ts' ], | ||
| 'script': [ 'py', 'pl', 'rb' ] } | ||
|
|
||
|
|
||
|
|
||
| def format_json(result, max_entries=5): | ||
| ret = [] | ||
| for sentence in result['sentences']: | ||
| ret.append(sentence['orig'] + ': ' + sentence['trans']) | ||
| ret.append('') | ||
|
|
||
| # format pos | ||
| if 'dict' in result: | ||
| for pos in result['dict']: | ||
| ret.append(pos['pos']) | ||
| for entry in pos['entry'][:max_entries]: | ||
| ret.append(entry['word'] + ': ' + ', '.join(entry['reverse_translation'])) | ||
| ret.append('') | ||
|
|
||
| return '\n'.join(ret) | ||
|
|
||
|
|
||
| def extract_translate(result, max_entries=5): | ||
| ret = [] | ||
| for sentence in result['sentences']: | ||
| ret.append(sentence['orig'] + ': ' + sentence['trans']) | ||
| return sentence['trans'] | ||
|
|
||
|
|
||
|
|
||
| def get_splits(text, splitLength = 4500): | ||
| """ | ||
|
|
@@ -57,6 +97,31 @@ def get_splits(text, splitLength = 4500): | |
|
|
||
| return (text[index:index + splitLength] | ||
| for index in xrange(0, len(text), splitLength)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def translate_call(text, from_lang="auto", to_lang="zh-CN"): | ||
| """translate text, return the result as json""" | ||
| url = 'https://translate.googleapis.com/translate_a/single?' | ||
|
|
||
| params = [] | ||
| params.append('client=gtx') | ||
| params.append('sl=' + from_lang) | ||
| params.append('tl=' + to_lang) | ||
| params.append('hl=en-US') | ||
| params.append('dt=t') | ||
| params.append('dt=bd') | ||
| params.append('dj=1') | ||
| params.append('source=input') | ||
| params.append(urlencode({'q': text})) | ||
| url += '&'.join(params) | ||
|
|
||
| request = urllib2.Request(url) | ||
| browser = "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" | ||
| request.add_header('User-Agent', browser) | ||
| response = urllib2.urlopen(request) | ||
| return json.loads(response.read().decode('utf8')) | ||
|
|
||
|
|
||
| def translate(text, src = '', to = lang): | ||
|
|
@@ -68,29 +133,17 @@ def translate(text, src = '', to = lang): | |
| * Splits up text if it's longer then 4500 characters, as a limit put | ||
| up by the API | ||
| """ | ||
|
|
||
| params = ( { | ||
| 'langpair': '%s|%s' % (src, to), | ||
| 'v': '1.0' | ||
| } ) | ||
|
|
||
| src = src_lang | ||
| retText = '' | ||
|
|
||
| for text in get_splits(text): | ||
| if trace: print '[+] translation requested...' | ||
| sys.stdout.flush() | ||
| params['q'] = text | ||
|
|
||
| resp = simplejson.load( | ||
| urllib.urlopen('%s' % (baseUrl), | ||
| data = urllib.urlencode(params)) | ||
| ) | ||
|
|
||
| try: | ||
| retText += resp['responseData']['translatedText'] | ||
| except: | ||
| retText += text | ||
| if trace: print '\treceived!' | ||
| resDic = translate_call(text, src,to) | ||
| resText = extract_translate(resDic) | ||
| retText += resText | ||
| if trace: print(text + ": " + resText) | ||
| return retText | ||
|
|
||
|
|
||
|
|
@@ -300,7 +353,7 @@ def is_script(filename): | |
|
|
||
| ##### start main code ##### | ||
| if __name__ == '__main__': | ||
| (opts, args) = getopt.getopt(sys.argv[1:], 's:d:e:o:t') | ||
| (opts, args) = getopt.getopt(sys.argv[1:], 's:d:e:o', ['t', 'srclang=', 'tolang=', 'ext=']) | ||
| dir_mode = False | ||
| target = None | ||
|
|
||
|
|
@@ -318,12 +371,19 @@ def is_script(filename): | |
| autodetect = True | ||
| if opt == '-o': | ||
| decodeas = arg | ||
|
|
||
| if opt == '--t': | ||
| trace = True | ||
| if opt == '--srclang': | ||
| src_lang = arg | ||
| if opt == '--tolang': | ||
| lang = arg | ||
| if opt == '--ext': | ||
| ext = arg | ||
|
|
||
| if dir_mode: | ||
| scan_dir(target) | ||
| else: | ||
| scan_file(target) | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was an issue in the original source code, not your patch, but can you remove all but the last newline? |
||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document these parameters?