-
Notifications
You must be signed in to change notification settings - Fork 3
/
yaTranslate.py
123 lines (91 loc) · 3.42 KB
/
yaTranslate.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
119
120
121
122
123
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import json
if sublime.version() < '3':
from core import *
else:
from .core import *
settings = sublime.load_settings("yaTranslate.sublime-settings")
class YaTranslateCommand(sublime_plugin.TextCommand):
def run(self, edit, output_language = settings.get("output_language"), output_language_additional = settings.get("output_language_additional")):
key = settings.get("key")
ui_lang = settings.get("ui_lang")
if not output_language:
output_language = settings.get("output_language")
if not output_language_additional:
output_language_additional = settings.get("output_language_additional")
output_type = settings.get("type")
for region in self.view.sel():
if not region.empty():
if key:
v = self.view
selection = v.substr(region).encode('utf-8')
translate = YandexTranslate(key, ui_lang)
detected = translate.detect(selection)
if detected:
if (detected == output_language):
result = translate.translate(selection, detected+'-'+output_language_additional, output_type)
else:
result = translate.translate(selection, detected+'-'+output_language, output_type)
else:
sublime.status_message(u'Error of translate text: look in console!')
text = (json.dumps(result['text'][0], ensure_ascii = False)).strip('"').replace('\\n', "\n").replace('\\t', "\t").replace('\\"', '"')
v.replace(edit, region, text)
if (result['code'] == 200):
sublime.status_message(u'Text translated: '+detected+' --> '+output_language)
else:
sublime.status_message(u'Error of translate text: look in console!')
else:
sublime.status_message(u'API Key not defined!')
def is_visible(self):
for region in self.view.sel():
if not region.empty():
return True
return False
class YaTranslateInfoCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("yaTranslate.sublime-settings")
key = settings.get("key")
ui_lang = settings.get("ui_lang")
output_language = settings.get("output_language")
output_type = settings.get("type")
if key:
v = self.view
selection = v.substr(v.sel()[0])
translate = YandexTranslate(key, ui_lang)
text = (json.dumps(translate.langs, ensure_ascii = False, indent = 2))
v.replace(edit, v.sel()[0], text)
else:
sublime.status_message(u'API Key not defined!')
class YaTranslateToCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("yaTranslate.sublime-settings")
key = settings.get("key")
ui_lang = settings.get("ui_lang")
output_type = settings.get("type")
if key:
v = self.view
selection = v.substr(v.sel()[0])
translate = YandexTranslate(key, ui_lang)
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("ya_translate", {"output_language": lkey[index]})
self.view.window().show_quick_panel(ltrasl, on_done)
else:
sublime.status_message(u'API Key not defined!')
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("yaTranslate.sublime-settings")