-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQTslate.ahk
190 lines (171 loc) · 6.83 KB
/
QTslate.ahk
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#Include Translator.ahk
#Include <AHKv2_Scripts\ClipSend> ; https://github.com/Axlefublr/lib-v2/blob/03f0bd71ec1e73753f33631b25e17d1fd81889e8/Utils/ClipSend.ahk
class Qtslate extends Translator {
static TranslationMode := ["From Clipboard", "In Place", "User Input"]
static CurrentModeIndex := 1
static Languages := ["auto", "en", "ru", "de"]
static SourceLanguageIndex := 1
static TargetLanguageIndex := 3
/**
* Method for switching Qtslate between translations methods - 'From Clipboard', 'In place' and 'User Input'.
* @param {number} backwards Used when you need to cycle backwards, for example with mouse. Purely optional, as there is only 3 methods available as of 04.2023.
*/
static SwitchMode(backwards := false) {
if backwards = true {
if this.SourceLanguageIndex = 1
this.SourceLanguageIndex := this.Languages.Length
else
this.SourceLanguageIndex--
}
else {
if this.CurrentModeIndex = this.TranslationMode.Length
this.CurrentModeIndex := 1
else
this.CurrentModeIndex++
}
ToolTip("'" this.TranslationMode[this.CurrentModeIndex] "' mode is activated.")
SetTimer () => ToolTip(), -2000
}
/**
* Method for cycling target languages. Based on a counter to cycle through the array of languages. Skips the index #1 which is 'auto' because you must specify the target language.
* @param {Boolean} backwards Used when you need to cycle backwards, for example with mouse.
*/
static CycleTargetLanguage(backwards := false) {
if backwards = true {
if this.TargetLanguageIndex = 2
this.TargetLanguageIndex := this.Languages.Length
else
this.TargetLanguageIndex--
}
else {
if this.TargetLanguageIndex = this.Languages.Length
this.TargetLanguageIndex := 2
else
this.TargetLanguageIndex++
}
ToolTip("Target language = " StrUpper(this.Languages[this.TargetLanguageIndex]))
SetTimer () => ToolTip(), -1500
}
/**
* Method for cycling languages. Based on a counter to cycle through the array of languages.
* @param {Boolean} backwards Used when you need to cycle backwards, for example with mouse.
*/
static CycleSourceLanguage(backwards := false) {
if backwards = true {
if this.SourceLanguageIndex = 1
this.SourceLanguageIndex := this.Languages.Length
else
this.SourceLanguageIndex--
}
else {
if this.SourceLanguageIndex = this.Languages.Length
this.SourceLanguageIndex := 1
else
this.SourceLanguageIndex++
}
ToolTip("Source language = " StrUpper(this.Languages[this.SourceLanguageIndex]))
SetTimer () => ToolTip(), -1500
}
/**
* Call a translation method based on the current translation mode.
*/
static Activate() {
switch this.CurrentModeIndex {
case 1:
this.FromClipboard()
case 2:
this.InPlace()
case 3:
this.UserInput()
default:
this.FromClipboard()
}
}
/**
* Method to display currently active translation mode and languages.
*/
static ShowCurrentSettings() {
Settings := "Mode: " this.TranslationMode[this.CurrentModeIndex]
. "`nSource: " this.Languages[this.SourceLanguageIndex]
. "`nTarget: " this.Languages[this.TargetLanguageIndex]
ToolTip(Settings)
SetTimer () => ToolTip(), -1500
}
/**
* Method used by other translation modes for displaying the translated text in a new window. The 'Copy' button can be used to copy the translation result. Otherwise, `Escape` can be pressed to close the GUI.
*/
static Display(translation) {
g := Gui()
g.Title := "QTslate"
g.Opt("ToolWindow")
g.AddText("vTranslationText w300", translation)
button := g.AddButton("vCopy Default", "Copy")
button.OnEvent("Click", CopyTranslation)
g.OnEvent("Escape", CloseWindow)
g.Show()
CopyTranslation(*) {
A_Clipboard := translation
g.Destroy()
}
CloseWindow(*) {
g.Destroy()
}
}
/**
* Used to translate text within a text editor. For example, in MS Word you can select a word or a whole paragraph and call this method. The selection will be copied, translated and pasted in place.
*/
static InPlace() {
A_Clipboard := ""
Send("^c")
ClipWait(1)
translation := this.Translate(A_Clipboard, this.Languages[this.TargetLanguageIndex], this.Languages[this.SourceLanguageIndex])
ClipSend(translation)
}
/**
* Method to show an input box. The entered text will then be translated and displayed in a new window via Display method.
*/
static UserInput() {
ib := InputBox("Enter text to translate", "QTslate")
if ib.Result = "Cancel"
return
if ib.Value = ""
return
translation := this.Translate(ib.Value, this.Languages[this.TargetLanguageIndex], this.Languages[this.SourceLanguageIndex])
this.Display(translation)
}
/**
* Method to translate text from the clipboard. The translation will the be displayed in a new window via Display method.
*/
static FromClipboard() {
translation := this.Translate(A_Clipboard, this.Languages[this.TargetLanguageIndex], this.Languages[this.SourceLanguageIndex])
this.Display(translation)
}
/**
* GUI for selecting mode, source and target languages.
*/
static Controls(*) {
controls := Gui()
controls.Opt("AlwaysOnTop ToolWindow")
controls.Title := "QTslate"
controls.AddText(, "Mode")
controls.AddListBox("vMode r4 Choose2 AltSubmit", this.TranslationMode)
controls.AddText("ym", "Source language")
controls.AddListBox("r4 vSource Choose2 AltSubmit", this.Languages)
Selector := controls.AddButton("vSelect Default w120", "Select")
Selector.OnEvent("Click", Update)
controls.AddText("ym", "Target language")
controls.AddListBox("r4 vTarget Choose3 AltSubmit", this.Languages)
controls.OnEvent("Escape", (*) => close_gui())
controls.OnEvent("Close", (*) => close_gui())
controls.Show()
Update(*) {
this.CurrentModeIndex := controls.Submit().Mode
this.SourceLanguageIndex := controls.Submit().Source
this.TargetLanguageIndex := controls.Submit().Target
}
close_gui() {
controls.Destroy()
Exit()
}
}
}