Combreal

PocketTraductor.ps1

Apr 11th, 2022 (edited)
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requieres PSImaging module, here's the oneliner to install it :
  2. #& ([scriptblock]::Create((iwr -uri http://tinyurl.com/Install-GitHubHostedModule).Content)) -GitHubUserName Positronic-IO -ModuleName PSImaging -Branch 'master' -Scope CurrentUser
  3.  
  4.  
  5. #Requieres Greenshot https://getgreenshot.org/downloads/
  6. #And the following settings : enable   Quick Preferences/ Destination/ Copy to clipboard
  7. #                             disable  Quick Preferences/ Effects/ Show Notifications
  8.  
  9.  
  10. #Requieres AHK https://www.autohotkey.com/download/?
  11. #Hot key will be ALT+Z
  12. #
  13. #<!z::
  14. #Path_To_File = "C:\Temp\Get-TextfromImage.ps1"
  15. #MsgBox,,Traduction, % JEE_RunGetStdOut("PowerShell.exe -ExecutionPolicy Bypass -File " Path_To_File)
  16. #return
  17. #
  18. #JEE_RunGetStdOut(vTarget, vSize:="")
  19. #{
  20. #   DetectHiddenWindows, On
  21. #   vComSpec := A_ComSpec ? A_ComSpec : ComSpec
  22. #   Run, % vComSpec,, Hide, vPID
  23. #   WinWait, % "ahk_pid " vPID
  24. #   DllCall("kernel32\AttachConsole", "UInt",vPID)
  25. #   oShell := ComObjCreate("WScript.Shell")
  26. #   oExec := oShell.Exec(vTarget)
  27. #   vStdOut := ""
  28. #   if !(vSize = "")
  29. #       VarSetCapacity(vStdOut, vSize)
  30. #   while !oExec.StdOut.AtEndOfStream
  31. #       vStdOut := oExec.StdOut.ReadAll()
  32. #   DllCall("kernel32\FreeConsole")
  33. #   Process, Close, % vPID
  34. #   return vStdOut
  35. #}
  36.  
  37. #C:\Temp\Get-TextfromImage.ps1 :
  38. Function TranslateText ([string]$TextToTranslate, [string]$TargetLanguage) {
  39.     <#
  40.     .SYNOPSIS
  41.     Translate text to the specified language.
  42.     The exhaustive languages list is available in $LanguagesList.
  43.  
  44.     .EXAMPLE
  45.     TranslateText "hello" "es"
  46.     TranslateText $Text "fr"
  47.     #>
  48.  
  49.     $LanguagesList = @('af','sq','am','ar','hy','az','eu','be','bn','bs','bg','ca','ceb','zh-CN','zh-TW','co','hr','cs','da','nl','en','eo','et','fi','fr','fy','gl','ka','de','el','gu','ht','ha','haw','he','hi','hmn','hu','is','ig','id','ga','it','ja','jv','kn','kk','km','rw','ko','ku','ky','lo','lv','lt','lb','mk','mg','ms','ml','mt','mi','mr','mn','my','ne','no','ny','or','ps','fa','pl','pt','pa','ro','ru','sm','gd','sr','st','sn','sd','si','sk','sl','so','es','su','sw','sv','tl','tg','ta','tt','te','th','tr','tk','uk','ur','ug','uz','vi','cy','xh','yi','yo','zu')
  50.     $PhrasesList = New-Object -TypeName 'System.Collections.ArrayList'
  51.        
  52.     $Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$TextToTranslate"
  53.  
  54.     $RawResponse = (Invoke-WebRequest -Uri $Uri -Method Get).Content
  55.     $RawResponse = $RawResponse -replace '\[','' -replace '\]','' -replace '\"','' -replace '\\r\\n',''
  56.     $RawResponse = $RawResponse -split ',' | foreach {
  57.         If($_ -ne "null" -And (-Not $LanguagesList.Contains($_)) -And $_ -ne "true" -And $_ -notmatch "^[\d\.]+$" -And $_ -notlike "*.md" -And $TextToTranslate -notmatch $_ -And $_ -notmatch "[0123456789abcdef]{$($_.length)}") {
  58.             $PhrasesList.Add($_)
  59.         }
  60.     }
  61.  
  62.     Return $PhrasesList
  63. }
  64.  
  65. Add-Type -Assembly PresentationCore
  66. [void][reflection.assembly]::loadwithpartialname("system.windows.forms")
  67.  
  68. [Windows.Clipboard]::Clear()
  69.  
  70. [system.windows.forms.sendkeys]::sendwait('{PRTSC}')
  71.  
  72. While(-Not [Windows.Clipboard]::ContainsImage()) {
  73.     Start-Sleep -Milliseconds 200
  74. }
  75.  
  76. $ImgFullPath = "$env:TEMP\textImg.png"
  77. Get-Clipboard -Format Image | ForEach-Object -MemberName Save -ArgumentList $ImgFullPath
  78. $TextImg = Get-ChildItem $ImgFullPath | Export-ImageText
  79.  
  80. TranslateText $TextImg "fr"
  81.  
  82. $ErrorActionPreference = "silentlycontinue"
  83. Remove-Item $ImgFullPath  
  84. $ErrorActionPreference = "Continue"
Add Comment
Please, Sign In to add comment