B4J Question B4J translation to other language

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am using several-year-old B4J software to translate the strings in HTML files to different languages (for example, English to French). I then save the translated files to disk to be included in my B4A Assets directory.

I have not found a webpage translator that allows you to translate the text to a different language and then save the translated file - it sort of works, but fixing the errors ends up being more effort than just translating the strings individually.

I have used my B4J program for several years, but now after translating about 1000 or 2000 characters Google gives me an error message basically saying "you are translating too much or too fast".

I guess they want me to upgrade from the Free Google Translator to a paid service such as Firebase or the Cloud Translator.

I have seen posts that say the new way to translate stuff is to use Firebase (or the Cloud Translator), but after searching this forum far and wide, I can't find an example of how to use Firebase in B4J or the Cloud Translator in B4J.

My current interface uses jHttpJob2 and the Translate.googleapis.com/translate_a/single end point. Very simple. I think I posted this code to this form several years ago.

Can anyone point me toward a sample that uses a newer interface, maybe Firebase or Google Cloud?

Thanks,
Barry.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Not an exact answer to your question, but very interesting too: www.deepl.com started a service, which translate docx, pptx and txt files; maybe usefull for you.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Not an exact answer to your question, but very interesting too: www.deepl.com started a service, which translate docx, pptx and txt files; maybe usefull for you.

Thanks.
They look interesting.

It is too bad they just translate the three file types. HTML files present a real challenge. You have to translate the text without messing up any of the HTML elements. I developed a B4J application which does this in a very Rube Goldberg way – it passes anything between < and > unmodified and then translates anything not between those brackets. It works, pretty much.

This is the first time I have run into the Free Google Translate error. I will have to look at their cloud interface.

Thanks,
Barry.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am using several-year-old B4J software to translate the strings in HTML files to different languages (for example, English to French). I then save the translated files to disk to be included in my B4A Assets directory.

Can anyone point me toward a sample that uses a newer interface, maybe Firebase or Google Cloud?

I figured out how to modify the example I posted years ago so that it uses the Google Cloud Translator rather than the Free Google Translator.

I was having a problem with the Free Translator. After about 1000 characters or so it would return an error message that basically said: "You are using this too much or too fast".

I signed up for the Google Cloud Translator.

Go to: https://cloud.google.com/translate/docs/quickstart
and press the blue "Set Up a Project" button. Follow the prompts. You will end up with an API Key. This needs to be inserted for the key in the source code Translate subroutine, below.

I use this to parse English strings out of a file and translate them to different languages.

B4X:
Sub Translate (jobname As String, xltext As String, sourcelang As String, destlang As String)
  Dim job As HttpJob
  Dim su As StringUtils
 
  XLateDone = ""
 
  job.Initialize(jobname, Me)
  job.Tag = 1
 
  'Use Google Cloud Translate API
  Dim pStr As String
 
  Dim q As String = su.EncodeUrl(xltext, "UTF8")
  Dim src As String = sourcelang
  Dim trg As String = destlang
  Dim fmt As String = "text"
  Dim mdl As String = "nmt"

  ' insert your project API Key from Google Cloud API-credentials page. The API key has a format similar to below.
  Dim key As String = "xxxxxxxxxxxxx-yyy-zzzzzzzzzzzzzzzzzzzzzzz"
 
  pStr = "https://translation.googleapis.com/language/translate/v2?"
  pStr = pStr & $"source=${src}&target=${trg}&model=${mdl}&format=${fmt}&key=${key}&q=${q}"$
 
  job.PostString(pStr, "")
End Sub

'Gets the Translate query result from Google
Sub JobDone(Job As HttpJob)
  If Job.Success Then
'    Log(Job.GetString)

    Dim res As String = JSParser(Job.GetString)
 
    XLateDone = res
  Else
    XLateDone = "Error Translating : Aborting Translate..."
  End If

  Job.release
End Sub

Sub JSParser(js As String) As String ' json return from Cloud Translate
'  {
'    "data": {
'      "translations": [
'        {
'          "translatedText": "Cette fonctionnalité nécessite",
'          "model": "nmt"
'        }
'      ]
'    }
'  }

'  Log("js: " & js)

  Dim s1 As String = js
  Dim sf As String = QUOTE & "translatedText" & QUOTE & ": "
  Dim res As String
 
  Dim p0 As Int = s1.IndexOf(sf)

  If (p0 >= 0) Then
    s1 = s1.SubString(p0 + sf.Length)
    s1 = s1.SubString(s1.IndexOf(QUOTE) + 1)
    res = s1.SubString2(0, s1.IndexOf(QUOTE))
  Else
    res = "no translation found"
  End If

  Return(res)
End Sub

I use the global variable XLateDone = "" to test for translation done [Do while (XLateDone = "") , Sleep(5) , Loop]. There is probably a better way using Resumable Subs.

In my B4J project I include the additional libraries: jHttp, JHttpUtils2, and jStringUtils

I hope this is useful.

Barry.
 
Last edited:
Upvote 0
Top