B4A Library Translate

Hello all ,

https://github.com/xdjm/Translate in B4A

@DependsOn(values={"translate-release.aar","okhttp-3.9.1","okio-2.2.2","kotlin-stdlib-1.3.20","gson-2.8.5"})

https://drive.google.com/drive/folders/1HidohCzYR31KoF5aj0AQWRxb_lGkXz01?usp=sharing


Translate
Author:
SMM
Version: 0.01
  • Translate
    Events:
    • _result (text As String)
    Methods:
    • YoudaoTranslate (AppId As java.lang.String, text As java.lang.String, fromLanguage As java.lang.String, toLanguage As java.lang.String) As void
    • GoogleTranslate (text As java.lang.String, fromLanguage As java.lang.String, toLanguage As java.lang.String) As void
    • BaiduTranslate (AppId As java.lang.String, text As java.lang.String, fromLanguage As java.lang.String, toLanguage As java.lang.String) As void
    • Initialize (ba As anywheresoftware.b4a.BA, EventName As java.lang.String) As void
    Permissions:
    • android.permission.INTERNET


Sample :
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim trns As Translate
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    trns.Initialize("trans")
    trns.BaiduTranslate("","Hello","en","it")
    
End Sub
Sub trans_result(text As String)
    Log(text)
End Sub
 

Attachments

  • Translate.zip
    24.1 KB · Views: 310

somed3v3loper

Well-Known Member
Licensed User
Longtime User
I tried MultiPartpost and now I am getting
B4X:
{"error_code":"54001","error_msg":"Invalid Sign"}
It seems I need an appId but the strange thing is that it worked earlier with github author's appid so it might be a different issue

B4X:
private Sub Class_Globals
    Private  salt As String
    Private  Tol As String= "zh"
    Private from As String  = "en"
    Private appId As String = "20160424000019521"
    Private  securityKey As String= "Hv8XkkCeQTn5xGBPDYgj"
    Private call_back As Object
    Private event_Name As String
    Private context As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(callback As Object,eventName As String)
    
    call_back=callback
    event_Name=eventName
End Sub

Public Sub SetAppId(App_Id As String) As BaiduTrans
    appId=App_Id
    Return Me
End Sub


Public Sub SetSecurityKey( security_Key As String) As BaiduTrans
    securityKey=security_Key
    Return Me
End Sub


Public Sub ToLanguage( toLang As String) As BaiduTrans
    Tol=toLang
    Return Me
End Sub

Public Sub fromLanguage( fromLang As String) As BaiduTrans
    Return Me
End Sub

Public Sub  with(text As String)As BaiduTrans
    context=text
    
    Return Me
End Sub

Public Sub doTranslate
    salt = DateTime.Now
    Dim url As String  = "http://api.fanyi.baidu.com/api/trans/vip/translate"
    Dim transJob As HttpJob
    transJob.Initialize("transJob",Me)
    
'    Dim bodyformbuilder As JavaObject
'    bodyformbuilder.InitializeNewInstance("okhttp3.FormBody.Builder",Null)
'    bodyformbuilder.RunMethodjo("add",Array("q",trtext))
'    bodyformbuilder.RunMethodjo("add",Array("from",from))
'    bodyformbuilder.RunMethodjo("add",Array("to",Tol))
'    bodyformbuilder.RunMethodjo("add",Array("appid",appId))
'    bodyformbuilder.RunMethodjo("add",Array("salt",salt))
'    bodyformbuilder.RunMethodjo("add",Array("sign",MD5(appId&trtext&salt&securityKey)) )
    
'    Log(bodyformbuilder)
'    Dim s As String=bodyformbuilder.RunMethod("build",Null )
'    Log(s)

    Dim bodymap As Map
    bodymap.Initialize
    bodymap.Put("q",context)
    bodymap.Put("from",from)
    bodymap.Put("to",Tol)
    bodymap.Put("appid",appId)
    bodymap.Put("salt",salt)
    bodymap.Put("sign",MD5(appId&context&salt&securityKey))
    Dim jg As JSONGenerator
    jg.Initialize(bodymap)
    Dim s As String=jg.ToString
    Log(s)
    transJob.PostMultipart(url, bodymap,Null)   
'    Public void into( final OnTransSuccess onTransSuccess) {
'    OkHttpClient client = new OkHttpClient();
'    RequestBody body = new FormBody.Builder()
'    .add("q", context)
'    .add("from", from)
'    .add("to", To)
'    .add("appid", appId)
'    .add("salt", salt)
'    .add("sign", MD5.md5(appId + context + salt + securityKey))
'    .build();
'    String url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
'    Request request = new Request.Builder()
'    .url(url)
'    .post(body)
'    .build();
'   
End Sub
Private Sub jobdone(okjob As HttpJob)
    If okjob.Success Then
        
            
            
        Log(okjob.GetString)
        
        
        
    Else
        Log("error with "&okjob.JobName& " " &okjob.ErrorMessage)
    End If
    
    okjob.Release
End Sub
Private Sub MD5(obj As String) As String
    Dim md As MessageDigest
    Dim ByteCon As ByteConverter
    Dim passwordhash() As Byte

    passwordhash = md.GetMessageDigest( obj.GetBytes("UTF8"),"MD5")


    Return ByteCon.HexFromBytes( passwordhash )

    
End Sub
 
Top