Skip to content

Commit

Permalink
add sugoiMT
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Dec 1, 2021
1 parent 4be12f5 commit 45f2e3d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions sugoiMT/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See <https://github.com/xulihang/ImageTrans-docs/issues/108>
45 changes: 45 additions & 0 deletions sugoiMT/sugoiMT.b4j
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
AppType=JavaFX
Build1=Default,org.xulihang.imagetrans
Group=Default Group
Library1=jcore
Library10=byteconverter
Library11=encryption
Library2=jfx
Library3=json
Library4=jxmlsax
Library5=xmlbuilder
Library6=jxui
Library7=javaobject
Library8=jokhttputils2
Library9=jstringutils
Module1=sugoiMTPlugin
NumberOfFiles=0
NumberOfLibraries=11
NumberOfModules=1
Version=8.8
@EndOfDesignText@
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region

Sub Process_Globals
Private fx As JFX
Public MainForm As Form

End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
Dim n As sugoiMTPlugin
n.Initialize
wait for (n.translate("人民網日本語版は中国で最も早く開設された日本語ニュースサイトです。","en","zh",Null)) complete (result As String)
Log(result)
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
9 changes: 9 additions & 0 deletions sugoiMT/sugoiMT.b4j.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ModuleBookmarks0=
ModuleBookmarks1=
ModuleBreakpoints0=
ModuleBreakpoints1=
ModuleClosedNodes0=
ModuleClosedNodes1=
NavigationStack=sugoiMTPlugin,Class_Globals,0,0,sugoiMTPlugin,GetNiceName,13,0,sugoiMTPlugin,Run,30,0,Main,AppStart,17,1,sugoiMTPlugin,translate,49,6,Main,Process_Globals,9,0
SelectedBuild=0
VisibleModules=1
62 changes: 62 additions & 0 deletions sugoiMT/sugoiMTPlugin.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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("path")
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
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 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("http://localhost:14366/",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

0 comments on commit 45f2e3d

Please sign in to comment.