Tradurre brevi testi, Translate short texts

Forum dedicato ai programmatori di X# in lingua italiana – Italian language forum

Moderator: wriedmann

Post Reply
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Salve, in un mio programma devo inviare email in italiano ed inglese. Pensavo di tradurre il testo con una chiamata a google translator, con un codice più o meno così:

Hi, in one of my programs I have to send emails in Italian and English. I was thinking of translating the text with a call to google translator, with some code like this:

#using System.Net
FUNCTION Start( ) AS VOID
LOCAL input_language AS STRING
LOCAL output_language AS STRING
LOCAL result AS STRING
input_language := "it"
output_language := "en"
result := TranslateText("Buongiorno", input_language + "|" + output_language)

System.Console.WriteLine(result)

RETURN

FUNCTION TranslateText(input AS STRING,languagePair AS STRING) AS STRING
LOCAL url AS STRING
LOCAL oWebClient AS WebClient
LOCAL result AS STRING

url := String.Format("https://translate.google.it/?text={0}&langpair={1}", input, languagePair)

oWebClient := WebClient{}
oWebClient:Encoding := System.Text.Encoding.Default
result := oWebClient:DownloadString(url)
result := result:Substring(result:IndexOf("data-text"))
result := result:Replace("data-text=","")
result := result:Substring(0, result:IndexOf("data-crosslingual-hint"))
result := result:Replace(Chr(34),"")
RETURN result

Ma torna sempre il testo italiano.
But the Italian text always comes back.

Grazie
thank you
User avatar
wriedmann
Posts: 3765
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Tradurre brevi testi, Translate short texts

Post by wriedmann »

Ciao Danilo,
per usare i servizi Google da un applicativo devi usare gli API di Google.
https://cloud.google.com/translate/docs ... ating-text
Devi avere un conto, ma fino a un certo livello non costa niente.
Oppure usi questo:
https://www.labnol.org/code/19909-google-translate-api
Saluti
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Grazie
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Ho guardato con attenzione
www.labnol.org/code/19909-google-translate-api
ma dall'esempio che è presente nella pagina non ci ho capito molto, credo manchino molti riferimenti.

I looked carefully
www.labnol.org/code/19909-google-translate-api
but from the example that is present on the page I did not understand much, I think there are many references.

Danilo
User avatar
wriedmann
Posts: 3765
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Tradurre brevi testi, Translate short texts

Post by wriedmann »

Ciao Danilo,
il codice che viene presentato sembra essere Javascript, ma il succo è questo:
una chiamata

Code: Select all

https://translate.googleapis.com/translate_a/single?client=gtx&sl=IT&tl=EN&dt=t&q=buongiorno
ritorna questo (json.txt):

Code: Select all

[[["good morning","buongiorno",null,null,1]
]
,null,"it",null,null,null,null,[]
]
Saluti
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Spendido grazie, funziona,

Danilo
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Riporto qui il codice se può essere utile ad altri.

Here is the code if it can be useful to others.

Danilo

#using System.Net
FUNCTION Start( ) AS VOID
LOCAL input_language AS STRING
LOCAL output_language AS STRING
LOCAL result AS STRING
input_language := "it"
output_language := "en"
result := TranslateText("La giustificazione - ORE ORDINARIE LAVORATE di 9.00 ore per il 23/06/2021 è stata concessa - Il responsabile",input_language,output_language)

System.Console.WriteLine(result)

RETURN

FUNCTION TranslateText(input AS STRING,input_language := "it" AS STRING,output_language := "en" AS STRING) AS STRING //,languagePair AS STRING)
LOCAL url AS STRING
LOCAL oWebClient AS WebClient
LOCAL result AS STRING
LOCAL n AS INT

url := "https://translate.googleapis.com/translate_a/single?"+;
"client=gtx&sl="+input_language+"&tl="+;
"output_language+"&dt=t&q="+input


oWebClient := WebClient{}
oWebClient:Encoding := System.Text.Encoding.Default
result := oWebClient:DownloadString(url)
n := result:IndexOf(input:Substring(0,10))
IF n > 4
result := result:Substring(4,n-7)
ELSE
result := ""
ENDIF

RETURN result
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Tradurre brevi testi, Translate short texts

Post by ic2 »

Hello Danilo,

I react in English, you will be able to translate. You can also use Microsoft translation. Here's the code. The function LangIC2ToMicrosoft just translates our internal language codes to what is used in Microsoft, so Italian to English would be it and en.

Dick


FUNCTION TranslateViaMicrosoft (cFrom AS STRING, cTo AS STRING, cPhrase AS STRING) AS STRING PASCAL

LOCAL oHTTP AS cHTTP

LOCAL cString AS STRING
LOCAL cResult AS STRING
LOCAL cAppID AS STRING
SetAnsi (TRUE)
cFrom := LangIC2ToMicrosoft(cFrom)
cTo := LangIC2ToMicrosoft(cTo)
oHTTP := CHttp {"x"}
cPhrase := AllTrim(cPhrase)
cPhrase := StrTran(cPhrase, "&")
cPhrase := StrTran(cPhrase, "%")
cPhrase := StrTran(cPhrase, "#")

cAppID := "Something" // via http://www.bing.com/toolbox/bingdeveloper
cString := [http://api.microsofttranslator.com/V1/H ... ate?appId=] + cAppID + [&text=] + cPhrase + [&from=] + cFrom + [&to=] + cTo

cResult := oHTTP:GetDocumentByURL(cString)

IF Left(cResult, 3) == CHR(0xEF) + CHR(0xBB) + CHR(0xBF)
cResult := ConvertFromCodePageToCodePage(SubStr2(cResult, 4), CP_UTF8, CP_ACP)
ENDIF

oHTTP:CloseRemote()
oHTTP:Axit()

SetAnsi(FALSE) // For ADS

RETURN cResult



FUNCTION ConvertFromCodePageToCodePage(cString AS STRING, dwFrom AS DWORD, dwTo AS DWORD) AS STRING
LOCAL pUStr AS PTR
LOCAL pBuffer AS PTR
LOCAL nLen,nULen AS LONG
// Convert VO string from dynamic memory to fixed memory
nLen := LONG(SLen(cString))
pBuffer := MemAlloc(DWORD(nLen))
MemCopyString(pBuffer,cString,DWORD(nLen))

// Determine length of Unicode string
// And allocate enough space to hold it
nULen := MultiByteToWideChar(dwFrom,0,pBuffer,nLen,NULL_PTR, 0)
pUStr := SysAllocStringLen(NULL_PTR,DWORD(nULen))
// Convert Fixed memory Ansi string to Fixed memory Unicode string
MultiByteToWideChar(dwFrom,0,pBuffer,nLen,pUStr,nULen)

// Now determine size needed for ANSI string
nLen := WideCharToMultiByte(dwTo,0,pUStr,nULen,NULL_PTR,0, NULL,NULL)

// Allocate Fixed memory buffer to hold the UTF8 string
pBuffer := MemRealloc(pBuffer, DWORD(nLen+1))
// Convert Unicode to Ansi
nLen := WideCharToMultiByte(dwTo,0,pUStr,nULen,pBuffer,nLen ,NULL,NULL)

// Convert fixed memory buffer to dynamic memory string
cString := Mem2String(pBuffer,DWORD(nLen))
// Release fixed memory buffer
MemFree(pBuffer)
// Release the Unicode String
SysFreeString(pUStr)

RETURN cString
User avatar
softdevo@tiscali.it
Posts: 191
Joined: Wed Sep 30, 2015 1:30 pm

Tradurre brevi testi, Translate short texts

Post by softdevo@tiscali.it »

Thank you Dick, Any help always helpful
Danilo
Post Reply