Android Question Google Translate API

inakigarm

Well-Known Member
Licensed User
Longtime User
B4J code (I suppose will work on B4X)

B4X:
'Use Google Free google API translate for translate

'Call Translate Sub to translate a word from sourcelang to destlang (see lang codes at Google)
Translate(wordtotranslate,sourcelangname,destlangname)

'Translate Sub
Sub Translate (jobnamekey As String,sourcelang As String,destlang As String)
    
    Dim job As HttpJob
    job.Initialize(jobnamekey,Me)
    job.Tag = counttrans

    'Use Google Free google API translate
     job.PostString($"https://Translate.googleapis.com/translate_a/single?client=gtx&sl=${sourcelangname}&tl=${destlangname}&dt=t&q=${jobnamekey}"$,"")

    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")

End Sub

'Gets the Translate query result from Google
Sub JobDone(Job As HttpJob)

  If Job.Success Then
      Dim translatedword as string=Job.GetString

  Else
      LblStatus.Text="Error Translating : Aborting Translate...."

  End If

  Job.release
    
End Sub
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Does anyone have a technique deciphering the result?

translatedword returns differently on every screen rotation. I added
Translate(wordtotranslate,sourcelangname,destlangname) at Created Event in B4A.

Thanks
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks for the Google API code.

I created a small test program using this code for B4A:

upload_2017-8-14_0-17-49.png


I have attached the source code export zip.

A few caveats:
I am still using an older version of B4A. There is probably a newer preferred way to use OKHTTP …
My version also does not support Smart Strings – they would be handy.

Barry.
 

Attachments

  • Goog Trans.zip
    15.4 KB · Views: 681
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Barry,
Good to hear from you, I hope all is well with you.
I downloaded your program and ran it WITHOUT changes in the v7.0 B4a and it ran very fast and without a problem :)
So, it works with the latest compiler very well.
Regards,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Barry,
I hope all is well with you. We just had a great trade show and are excited about the future:)
Did we ever do a BING version of your Goog Trans application?
If so, I don't seem to have a copy of the source. I'm having trouble with the BING Authorization header using bearer and the accesstoken .
Do you know how to do this?
Regards,
Rusty
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi Barry,
I hope all is well with you. We just had a great trade show and are excited about the future:)
Did we ever do a BING version of your Goog Trans application?
If so, I don't seem to have a copy of the source. I'm having trouble with the BING Authorization header using bearer and the accesstoken .
Do you know how to do this?
Regards,
Rusty
No, sorry. I've no experience with BING (https://stackoverflow.com/questions...re-microsoft-translator-api-with-php-and-curl)
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I did use Bing translator a year or two ago.

It was implemented with HTTP, but it was very cumbersome and confusing. There did not seem to be any benefits versus Google.

Bing seemed to morph into Microsoft Cognitive Services. It became confusing, and my implementation stopped working – I stopped using Bing.

My Bing code was based on code snippets from their examples and Google searches.

Barry.
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Thanks for the Google API code.

I created a small test program using this code for B4A:

View attachment 58659

I have attached the source code export zip.

A few caveats:
I am still using an older version of B4A. There is probably a newer preferred way to use OKHTTP …
My version also does not support Smart Strings – they would be handy.

Barry.
Thank you very much for posting the code...
Can you explain:
B4X:
  job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0")
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I believe that header tells the receiver (Google Translate) that the request is coming from an NT machine running Firefox.

I believe I read years ago, although I may be wrong, that Google Translate requires a header of this sort in order to function properly.

Barry.
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Wondering how this webservice works , and what is
single?client

If I am going to put the sample code in my app and hundreds of users access this translate service will it still work ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
single?client
single is the end of the endpoint (url). client the first parameter.

If I am going to put the sample code in my app and hundreds of users access this translate service will it still work ?
If the api does not need a Api key and the free quotea for this endpoint is not reached; why not.
If you use an Api key then it will work with a lot of users for sure.

Note that code above doe not use an Api Key. Check the Google Documentation on how to use an Api Key with this EndPoint.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
AFAIK this is an unofficial translation API so it could be dismissed/changed at any time and it could violate copyrights or other rules/laws.
BTW, as of today, it still works ..
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
  • Like
Reactions: udg
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
For some reasons the translations of large strings as 200 charachters are not returned corectly !!! do not know how to fix or what to do about it .
 
Upvote 0
Top