Combreal

TranslateText.ps1

Apr 11th, 2022 (edited)
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Text = @"
  2. Watch out for the dog.
  3. It's angry.
  4. I'm telling you.
  5. You better watch out.
  6. Do you hear me?
  7. Last warning my dude.
  8. You are going to far.
  9. I don't know.
  10. You check.
  11. I couldn't be arse with it.
  12. Now leave, please.
  13. "@
  14.  
  15. Function TranslateText ([string]$TextToTranslate, [string]$TargetLanguage) {
  16.     <#
  17.     .SYNOPSIS
  18.     Translate text to the specified language.
  19.     The exhaustive languages list is available in $LanguagesList.
  20.  
  21.     .EXAMPLE
  22.     TranslateText "hello" "es"
  23.     TranslateText $Text "fr"
  24.     #>
  25.  
  26.     $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')
  27.     $PhrasesList = New-Object -TypeName 'System.Collections.ArrayList'
  28.        
  29.     $Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$TextToTranslate"
  30.  
  31.     $RawResponse = (Invoke-WebRequest -Uri $Uri -Method Get).Content
  32.     $RawResponse = $RawResponse -replace '\[','' -replace '\]','' -replace '\"','' -replace '\\r\\n',''
  33.     $RawResponse = $RawResponse -split ',' | foreach {
  34.         If($_ -ne "null" -And (-Not $LanguagesList.Contains($_)) -And $_ -ne "true" -And $_ -notmatch "^[\d\.]+$" -And $_ -notlike "*.md" -And $TextToTranslate -notmatch $_ -And $_ -notmatch "[0123456789abcdef]{$($_.length)}") {
  35.             $PhrasesList.Add($_)
  36.         }
  37.     }
  38.  
  39.     Return $PhrasesList
  40. }
  41.  
  42. TranslateText $Text "fr"
  43. #TranslateText "hello" "es"
Add Comment
Please, Sign In to add comment