Jump to content

udf Deepl API


Go to solution Solved by argumentum,

Recommended Posts

Posted (edited)

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
Posted

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

 

Posted

👍

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

 

Posted
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

Posted

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

 

Posted

...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

Posted

whatever you think, it's not a problem for me, this is the work I did and shared, I hope it comes in handy for someone (translated with DeepL)

  • Solution
Posted
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

  • 1 year later...
Posted

Thanks cdeb for the great script. I just signed up to use the free DeepL API for my Japanese to English translation work. I copied your code, changed the following and the API key:

Local $sText = "元気ですか?"
Local $source_lang = "JA"
Local $target_lang = "EN"

I'm using the SciTE editor to run the code. It seems to me that the code ran successfully but I don't see the result. Here's what I see in the debug window. Any suggestion on where I need to fine tune the code? Thanks.

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\kl\AutoIt\DeepL1.au3" /UserParams    
+>22:08:23 Starting AutoIt3Wrapper (21.316.1639.1) from:SciTE.exe (4.4.6.0)  Keyboard:00000411  OS:WIN_11/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\na\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\na\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.16.0)  from:C:\Program Files (x86)\AutoIt3  input:C:\kl\AutoIt\DeepL1.au3
+>22:08:23 AU3Check ended.rc:0
>Running:(3.3.16.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\kl\AutoIt\DeepL1.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
+>22:08:25 AutoIt3.exe ended.rc:0
+>22:08:25 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 2.915
 

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
  • Recently Browsing   0 members

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