B4J Question B4J Translate via GoogleTranslate

skyracer90

Member
Licensed User
Longtime User
Hello Community,
I would like to use a text field from my application
in which I use a default language (de, en, fr, it, es, nl) and then click_Button_Translate e.g. translate the non-default language text fields via translate.google.com ...

Before Click [Button] Translate

txt_German.Text = "Hallo"
txt_English.Text = ""
txt_French.Text = ""
txt_Spain.Text = ""

Sub Click_Button_Translate

'This is the call via browser: (German to English)
'https://translate.google.com/?hl=de#view=home&op=translate&'sl=auto&tl=en&text=Hallo

'Yes, I know that I have to start this call for each language ...
'I don't know how to get the response, sorry! (e.g like "Json" or what ever)
'The idea behind this is to make my app easier to multilingual

'Text fields after Translate
txt_German.text = Hallo"
txt_Englisch.text = "Hello"
txt_French.text = "Bonjour"
txt_Spain.text = "Ola"
End Sub

Any, idea? Thank!
 

xulihang

Active Member
Licensed User
Longtime User
You may need to use the translation api in Google Cloud Platform.
 
Upvote 0

skyracer90

Member
Licensed User
Longtime User
Thank you, xulihang. You have helped me a lot. Here is my quick solution ...

Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private Job As HttpJob
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
End Sub

Sub Button1_Click
Job.Initialize("Translate", Me)
Job.poststring("https://api.mymemory.translated.net/get","q=Heute ist ein schƶner Tag!&langpair=de|en")
End Sub

Sub JobDone (Jobe As HttpJob)
Dim x,y As Int
x = Job.GetString.IndexOf("translatedText") + 18
y = Job.GetString.IndexOf("match")-38
Log(Mid(Job.GetString(),x,y))
End Sub

Sub Mid(Text As String, Start As Int, Length As Int) As String
Return Text.SubString2(Start-1,Start+Length-1)
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
B4X:
Sub JobDone (Jobe As HttpJob)
    Dim x,y As Int
    x = Job.GetString.IndexOf("translatedText") + 18
    y = Job.GetString.IndexOf("match")-38
    Log(Mid(Job.GetString(),x,y))
End Sub

You should use JSON Parser to parse this string.

My tool BasicCAT has implemented many translation apis in B4J, you can check them out: https://github.com/xulihang/BasicCAT
 
Upvote 0
Top