forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate-text.ps1
More file actions
executable file
·46 lines (39 loc) · 1.26 KB
/
Copy pathtranslate-text.ps1
File metadata and controls
executable file
·46 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/snap/bin/powershell
<#
.SYNTAX ./translate-text.ps1 [<text>] [<source-lang>]
.DESCRIPTION translates the given text into other languages
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param([string]$SourceText = "", [string]$SourceLang = "en")
if ($SourceText -eq "" ) {
$SourceText = read-host "Enter text to translate"
}
$TargetLanguages = "af","da","de","el","es","hr","it","ja","ko","pl","pt","nl","ru","tr","uk","vi"
function TranslateWithGoogle {
[CmdletBinding()]
param (
[Parameter(Position = 1, ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[string]$Text,
[Parameter(Position = 2)]
[ValidateNotNullorEmpty()]
[string]$SourceLang,
[Parameter(Position = 3)]
[ValidateNotNullorEmpty()]
[string]$TargetLang
)
$URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=$($SourceLang)&tl=$($TargetLang)&dt=t&q=$($Text)"
$result = Invoke-RestMethod $URL
return $result[0][0][0]
}
try {
foreach($TargetLanguage in $TargetLanguages) {
$Result = TranslateWithGoogle $SourceText $SourceLang $TargetLanguage
write-output $TargetLanguage" : "$Result
}
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}