Jump to content

udf Deepl API


cdeb
 Share

Go to solution Solved by argumentum,

Recommended Posts

here is a script to be able to translate texts via the API of DeepL

 

Local $sText = "Questa è una prova"
Local $source_lang = "IT"
Local $target_lang = "ES"
Local $key = "xxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxx:fx"
Local $sURL = "https://api-free.deepl.com/v2/translate"
Local $sResult = deeplapi__translate($sText, $source_lang, $target_lang, $key, $sURL)
If @error Then MsgBox($MB_SYSTEMMODAL, "", "Error deeplapi__translate() - @error ("&@error&")"&@CRLF&$sResult)



;###############################################################################
Func deeplapi__translate($sText, $source_lang, $target_lang, $key, $sURL)
    Local $debug = 0
    Local $oComErr = Null

    Local $sData = "text="&deeplapi__URIEncode($sText)&"&source_lang="&$source_lang&"&target_lang="&$target_lang
    If $debug Then ConsoleWrite("$sData: "&$sData&@CRLF)

    ;Set up a local COM error handler
    $oComErr = ObjEvent("AutoIt.Error", deeplapi__com_error_handler)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to register COM error handler - @error = " & @error)
    #forceref $oComErr

    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")

    $oHTTP.Open("POST", $sURL, False)
    If (@error) Then Return SetError(1, 0, 0)
    $oHTTP.SetRequestHeader("Authorization", "DeepL-Auth-Key "&$key&"")
    $oHTTP.SetRequestHeader("User-Agent", "YourApp/1.2.3")
    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

    $oHTTP.Send($sData)
    If @error Then Return SetError(2, 0, 0)

    If ($oHTTP.Status <> 200) Then
        Local $msgError
        Switch $oHTTP.Status
            Case 400
                $msgError = "400 (BadRequest)"
            Case 403
                $msgError = "403 (Forbidden)"
            Case 413
                $msgError = "413 (PayloadTooLarge)"
            Case 414
                $msgError = "414 (URITooLong)"
            Case 429
                $msgError = "429 (TooManyRequests)"
            Case 456
                $msgError = "456 (QuotaExceeded)"
            Case 500
                $msgError = "500 (InternalServerError)"
            Case 504
                $msgError = "504 (ServiceUnavailable)"
            Case 529
                $msgError = "529 (TooManyRequests)"

            Case Else
                $msgError = $oHTTP.Status&" (undefined status)"
        EndSwitch
        Return SetError(3, 0, $msgError)
    EndIf

    Return SetError(0, 0, $oHTTP.ResponseText)
EndFunc

#Region - internal ---------------------------------------------------------------

;###############################################################################
; source: https://www.autoitscript.com/forum/topic/95850-url-encoding/?do=findComment&comment=689060
; modified es: 'ciao%20mondo' e non 'ciao+mondo'
Func deeplapi__URIEncode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(BinaryToString(StringToBinary($sData,4),1),"")
    Local $nChar
    $sData=""
    For $i = 1 To $aData[0]
        ; ConsoleWrite($aData[$i] & @CRLF)
        $nChar = Asc($aData[$i])
        Switch $nChar
            Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
                $sData &= $aData[$i]
        ;~             Case 32
        ;~                 $sData &= "+"
            Case Else
                $sData &= "%" & Hex($nChar,2)
        EndSwitch
    Next
    Return $sData
EndFunc


;###############################################################################
Func deeplapi__com_error_handler($oError)
    With $oError
        ConsoleWrite(@CRLF & "COM ERROR DETECTED!" & @CRLF)
        ConsoleWrite("  Error ScriptLine....... " & .scriptline & @CRLF)
        ConsoleWrite("  Error Number........... " & "0x" & Hex(.number) & " (" & .number & ")" & @CRLF)
        ConsoleWrite("  Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF)
        ConsoleWrite("  Error RetCode.......... " & "0x" & Hex(Number(.retcode)) & " (" & Number(.retcode) & ")" & @CRLF)
        ConsoleWrite("  Error Description...... " & StringStripWS(.description   , $STR_STRIPTRAILING) & @CRLF)
    EndWith
    Return ;Return to allow calling function to handle error
EndFunc


#EndRegion - internal ------------------------------------------------------------

 

Edited by cdeb
Link to comment
Share on other sites

Thanks for this UDF :)
A small remark: Should the function name not be deeplapi__traNslate?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

👍

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

My Italian is rusty but "Questa è una prova" means "This is a test", but the translations were "This is a proof" and as alternatives: "This is evidence", "This is an evidence" and "This is proof".

I don't think this site is worth it.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

10 hours ago, argumentum said:

My Italian is rusty but "Questa è una prova" means "This is a test", but the translations were "This is a proof" and as alternatives: "This is evidence", "This is an evidence" and "This is proof".

I don't think this site is worth it.

I don't think there are any better alternatives to DeepL...it certainly gives its best with longer sentences. At least that is my opinion

Link to comment
Share on other sites

Prova can have both meanings.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

...not really. One has to take into consideration context. If you would add "of" then yes because you had the "a", proof of, but just proof, nah!, and not even as an alternative ?.
Literally "Questa(this) è(is) una(a) prova(proof)" but in context could never write proof, it'd be written as test.    Ma ! :D

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Solution
3 minutes ago, cdeb said:

this is the work I did and shared

oh, no. Don't get me wrong. I'm thank full for your work and sharing it !.
My beef is that I tried that same phrase on google translate and has the expected translation for the meaning.
Say I translate a bunch of stuff in languages I can not even read ( Arabic, Chinese, etc. ) and it translates to something that is not quite there or worse, offensive ?, ..I don't know !, I trust the translator. This service, in my view, I can not trust with "This is a test". Then again, I'm thinking of translating software prompts and menus. Maybe with longer texts it shows better translations, ..I don't know.

Again, for your work and taking the time of posting the code in the forum, thank you! :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...