B4A Library Translate

somed3v3loper

Well-Known Member
Licensed User
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

GeoT

Member
Licensed User
Hi.

Good job. It works for me, but now all these jar files must be inside the folder C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries:

kotlin-stdlib-1.3.20.jar
okhttp-3.5.0.jar
okhttp-3.9.1.jar
okio-1.11.0.jar
okio-2.2.2.jar

You can download them from mvnrepository.com for example.

In addition to your Translate.jar

Regards.
 
Last edited:

somed3v3loper

Well-Known Member
Licensed User
but now all these jar files must be inside the folder C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries:

kotlin-stdlib-1.3.20.jar
okhttp-3.5.0.jar
okhttp-3.9.1.jar
okio-1.11.0.jar
okio-2.2.2.jar
Yes sorry for that these are Translate dependencies
If there is any other way that can help reduce them , please tell me how
 

DonManfred

Expert
Licensed User
I don't think so
Why? I just look at the github source. For example BaiduTrans.java
B4X:
 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();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, final Response response){
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            assert response.body() != null;
                            onTransSuccess.out(new Gson().fromJson(response.body().string(), Trans.class).getTrans_result().get(0).getDst());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }
It can be easily done with b4a using okhttputils2. You need to build the correct request for sure. But i don´t see a reason why it should not work.
 

Sasuke Sama

Active Member
Licensed User
im getting this

B4X:
B4A Version: 8.50
Parsing code.    (0.10s)
Building folders structure.    (0.08s)
Compiling code.    (2.39s)
Compiling layouts code.    (0.06s)
Organizing libraries.    (0.00s)
Generating R file.    (1.36s)
Compiling generated Java code.    (4.69s)
Convert byte code - optimized dex.    Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Address;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheResponseBody;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:692)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:315)
    at com.android.dx.command.dexer.Main.runDx(Main.java:293)
    at com.android.dx.command.dexer.Main.main(Main.java:249)
    at com.android.dx.command.Main.main(Main.java:94)
Caused by: java.lang.InterruptedException: Too many errors
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:684)
    ... 4 more
 

somed3v3loper

Well-Known Member
Licensed User
im getting this

B4X:
B4A Version: 8.50
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Address;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheResponseBody;
It must be because this library depends on another okhttp .jar
I will see what I can do
Did you try to untick okhttp from libraries tab
 

somed3v3loper

Well-Known Member
Licensed User
Google Translate worked but the other two are still NOT WORKING
BaiduTranslate returns {"error_code":"52003","error_msg":"UNAUTHORIZED USER"}
YoudoaTranslate returns a similar message with some nulls
If someone can signup for an appid , they can test and feedback
Also you can fix my mistakes :D
B4X:
       Dim trns As GoogleTrans
    trns.Initialize(Me,"trans")
    trns.fromLanguage("en").ToLanguage("ar").with("hello guys").doTranslate
B4X:
Sub trans_result(text As String)
    Log(text)
End Sub
 

Attachments

Sasuke Sama

Active Member
Licensed User
Google Translate worked but the other two are still NOT WORKING
BaiduTranslate returns {"error_code":"52003","error_msg":"UNAUTHORIZED USER"}
YoudoaTranslate returns a similar message with some nulls
If someone can signup for an appid , they can test and feedback
Also you can fix my mistakes :D
B4X:
       Dim trns As GoogleTrans
    trns.Initialize(Me,"trans")
    trns.fromLanguage("en").ToLanguage("ar").with("hello guys").doTranslate
B4X:
Sub trans_result(text As String)
    Log(text)
End Sub
Google works great now but it gave me some errors when trying to translate a larg list will keep testing it
 

DonManfred

Expert
Licensed User
can you share logs ?
not really. I just compared the java code with your class code.
The java source is using a Requestbuilder and setting values to it. My thought is that a multipartpost is probably more suitable/compareable than a json poststring...
 

somed3v3loper

Well-Known Member
Licensed User
not really. I just compared the java code with your class code.
The java source is using a Requestbuilder and setting values to it. My thought is that a multipartpost is probably more suitable/compareable than a json poststring...
I tried using requestbuilder through JavaObject but I got the same result
I will try MultiPartpost and see
 
Top