Skip to content

Commit e62fdd9

Browse files
committed
add papago nvcloud version
1 parent 7b8629f commit e62fdd9

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
AppType=JavaFX
2+
Build1=Default,org.xulihang.imagetrans
3+
Group=Default Group
4+
Library1=jcore
5+
Library2=jfx
6+
Library3=json
7+
Library4=jxmlsax
8+
Library5=xmlbuilder
9+
Library6=jxui
10+
Library7=javaobject
11+
Library8=jokhttputils2
12+
Library9=jstringutils
13+
Module1=papagonvcloudMTPlugin
14+
NumberOfFiles=0
15+
NumberOfLibraries=9
16+
NumberOfModules=1
17+
Version=8.9
18+
@EndOfDesignText@
19+
#Region Project Attributes
20+
#MainFormWidth: 600
21+
#MainFormHeight: 600
22+
#End Region
23+
24+
Sub Process_Globals
25+
Private fx As JFX
26+
Public MainForm As Form
27+
28+
End Sub
29+
30+
Sub AppStart (Form1 As Form, Args() As String)
31+
MainForm = Form1
32+
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
33+
MainForm.Show
34+
Dim n As papagonvcloudMTPlugin
35+
n.Initialize
36+
wait for (n.translate("I love you!","en","zh",Null)) complete (result As String)
37+
Log(result)
38+
End Sub
39+
40+
'Return true to allow the default exceptions handler to handle the uncaught exception.
41+
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
42+
Return True
43+
End Sub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
B4J=true
2+
Group=Default Group
3+
ModulesStructureVersion=1
4+
Type=Class
5+
Version=4.2
6+
@EndOfDesignText@
7+
Sub Class_Globals
8+
Private fx As JFX
9+
End Sub
10+
11+
'Initializes the object. You can NOT add parameters to this method!
12+
Public Sub Initialize() As String
13+
Log("Initializing plugin " & GetNiceName)
14+
' Here return a key to prevent running unauthorized plugins
15+
Return "MyKey"
16+
End Sub
17+
18+
' must be available
19+
public Sub GetNiceName() As String
20+
Return "papago_nvcloudMT"
21+
End Sub
22+
23+
' must be available
24+
public Sub Run(Tag As String, Params As Map) As ResumableSub
25+
Log("run"&Params)
26+
Select Tag
27+
Case "getParams"
28+
Dim paramsList As List
29+
paramsList.Initialize
30+
paramsList.Add("Client ID")
31+
paramsList.Add("Client Secret")
32+
Return paramsList
33+
Case "translate"
34+
wait for (translate(Params.Get("source"),Params.Get("sourceLang"),Params.Get("targetLang"),Params.Get("preferencesMap"))) complete (result As String)
35+
Return result
36+
End Select
37+
Return ""
38+
End Sub
39+
40+
Sub ConvertLangCode(lang As String) As String
41+
If lang="zh" Then
42+
lang="zh-CN"
43+
End If
44+
Return lang
45+
End Sub
46+
47+
Sub translate(source As String, sourceLang As String, targetLang As String,preferencesMap As Map) As ResumableSub
48+
sourceLang=ConvertLangCode(sourceLang)
49+
targetLang=ConvertLangCode(targetLang)
50+
Dim target As String
51+
Dim su As StringUtils
52+
Dim job As HttpJob
53+
job.Initialize("job",Me)
54+
Dim params As String
55+
params="text="&su.EncodeUrl(source,"UTF-8")&"&source="&sourceLang&"&target="&targetLang
56+
57+
Dim clientid,clientsecret As String
58+
clientid=getMap("papago_nvcloud",getMap("mt",preferencesMap)).Get("Client ID")
59+
clientsecret=getMap("papago_nvcloud",getMap("mt",preferencesMap)).Get("Client Secret")
60+
Dim URL As String="https://naveropenapi.apigw.ntruss.com/nmt/v1/translation"
61+
job.PostString(URL,params)
62+
job.GetRequest.SetHeader("X-NCP-APIGW-API-KEY-ID",clientid)
63+
job.GetRequest.SetHeader("X-NCP-APIGW-API-KEY",clientsecret)
64+
wait For (job) JobDone(job As HttpJob)
65+
If job.Success Then
66+
Log(job.GetString)
67+
Try
68+
Dim json As JSONParser
69+
json.Initialize(job.GetString)
70+
Dim message As Map=json.NextObject.Get("message")
71+
Dim result As Map=message.Get("result")
72+
target=result.Get("translatedText")
73+
Catch
74+
Log(LastException)
75+
End Try
76+
Else
77+
target=""
78+
End If
79+
job.Release
80+
Return target
81+
End Sub
82+
83+
84+
Sub getMap(key As String,parentmap As Map) As Map
85+
Return parentmap.Get(key)
86+
End Sub

0 commit comments

Comments
 (0)