2020-05-25 21:21:40 +02:00
|
|
|
#!/snap/bin/powershell
|
2020-10-10 18:51:57 +02:00
|
|
|
|
2020-09-29 20:05:52 +02:00
|
|
|
# Syntax: ./translate.ps1
|
2020-06-15 14:55:54 +02:00
|
|
|
# Description: translates the given text
|
|
|
|
# Author: Markus Fleschutz
|
|
|
|
# Source: github.com/fleschutz/PowerShell
|
|
|
|
# License: CC0
|
2020-09-29 20:05:52 +02:00
|
|
|
|
2020-05-03 18:54:49 +02:00
|
|
|
$SourceText = "Hello World!"
|
|
|
|
$SourceLang = "en"
|
|
|
|
$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]
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:12:12 +02:00
|
|
|
try {
|
|
|
|
foreach($TargetLang in $TargetLanguages) {
|
|
|
|
$Result = TranslateWithGoogle $SourceText $SourceLang $TargetLang
|
|
|
|
write-output $TargetLang" : "$Result
|
|
|
|
}
|
|
|
|
exit 0
|
|
|
|
} catch { Write-Error $Error[0] }
|
|
|
|
exit 1
|