Android Question Simple translation

tsteward

Well-Known Member
Licensed User
Longtime User
I'm looking for a simple way to translate some text.
I've found a few threads here but A. I don't understand them and B. they are mostly outdated.

For starters I would like to translate a simple paragraph of text but maybe in the future I would like to translate a html file I create and display in my current app.

I found this thread which seems simple enough but no longer works.

Has anyone got a sample working that I could look at please.

Thanks
Tony
 

tsteward

Well-Known Member
Licensed User
Longtime User
At this stage I would like to put a translate button below each thread in the feedback like some forums have.
 

Attachments

  • Screenshot_20201108_200011_lishi.assistand.tony.stewardgmail.com.jpg
    Screenshot_20201108_200011_lishi.assistand.tony.stewardgmail.com.jpg
    471.9 KB · Views: 152
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
All translation services provide a REST API. It is just a matter of registering and making the correct call.

Example with Google Translate service:
B4J non-ui
B4X:
Sub Process_Globals
    Private API_KEY As String = "AIzaSyC..."
End Sub

Sub AppStart (Args() As String)
    Translate("ניסיון אחת שתיים שלוש", "he", "en")
    StartMessageLoop
End Sub

Sub Translate (Text As String, SourceLang As String, TargetLang As String)
    Dim m As Map = CreateMap("q": Text, "source": SourceLang, "target": TargetLang, "format": "text")
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString($"https://translation.googleapis.com/language/translate/v2?key=${API_KEY}"$, jg.ToString)
    j.GetRequest.SetContentType("application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim p As JSONParser
        p.Initialize(j.GetString)
        Dim result As Map = p.NextObject
        Log(result)
        Dim data As Map = result.Get("data")
        Dim translations As List = data.Get("translations")
        For Each t As Map In translations
            Log(t.Get("translatedText"))
        Next
    End If
    j.Release
End Sub

For this to work, you need to register with Google Cloud Platform and, enable the translate API and create an API key.

This is a server side solution. Assuming that it is a public app then you should create a simple server with B4J which acts as a middleman between the mobile apps and the translation service.
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Okay thank you. A shame this is a paid service.....google already get enough out of me LoL
 
Upvote 0

Unobtainius

Active Member
Licensed User
Longtime User
if you actually know the strings you want to display SD has a good example (well I liked it) in his calendar example that depending the devices default language, changes the calendar day headings, and the month to those used by the relevant language. Not much good if you want on the fly translating of course, but I thought it was worth a mention
 
Upvote 0
Top