2021-04-07 11:53:57 +02:00
|
|
|
#!/usr/bin/pwsh
|
2020-12-29 15:14:21 +01:00
|
|
|
<#
|
2021-04-09 12:53:22 +02:00
|
|
|
.SYNTAX translate-text.ps1 [<text>] [<source-lang>] [<target-lang>]
|
|
|
|
.DESCRIPTION translates the given text 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-09 12:53:22 +02:00
|
|
|
param($Text = "", $SourceLang = "en", $TargetLang = "any")
|
2021-04-08 08:39:13 +02:00
|
|
|
if ($Text -eq "" ) { $Text = read-host "Enter text in English to translate" }
|
2020-05-03 18:54:49 +02:00
|
|
|
|
2021-04-08 11:19:53 +02:00
|
|
|
function Language2Code { param([string]$Language)
|
|
|
|
$Code = switch($Language) {
|
|
|
|
"Arabic" {"ar"}
|
|
|
|
"Chinese" {"zh"}
|
|
|
|
"French" {"fr"}
|
|
|
|
"German" {"de"}
|
|
|
|
"Hindi" {"hi"}
|
|
|
|
"Irish" {"ga"}
|
|
|
|
"Italian" {"it"}
|
|
|
|
"Japanese"{"ja"}
|
|
|
|
"Korean" {"ko"}
|
|
|
|
"Portuguese"{"pt"}
|
|
|
|
"Russian" {"ru"}
|
|
|
|
"Spanish" {"es"}
|
|
|
|
Default {$Language}
|
|
|
|
}
|
|
|
|
return $Code
|
|
|
|
}
|
|
|
|
|
|
|
|
function UseGoogleTranslate { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
|
|
|
$Result = Invoke-RestMethod "https://translate.googleapis.com/translate_a/single?client=gtx&sl=$($SourceLang)&tl=$($TargetLang)&dt=t&q=$($Text)"
|
|
|
|
return $Result[0][0][0]
|
|
|
|
}
|
|
|
|
|
|
|
|
function UseLibreTranslate { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
|
|
|
$Parameters = @{"q"="$Text"; "source"="$SourceLang"; "target"="$TargetLang"; }
|
|
|
|
$Result = (Invoke-WebRequest -Uri https://libretranslate.com/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json").content | ConvertFrom-Json
|
|
|
|
return $Result.translatedText
|
|
|
|
}
|
2020-05-03 18:54:49 +02:00
|
|
|
|
2021-04-08 11:19:53 +02:00
|
|
|
function UseArgosTranslateCLI { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
|
|
|
$Result = (argos-translate.cli --from-lang $SourceLang --to-lang $TargetLang $Text)
|
|
|
|
return $Result
|
2020-05-03 18:54:49 +02:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:12:12 +02:00
|
|
|
try {
|
2021-04-09 12:53:22 +02:00
|
|
|
if ($TargetLang -eq "any") {
|
|
|
|
$TargetLanguages = "Arabic","Chinese","French","German","Hindi","Irish","Italian","Japanese","Korean","Portuguese","Russian","Spanish"
|
|
|
|
foreach($TargetLang in $TargetLanguages) {
|
|
|
|
$TargetLangCode = Language2Code $TargetLang
|
|
|
|
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
|
|
|
write-output "$TargetLang : $Result"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$TargetLangCode = Language2Code $TargetLang
|
2021-04-08 11:19:53 +02:00
|
|
|
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
2021-04-09 12:53:22 +02:00
|
|
|
write-output "$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
|
|
|
|
}
|