Welcome, Guest
Username: Password: Remember me
Qui si parla italiano
  • Page:
  • 1

TOPIC:

Tradurre brevi testi, Translate short texts 23 Jun 2021 08:23 #18889

  • softdevo@tiscali.it's Avatar
  • Topic Author


  • Posts: 149
  • 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(" 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

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 23 Jun 2021 08:44 #18890

    • wriedmann
    • wriedmann's Avatar


  • Posts: 2726
  • Ciao Danilo,
    per usare i servizi Google da un applicativo devi usare gli API di Google.
    cloud.google.com/translate/docs/basic/translating-text
    Devi avere un conto, ma fino a un certo livello non costa niente.
    Oppure usi questo:
    www.labnol.org/code/19909-google-translate-api
    Saluti
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 23 Jun 2021 08:56 #18891

    • softdevo@tiscali.it's Avatar
    • Topic Author


  • Posts: 149
  • Grazie

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 23 Jun 2021 11:39 #18894

    • softdevo@tiscali.it's Avatar
    • Topic Author


  • Posts: 149
  • 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

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 23 Jun 2021 11:47 #18895

    • wriedmann
    • wriedmann's Avatar


  • Posts: 2726
  • Ciao Danilo,
    il codice che viene presentato sembra essere Javascript, ma il succo è questo:
    una chiamata
    https://translate.googleapis.com/translate_a/single?client=gtx&sl=IT&tl=EN&dt=t&q=buongiorno
    ritorna questo (json.txt):
    [[["good morning","buongiorno",null,null,1]
    ]
    ,null,"it",null,null,null,null,[]
    ]
    Saluti
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 23 Jun 2021 12:11 #18896

    • softdevo@tiscali.it's Avatar
    • Topic Author


  • Posts: 149
  • Spendido grazie, funziona,

    Danilo

    Please Log in or Create an account to join the conversation.

    Tradurre brevi testi, Translate short texts 24 Jun 2021 08:40 #18903

    • softdevo@tiscali.it's Avatar
    • Topic Author


  • Posts: 149
  • 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 := " 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

    Please Log in or Create an account to join the conversation.

    Last edit: by .

    Tradurre brevi testi, Translate short texts 24 Jun 2021 09:09 #18904

    • ic2


  • Posts: 1075
  • 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 www.bing.com/toolbox/bingdeveloper
    cString := [ api.microsofttranslator.com/V1/Http.svc/Translate?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

    Please Log in or Create an account to join the conversation.

    Last edit: by ic2.

    Tradurre brevi testi, Translate short texts 24 Jun 2021 11:52 #18906

    • softdevo@tiscali.it's Avatar
    • Topic Author


  • Posts: 149
  • Thank you Dick, Any help always helpful
    Danilo

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1
    Moderators: wriedmann