Android Question Google translate not returning all text

hookshy

Well-Known Member
Licensed User
Longtime User
I use the code below to translate text in diferent languages , problem is that only first word is translated and the text does not return entirely ?

Is there a problem in the code or this is how the service works ?

B4X:
Sub translate(text As String,source As String , destination As String)
    Dim job As HttpJob
 
    Dim transtr As String = text
    Dim jobname As String = "xlate"
    Dim jobtag As String = "french"

    job.Initialize(jobname, Me)
    job.Tag = jobtag

    ' Use Google Free google API translate
    Dim srclang As String = source
    Dim dstlang As String = destination
 
    Dim poststr As String = "https://Translate.googleapis.com/translate_a/single?client=gtx&sl=" & _
    srclang & "&tl=" & dstlang & "&dt=t&q=" & transtr
  
    job.PostString(poststr, "")

    job.GetRequest.SetHeader("Content-Type", "application/json")
    job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0")
    job.GetRequest.SetContentType("application/json")
    job.GetRequest.SetContentEncoding("text/plain")
 
    'lbOut.Text = ""
End Sub

Sub MyParser(js As String) As List ' returns list of string arrays
    Dim rlst As List
    Dim sa1(), str As String
    Dim p As Int
 
    rlst.Initialize
 
    sa1 = Regex.Split("]", js)
 
    For Each s1 As String In sa1
        p = s1.LastIndexOf("[")
        If (p < 0) Then Continue
    
        str = s1.SubString2(p + 1, s1.Length)
        str = str.Replace(QUOTE, "")
    
        Dim sa2() As String = Regex.Split(",", str)
        rlst.Add(sa2)
    Next
 
    Return(rlst)
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
translate("Welcome to new york and have a nice day","en","de")
is working for me withoiut a problem. Though i do not know if the result is correct. Dont know what the null are and the 3...

Anyway: it does work for me.

[[["Willkommen in New York und einen schönen Tag","Welcome to new york and have a nice day",null,null,3]],null,"en"]
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
google can not translate a whole sentence. u get a output but often it is nonsense or incomplete.
 
Upvote 0
Top