Add translate-file.ps1

This commit is contained in:
Markus Fleschutz
2021-04-08 08:39:13 +02:00
parent 1907062856
commit b2e571ec15
5 changed files with 5730 additions and 22 deletions

View File

@ -1,30 +1,17 @@
#!/usr/bin/pwsh
<#
.SYNTAX translate-text.ps1 [<text>] [<source-lang>]
.DESCRIPTION translates the given text into other languages
.DESCRIPTION translates the given text in English into other languages
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param($SourceText = "", $SourceLang = "en")
if ($SourceText -eq "" ) { $SourceText = read-host "Enter text to translate" }
param($Text = "", $SourceLang = "en")
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"
function TranslateWithGoogle {
[CmdletBinding()]
param (
[Parameter(Position = 1, ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[string]$Text,
[Parameter(Position = 2)]
[ValidateNotNullorEmpty()]
[string]$SourceLang,
[Parameter(Position = 3)]
[ValidateNotNullorEmpty()]
[string]$TargetLang
)
function TranslateUsingGoogle { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
$URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=$($SourceLang)&tl=$($TargetLang)&dt=t&q=$($Text)"
$result = Invoke-RestMethod $URL
@ -32,9 +19,9 @@ function TranslateWithGoogle {
}
try {
foreach($TargetLanguage in $TargetLanguages) {
$Result = TranslateWithGoogle $SourceText $SourceLang $TargetLanguage
write-output $TargetLanguage" : "$Result
foreach($TargetLang in $TargetLanguages) {
$Result = TranslateUsingGoogle $Text $SourceLang $TargetLang
write-output "$TargetLang : $Result"
}
exit 0
} catch {