Jump to content

Google Translation with GUI


Recommended Posts

Posted (edited)

Changed the colors for my dark theme

Spoiler
... ...
Local $hGUI = GUICreate($sGUI_Title, 900, 700, -1, -1, $WS_OVERLAPPEDWINDOW, $WS_EX_ACCEPTFILES)
_WinAPI_SetCurrentProcessExplicitAppUserModelID(@ScriptName)
GUISetIcon(StringTrimRight(@ScriptFullPath, 4) & ".ico") ; https://uxwing.com/language-translation-icon/
TraySetIcon(StringTrimRight(@ScriptFullPath, 4) & ".ico")
... ...

;==============================================

Func IsDarkMode() ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1520332
    ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor
    Return (PerceivedBrightnessOfColor(_WinAPI_GetSysColor($COLOR_WINDOWTEXT)) _
             > PerceivedBrightnessOfColor(_WinAPI_GetSysColor($COLOR_WINDOW)))
    ; if the text is perceived as brighter than the background then Return True
EndFunc   ;==>IsDarkMode

Func PerceivedBrightnessOfColor($color)
    ; https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes#know-when-dark-mode-is-enabled
    ; This function performs a quick calculation of the perceived brightness of a color, and takes into consideration
    ;           ways that different channels in an RGB color value contribute to how bright it looks to the human eye.
    Local $a = StringSplit(Hex($color, 6), ""), $r = 2 * Dec($a[1] & $a[2]), $g = 5 * Dec($a[3] & $a[4]), $b = 1 * Dec($a[5] & $a[6])
    Return ($r + $g + $b)
EndFunc   ;==>PerceivedBrightnessOfColor

;==============================================
Func _UpdateCharStatus()
    Local $bColor_reddish = 0xFF4040
    Local $bColor_greenish = 0x80FF80
    Local $bColor_yellow = 0xFFFF00

    If IsDarkMode() Then
        ; for white FgColor
        $bColor_reddish = 0x661919
        $bColor_greenish = 0x264C26
        $bColor_yellow = 0x7F7F00
    EndIf

    Local $iChars = GUICtrlSendMsg($idTextFrom, $WM_GETTEXTLENGTH, 0, 0) ; same as _GUICtrlEdit_GetTextLen($idTextFrom) ?
    Select
        Case $iChars > $iMaxInputLength
            GUICtrlSetData($idTextFrom, StringLeft(GUICtrlRead($idTextFrom), $iMaxInputLength))
            ContinueCase

        Case $iChars = $iMaxInputLength
            GUICtrlSetData($idCharStatus, $iMaxInputLength & " / " & $iMaxInputLength)
            GUICtrlSetBkColor($idCharStatus, $bColor_reddish) ; reddish

        Case Else ; i.e. $iChars < $iMaxInputLength
            GUICtrlSetData($idCharStatus, $iChars & " / " & $iMaxInputLength)
            GUICtrlSetBkColor($idCharStatus, ($iChars <= 5460) ? $bColor_greenish : $bColor_yellow) ; greenish => ok, yellow => encoded string may become too long.
    EndSelect
EndFunc  ;==>_UpdateCharStatus

maybe you can add it in a next build

Thanks for sharing. Nice tool to have.

Edited by argumentum

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

Hi everybody :)

As of June 2024, Google translates 133 languages, as indicated in this Wikipedia link
These 133 languages are found in the script, but if a user would like to display less than 133 languages in both combobox lists (i.e. "language from", "language to") then I added some code to easily achieve this.

The following image shows a combobox containing 133 languages, so the user has to scroll up and down to retrieve the language he needs (or the user can type the 1st letter of the language he needs and cycle by typing again etc...)

1-howtokeeplesslanguages.png.7e360d4f34d49b601f6041b6d53616d7.png

A click on the mysterious button (labeled "?") will display a temporary GUI where you can Uncheck All languages, then check the ones you really need. Here is an example where 4 languages are checked :

2-howtokeeplesslanguages.png.b3a54c9c24e02e9fd9780bcfce9ba690.png

Here is the result : now the combobox shows only 4 languages, no scrolling required anymore.

3-howtokeeplesslanguages.png.66013847a3d829ecc0215c02f221e592.png

After that, If you want to add more languages, or delete some, just press any time the mysterious button "?"
For the record, on Windows 10, the number of items displayed in a combobox (before a vertical scrollbar appears) is 30, so if you want to avoid a vertical scrollbar and click directly on the language used for translation, then 30 seems an interesting choice.

The list of the checked languages will be preserved during sessions (it is written in the .ini file) . For those who upgrade the script from a precedent version, you'll be warned that your actual ini file is not compatible with the new script. No big deal, it will be deleted automatically, then recreated correctly when you run the new script a 2nd time.

The updated script (version 7) is found in the 1st post of this thread.
Please indicate if you found any problem.
Have a great day

 

Link to comment
Share on other sites

Posted (edited)

Hi everybody :)
There is a new version #9 of the script "Google Translate with GUI" (code in 1st post)

Now the user can display all 133 traductable language names not only in English, but in a language choosen amongst the 39 ones showed in the pic below :

39GUIlanguages.thumb.png.d30ea62e46a35fd3578996befcb8e384.png

All 133 language names will be translated "on the fly" (no need to quit the script) and they will appear like this in both combobox, for example in German if the user ticks the German GUI language from the menu :

GUIlanguageincombobox.png.2ce3245d9a3c6f8b45130ada5f867a62.png

For those who tick a right-to-left GUI language (such as Arabic, Hebrew or Persian), then all checkbox & combobox will be displayed right-to-left, like this :

RTLlanguage.png.93137f2f543f5965950d97304d1007e4.png

In all cases, the list of traductable languages that you ticked in checkbox will be preserved when you choose a new GUI language, so no need to tick them again in case you switch from one GUI language to another, nothing will be lost.

If some languages appear too small in the combobox, then you can click the appropriate label to increase / decrease their font, as shown in this pic :

Increase-decrease2comboboxfontsize.png.35b423842a29064668c854b19b1f0866.png

Please remember that the 2 Input controls and their associated Updown control shown in the preceding pic (displaying "11") refer to the 2 big EDIT fields which contain the original & translated text (in case the original or translated text appear too big or too small)

All 39 GUI languages are found in a single separate UTF-8 text file, named "Google Translate (languages list #3).txt" . Please do not rename this file as its name is required once during the script. Its size is 111 Kb (unzipped) so I choosed to zip it (32 KB) and you can download it at the end of 1st post, unzip it, then place it in the same folder as the script. It's a good idea to keep this text file read-only.

Happy translations

Edited by pixelsearch
typo
Link to comment
Share on other sites

Update June 27, 2024 (version 10) this will be probably my final update (unless a bug is discovered in the script)

+ Added code to take care of possible RTL edit fields (10 languages amongst 133) during Edit controls creation, or during the registered WM_COMMAND function (test on $CBN_CLOSEUP, $CBN_KILLFOCUS etc...)
+ Minor cosmetic changes

Link to comment
Share on other sites

Update June 30, 2024 (version 11)
Pdf files can now be opened for translation.

My bro often uses the script and he asked me a few days ago if it was possible to translate pdf files directly from the script (we're talking about pdf files that contain text which doesn't require OCR)

So I checked on the web to find a free (and reliable) command line utility that could be helpful to extract text from pdf files. After reading some reviews of users on different web pages, I choosed the free utility named "pdftotext" which is included in the Xpdf package and found on the developers web page : "Current version: 4.05 Released: 2024 Feb 08" . The results were great (speed & accuracy)

No need to download the Pdf Reader, just download the utilities as indicated on the web page :

Download the Xpdf command line tools:
...
Windows 32/64-bit (Win 7 and newer): download

Then pick the file "pdftotext.exe" (32 bits or 64 bits depending on your OS) and place it in the same folder as the script. Only 1 file is required and everything runs fine, great !

To achieve this, here is the function I added in the script :

Func _PdfToText(Const $sInputFileName)

    Local Static $sPdfToText = @ScriptDir & ((@OSVersion <> "WIN_XP") ? "\pdftotext.exe" : "\pdftotext40.exe")
    Local Static $sOutputFileName = @ScriptDir & "\TextExtractedFromPDF.txt"

    If Not FileExists($sPdfToText) Then
        MsgBox($MB_TOPMOST, "PDF : missing extractor file", _
            "To translate a pdf, you need the file 'pdftotext.exe'" & @crlf & _
            "" & @crlf & _
            "It can be downloaded at https://www.xpdfreader.com" & @crlf & _
            "then place it in same folder as the script", 0, $hGUI)
        _UpdateGUI($sEmpty)
        Return ; 0
    EndIf

    Local $sCommand = " /c" & " " & _
        chr(34) & chr(34) & $sPdfToText & chr(34) & " " & _
        "-enc UTF-8" & " " & _
        chr(34) & $sInputFileName & chr(34) & " " & _
        chr(34) & $sOutputFileName & chr(34) & chr(34)

    Local $iRet = RunWait(@ComSpec & $sCommand, @ScriptDir, @SW_HIDE)

    Select
        Case @error
            MsgBox($MB_TOPMOST, "RunWait: error " & @error, _
                "AutoIt error, please check AutoIt code", 0, $hGUI)
            Return ; 0
        Case $iRet
            Local $sErr = "" ; following exit codes (from 0 To 99) are found in PdfToText documentation, except the personal "Case Else"
            Switch $iRet
;~              Case 0
;~                  $sErr = "No error."
                Case 1
                    $sErr = "Error opening a PDF file."
                Case 2
                    $sErr = "Error opening an output file."
                Case 3
                    $sErr = "Error related to PDF permissions."
                Case 98
                    $sErr = "Out of memory." ; this exit code 98 was added in last version 4.05 of pdftotxt.exe (released on Feb 8, 2024)
                Case 99
                    $sErr = "Other error."
                Case Else ; should never happen
                    $sErr = "This error isn't found in PdfToText documentation !"
            EndSwitch
            MsgBox($MB_TOPMOST, "PdfToText exit code: error " & $iRet, _
                $sErr, 0, $hGUI)
            _UpdateGUI($sEmpty)
            Return ; 0
    EndSelect

    ; all is good from now on ($iRet = 0, which means no error returned from pdftotext.exe)
    Return $sOutputFileName
EndFunc   ;==>_PdfToText

As you can see, apart from the exit codes, I used only 1 option from the utility, which is "-enc UTF-8" but there are plenty of options as you can notice in this link

Some options seem very interesting (like page from "−f number" and page to "−l number" also "-layout" gives good results depending on the pdf etc...) but to include all options tickable in the script and make it user friendly, it would require an additional small GUI when a pdf is selected (by the open file dialog or the dropped event) and let the user ticks the options he wants. maybe in a future release of the script, we'll see... :)

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