forked from mullnerz/SublimeText-Google-Translate-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTranslate.py
118 lines (92 loc) · 4.3 KB
/
goTranslate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
# author:mtimer
# https://github.com/MTimer/SublimeText-Google-Translate-Plugin
import sublime
import sublime_plugin
import json
if sublime.version() < '3':
from core.translate import *
else:
from .core.translate import *
settings = sublime.load_settings("goTranslate.sublime-settings")
class GoTranslateCommand(sublime_plugin.TextCommand):
def run(self, edit, proxy_enable = settings.get("proxy_enable"), proxy_type = settings.get("proxy_type"), proxy_host = settings.get("proxy_host"), proxy_port = settings.get("proxy_port"), source_language = settings.get("source_language"), target_language = settings.get("target_language")):
if not source_language:
source_language = settings.get("source_language")
if not target_language:
target_language = settings.get("target_language")
if not proxy_enable:
proxy_enable = settings.get("proxy_enable")
if not proxy_type:
proxy_type = settings.get("proxy_type")
if not proxy_host:
proxy_host = settings.get("proxy_host")
if not proxy_port:
proxy_port = settings.get("proxy_port")
target_type = settings.get("target_type")
for region in self.view.sel():
if not region.empty():
v = self.view
selection = v.substr(region).encode('utf-8')
translate = GoogleTranslate(proxy_enable, proxy_type, proxy_host, proxy_port, source_language, target_language)
if not target_language:
self.view.run_command("go_translate_to")
return
else:
result = translate.translate(selection, target_type)
v.replace(edit, region, result)
if not source_language:
detected = 'Auto'
else:
detected = source_language
sublime.status_message(u'Done! (translate '+detected+' --> '+target_language+')')
def is_visible(self):
for region in self.view.sel():
if not region.empty():
return True
return False
class GoTranslateInfoCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("goTranslate.sublime-settings")
source_language = settings.get("source_language")
target_language = settings.get("target_language")
proxy_enable = settings.get("proxy_enable")
proxy_type = settings.get("proxy_type")
proxy_host = settings.get("proxy_host")
proxy_port = settings.get("proxy_port")
v = self.view
selection = v.substr(v.sel()[0])
translate = GoogleTranslate(proxy_enable, proxy_type, proxy_host, proxy_port, source_language, target_language)
text = (json.dumps(translate.langs, ensure_ascii = False, indent = 2))
v.replace(edit, v.sel()[0], text)
class GoTranslateToCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("goTranslate.sublime-settings")
source_language = settings.get("source_language")
target_language = settings.get("target_language")
proxy_enable = settings.get("proxy_enable")
proxy_type = settings.get("proxy_type")
proxy_host = settings.get("proxy_host")
proxy_port = settings.get("proxy_port")
v = self.view
selection = v.substr(v.sel()[0])
translate = GoogleTranslate(proxy_enable, proxy_type, proxy_host, proxy_port, source_language, target_language)
text = (json.dumps(translate.langs['langs'], ensure_ascii = False))
continents = json.loads(text)
lkey = []
ltrasl = []
for (slug, title) in continents.items():
lkey.append(slug)
ltrasl.append(title+' ['+slug+']')
def on_done(index):
if index >= 0:
self.view.run_command("go_translate", {"target_language": lkey[index]})
self.view.window().show_quick_panel(ltrasl, on_done)
def is_visible(self):
for region in self.view.sel():
if not region.empty():
return True
return False
def plugin_loaded():
global settings
settings = sublime.load_settings("goTranslate.sublime-settings")