PowerShell/Scripts/translate-text.ps1

31 lines
1.0 KiB
PowerShell
Raw Normal View History

2021-04-07 11:53:57 +02:00
#!/usr/bin/pwsh
2020-12-29 15:14:21 +01:00
<#
2021-04-07 15:17:49 +02:00
.SYNTAX translate-text.ps1 [<text>] [<source-lang>]
2021-04-08 08:39:13 +02:00
.DESCRIPTION translates the given text in English into other languages
2021-03-22 20:10:18 +01:00
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-10-10 18:51:57 +02:00
2021-04-08 08:39:13 +02:00
param($Text = "", $SourceLang = "en")
2020-05-03 18:54:49 +02:00
2021-04-08 08:39:13 +02:00
if ($Text -eq "" ) { $Text = read-host "Enter text in English to translate" }
$TargetLanguages = "af","da","de","el","es","fr","hr","it","ja","ko","pl","pt","nl","ru","tr","uk","vi"
2020-05-03 18:54:49 +02:00
2021-04-08 08:39:13 +02:00
function TranslateUsingGoogle { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
2020-05-03 18:54:49 +02:00
$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 {
2021-04-08 08:39:13 +02:00
foreach($TargetLang in $TargetLanguages) {
$Result = TranslateUsingGoogle $Text $SourceLang $TargetLang
write-output "$TargetLang : $Result"
2020-10-05 13:12:12 +02:00
}
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2021-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}