PowerShell/Scripts/translate-text.ps1

47 lines
1.3 KiB
PowerShell
Raw Normal View History

2021-02-09 19:30:45 +01:00
#!/bin/powershell
2020-12-29 15:14:21 +01:00
<#
.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
#>
2020-10-10 18:51:57 +02:00
2021-01-02 11:08:59 +01:00
param([string]$SourceText = "", [string]$SourceLang = "en")
2020-09-29 20:05:52 +02:00
2020-12-08 13:36:22 +01:00
if ($SourceText -eq "" ) {
2020-12-29 15:14:21 +01:00
$SourceText = read-host "Enter text to translate"
2020-12-08 13:36:22 +01:00
}
2020-05-03 18:54:49 +02:00
$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 {
2020-12-08 13:45:12 +01:00
foreach($TargetLanguage in $TargetLanguages) {
2020-12-29 15:14:21 +01:00
$Result = TranslateWithGoogle $SourceText $SourceLang $TargetLanguage
2020-12-08 13:45:12 +01:00
write-output $TargetLanguage" : "$Result
2020-10-05 13:12:12 +02:00
}
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2020-12-25 11:30:43 +01:00
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}