Android Code Snippet [B4X] Simple Google Translate Class (attached for download)

Translation of texts from the source language to the target language with Google Translate (free)

Sorry, the class and example are now attached for download.

B4X:
    Dim Translate As clsGoogleTranslate
    Translate.Initialize
    'Local to engles
    Wait For (Translate.Target("en").Text(B4XFloatTextField1.Text)) Complete (Result As String)
    'Engles to italian
    Wait For (Translate.Source("en").Target("it").Text(B4XFloatTextField1.Text)) Complete (Result As String)
    B4XFloatTextField2.Text = Result

1.gif


SAMPLES:
Local to Engles

B4X:
    Wait For (Translate.Target("en").Text(B4XFloatTextField1.Text)) Complete (Result As String)
1733775809941.png


Engles to italian
B4X:
    Wait For (Translate.Source("en").Target("it").Text(B4XFloatTextField1.Text)) Complete (Result As String)

1733775991103.png


Regards
 

Attachments

  • clsGoogleTranslate.bas
    3.2 KB · Views: 49
  • B4XGoogleTranslate.zip
    4.8 KB · Views: 49
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
I think that your work can be used in this in order to help the non-English developers understand the libraries about the usage.
 

fernando1987

Active Member
Licensed User
This doesn't need an api key I use it at Lingua Locator

Example:
Sub Translate(word As String, lang1 As String, lang2 As String)
   
    Try
' Declare variables for the HttpJob and JSONParser and initialize them.
Dim j As HttpJob, parser As JSONParser
j.Initialize("", Me)
parser.Initialize("")

' Construct the URL for the Google Translate API request with the given parameters.
Dim url As String = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & lang1 & "&tl=" & lang2 & "&dt=t&q=" & word
 
' Download the translation data from the API using the HttpJob.
j.Download(url)
 
' Wait for the HttpJob to finish and handle the result.
Wait For (j) JobDone(j As HttpJob)
 
' If the request was successful, parse the response and extract the translated word.
If j.Success = True Then
    Dim s As String = j.GetString.Trim
    parser.Initialize(s)
    Dim outerList As List = parser.NextArray
   
    ' Check if the response contains the translated word.
    If outerList.Size > 0 Then
        Dim innerList As List = outerList.Get(0)
        If innerList.Size > 0 Then
            Dim firstNonNullElement As List = innerList.Get(0)
            If firstNonNullElement <> Null Then
                Log(firstNonNullElement.Get(0))
               
               
            End If
        End If
    End If
Else
    Log(2)
    ' Handle the error if necessary.
End If
 
' Release the HttpJob.
j.Release
 
' Handle exceptions if necessary.
Catch
Log(LastException)
End Try
End Sub
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
?
 

TILogistic

Expert
Licensed User
Longtime User
Note:
This class I posted only uses Google Translate, but the original version uses other translation services.

In addition, the class can be used on multiple platforms (b4i, b4a, b4j, etc.)
 

TILogistic

Expert
Licensed User
Longtime User
Note:
Edit Class (For IOS):

B4X:
Private Sub GetDeviceLanguage As String
#If B4J or B4A
    Dim jo As JavaObject
    jo = jo.InitializeStatic("java.util.Locale").RunMethod("getDefault", Null)
    Return jo.RunMethod("getLanguage", Null)
#Else
    Dim no As NativeObject
    Return no.Initialize("NSLocale").RunMethod("preferredLanguages", Null).RunMethod("objectAtIndex:", Array(0)).AsString
#End If
End Sub

Other translation services:
 

TILogistic

Expert
Licensed User
Longtime User
You can create combo boxes with ISO 639-1 language codes and call the class to translate the text. Remember that the class is translation and how you use it is up to you.
Here you will find json and others with the ISO-639 codes of languages

search:
 

TILogistic

Expert
Licensed User
Longtime User
NOTE:
I will soon publish a custom Combobox, where you can use the database, Json, XML, etc. and you can define the columns or data to display in the combo (image, text, etc.).

B4X: simple example
1.gif


SEE MORE:
 
Top