-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsugoiMTPlugin.bas
executable file
·72 lines (68 loc) · 1.99 KB
/
sugoiMTPlugin.bas
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
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=4.2
@EndOfDesignText@
Sub Class_Globals
Private fx As JFX
End Sub
'Initializes the object. You can NOT add parameters to this method!
Public Sub Initialize() As String
Log("Initializing plugin " & GetNiceName)
' Here return a key to prevent running unauthorized plugins
Return "MyKey"
End Sub
' must be available
public Sub GetNiceName() As String
Return "sugoiMT"
End Sub
' must be available
public Sub Run(Tag As String, Params As Map) As ResumableSub
Log("run"&Params)
Select Tag
Case "getParams"
Dim paramsList As List
paramsList.Initialize
paramsList.Add("url")
Return paramsList
Case "translate"
wait for (translate(Params.Get("source"),Params.Get("sourceLang"),Params.Get("targetLang"),Params.Get("preferencesMap"))) complete (result As String)
Return result
Case "getDefaultParamValues"
Return CreateMap("url":"http://localhost:14366")
End Select
Return ""
End Sub
Sub translate(source As String, sourceLang As String, targetLang As String,preferencesMap As Map) As ResumableSub
Dim target As String
Dim url As String = "http://localhost:14366"
If preferencesMap.ContainsKey("api") Then
Dim api As Map = preferencesMap.Get("api")
If api.ContainsKey("sugoi") Then
Dim settings As Map = api.Get("sugoi")
url=settings.GetDefault("url",url)
End If
End If
Dim params As Map
params.Initialize
params.Put("content",source)
params.Put("message","translate sentences")
Dim json As JSONGenerator
json.Initialize(params)
Dim job As HttpJob
job.Initialize("",Me)
job.PostString(url,json.ToString)
job.GetRequest.SetContentType("application/json")
Wait For (job) JobDone (job As HttpJob)
If job.Success Then
target=job.GetString
If target.StartsWith($"""$) And source.StartsWith($"""$) = False Then
target=target.SubString2(1,target.Length-1)
End If
Else
target=""
End If
job.Release
Return target
End Sub