Skip to content

Commit

Permalink
add papago nvcloud version
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed May 30, 2021
1 parent 7b8629f commit e62fdd9
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
43 changes: 43 additions & 0 deletions plugins/papagonvcloudMT/papagonvcloudMT.b4j
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
AppType=JavaFX
Build1=Default,org.xulihang.imagetrans
Group=Default Group
Library1=jcore
Library2=jfx
Library3=json
Library4=jxmlsax
Library5=xmlbuilder
Library6=jxui
Library7=javaobject
Library8=jokhttputils2
Library9=jstringutils
Module1=papagonvcloudMTPlugin
NumberOfFiles=0
NumberOfLibraries=9
NumberOfModules=1
Version=8.9
@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 papagonvcloudMTPlugin
n.Initialize
wait for (n.translate("I love you!","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
86 changes: 86 additions & 0 deletions plugins/papagonvcloudMT/papagonvcloudMTPlugin.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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 "papago_nvcloudMT"
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("Client ID")
paramsList.Add("Client Secret")
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 ConvertLangCode(lang As String) As String
If lang="zh" Then
lang="zh-CN"
End If
Return lang
End Sub

Sub translate(source As String, sourceLang As String, targetLang As String,preferencesMap As Map) As ResumableSub
sourceLang=ConvertLangCode(sourceLang)
targetLang=ConvertLangCode(targetLang)
Dim target As String
Dim su As StringUtils
Dim job As HttpJob
job.Initialize("job",Me)
Dim params As String
params="text="&su.EncodeUrl(source,"UTF-8")&"&source="&sourceLang&"&target="&targetLang

Dim clientid,clientsecret As String
clientid=getMap("papago_nvcloud",getMap("mt",preferencesMap)).Get("Client ID")
clientsecret=getMap("papago_nvcloud",getMap("mt",preferencesMap)).Get("Client Secret")
Dim URL As String="https://naveropenapi.apigw.ntruss.com/nmt/v1/translation"
job.PostString(URL,params)
job.GetRequest.SetHeader("X-NCP-APIGW-API-KEY-ID",clientid)
job.GetRequest.SetHeader("X-NCP-APIGW-API-KEY",clientsecret)
wait For (job) JobDone(job As HttpJob)
If job.Success Then
Log(job.GetString)
Try
Dim json As JSONParser
json.Initialize(job.GetString)
Dim message As Map=json.NextObject.Get("message")
Dim result As Map=message.Get("result")
target=result.Get("translatedText")
Catch
Log(LastException)
End Try
Else
target=""
End If
job.Release
Return target
End Sub


Sub getMap(key As String,parentmap As Map) As Map
Return parentmap.Get(key)
End Sub

0 comments on commit e62fdd9

Please sign in to comment.