Advertisement
Guest User

Translator

a guest
Oct 9th, 2021
1,759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###BATCH:
  2. #!Translator.bat
  3.  
  4. @echo off
  5. Powershell.exe -executionpolicy bypass -File "!Translator.ps1"
  6. echo Done.
  7. pause
  8.  
  9.  
  10.  
  11.  
  12. ###POWERSHELL:
  13. #!Translator.ps1
  14.  
  15. function Translate {
  16. param (
  17.     [String]
  18.     $TargetLanguage = "English", # See list of possible languages in $LanguageHashTable below.
  19.  
  20.     [String]
  21.     $Text = " " # This can either be the text to translate, or the path to a file containing the text to translate
  22. )
  23. # Create a Hashtable containing the full names of languages as keys and the code for that language as values
  24. $LanguageHashTable = @{
  25. Afrikaans='af'
  26. Albanian='sq'
  27. Arabic='ar'
  28. Azerbaijani='az'
  29. Basque='eu'
  30. Bengali='bn'
  31. Belarusian='be'
  32. Bulgarian='bg'
  33. Catalan='ca'
  34. 'Chinese Simplified'='zh-CN'
  35. 'Chinese Traditional'='zh-TW'
  36. Croatian='hr'
  37. Czech='cs'
  38. Danish='da'
  39. Dutch='nl'
  40. English='en'
  41. Esperanto='eo'
  42. Estonian='et'
  43. Filipino='tl'
  44. Finnish='fi'
  45. French='fr'
  46. Galician='gl'
  47. Georgian='ka'
  48. German='de'
  49. Greek='el'
  50. Gujarati='gu'
  51. Haitian ='ht'
  52. Creole='ht'
  53. Hebrew='iw'
  54. Hindi='hi'
  55. Hungarian='hu'
  56. Icelandic='is'
  57. Indonesian='id'
  58. Irish='ga'
  59. Italian='it'
  60. Japanese='ja'
  61. Kannada='kn'
  62. Korean='ko'
  63. Latin='la'
  64. Latvian='lv'
  65. Lithuanian='lt'
  66. Macedonian='mk'
  67. Malay='ms'
  68. Maltese='mt'
  69. Norwegian='no'
  70. Persian='fa'
  71. Polish='pl'
  72. Portuguese='pt'
  73. Romanian='ro'
  74. Russian='ru'
  75. Serbian='sr'
  76. Slovak='sk'
  77. Slovenian='sl'
  78. Spanish='es'
  79. Swahili='sw'
  80. Swedish='sv'
  81. Tamil='ta'
  82. Telugu='te'
  83. Thai='th'
  84. Turkish='tr'
  85. Ukrainian='uk'
  86. Urdu='ur'
  87. Vietnamese='vi'
  88. Welsh='cy'
  89. Yiddish='yi'
  90. }
  91. # Determine the target language
  92. if ($LanguageHashTable.ContainsKey($TargetLanguage)) {
  93.     $TargetLanguageCode = $LanguageHashTable[$TargetLanguage]
  94. }
  95. elseif ($LanguageHashTable.ContainsValue($TargetLanguage)) {
  96.     $TargetLanguageCode = $TargetLanguage
  97. }
  98. else {
  99.     throw "Unknown target language. Use one of the languages in the `$LanguageHashTable hashtable."
  100. }
  101. if ($Text -eq " ") {
  102. $Text = ""
  103. }
  104. elseif (Test-Path $Text -PathType Leaf) {
  105.     $Text = Get-Content $Text -Raw
  106. }
  107. #$Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguageCode)&dt=t&q=$Text"
  108. $Uri = "https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=en&q=$Text"
  109. #$RawResponse = (Invoke-WebRequest -Uri $Uri -Method Get).Content
  110. #$CleanResponse = $RawResponse -split '\\r\\n' -replace '^(","?)|(null.*?\[")|\[{3}"' -split '","'
  111. $Response = ((Invoke-WebRequest -Uri $uri -Method Get).content | ConvertFrom-Json | select -expand sentences | Select trans).trans
  112.  
  113. #$Second = $CleanResponse[2] -split '\['
  114. #$Final = $CleanResponse[0]+$Second[1]
  115. #$Final -replace '\"', "" -replace '\?', "" -replace '\\', "" -replace '\/', "" -replace '\:', "" -replace '\|', "" -replace '\*', "" -replace '\<', "" -replace '\>', ""
  116. $Final = ""
  117. foreach($name in $Response) { $final = $final+$name }
  118. $Final.trim()
  119. }
  120.  
  121. $files = Get-ChildItem -Path $pwd -exclude('*.ps1','*.bat')
  122.  
  123. foreach($file in $files) {
  124.     $extension = [io.path]::GetExtension($file)
  125.     $translation = Translate -Text "$($file.basename)"
  126.     if ($translation -eq "") {
  127.     break
  128.     }
  129.     ren "$($file.name)"  $translation$extension
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement