mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-23 18:38:19 +02:00
Improve translate-file.ps1 and translate-text.ps1
This commit is contained in:
parent
b2e571ec15
commit
2ae727019b
5693
Data/trans
5693
Data/trans
File diff suppressed because it is too large
Load Diff
@ -12,13 +12,24 @@ if ($File -eq "" ) { $File = read-host "Enter path to file" }
|
|||||||
if ($SourceLang -eq "" ) { $SourceLang = read-host "Enter language used in this file" }
|
if ($SourceLang -eq "" ) { $SourceLang = read-host "Enter language used in this file" }
|
||||||
if ($TargetLang -eq "" ) { $TargetLang = read-host "Enter language to translate to" }
|
if ($TargetLang -eq "" ) { $TargetLang = read-host "Enter language to translate to" }
|
||||||
|
|
||||||
|
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
|
||||||
|
start-sleep -milliseconds 3000 # 20 requests per minute maximum
|
||||||
|
return $Result.translatedText
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$PathToRepo = "$PSScriptRoot/.."
|
$Lines = Get-Content -path $File
|
||||||
|
foreach($Line in $Lines) {
|
||||||
$Engine = "google" # aspell | google | bing | spell | hunspell | apertium | yandex
|
if ("$Line" -eq "") { write-output "$Line"; continue }
|
||||||
|
if ("$Line" -eq " ") { write-output "$Line"; continue }
|
||||||
start-process -filePath "$PathToRepo/Data/trans" -argumentList "-i $File -s $SourceLang -t $TargetLang -e $Engine -brief" -noNewWindow -wait
|
if ("$Line" -like "===*") { write-output "$Line"; continue }
|
||||||
|
if ("$Line" -like "---*") { write-output "$Line"; continue }
|
||||||
|
if ("$Line" -like "!*(/*)") { write-output "$Line"; continue }
|
||||||
|
$Result = UseLibreTranslate $Line $SourceLang $TargetLang
|
||||||
|
write-output $Result
|
||||||
|
}
|
||||||
exit 0
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
@ -9,19 +9,48 @@
|
|||||||
param($Text = "", $SourceLang = "en")
|
param($Text = "", $SourceLang = "en")
|
||||||
|
|
||||||
if ($Text -eq "" ) { $Text = read-host "Enter text in English to translate" }
|
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"
|
$TargetLanguages = "Arabic","Chinese","French","German","Hindi","Irish","Italian","Japanese","Korean","Portuguese","Russian","Spanish"
|
||||||
|
|
||||||
function TranslateUsingGoogle { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
$URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=$($SourceLang)&tl=$($TargetLang)&dt=t&q=$($Text)"
|
function UseGoogleTranslate { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
||||||
$result = Invoke-RestMethod $URL
|
$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]
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
function UseArgosTranslateCLI { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
||||||
|
$Result = (argos-translate.cli --from-lang $SourceLang --to-lang $TargetLang $Text)
|
||||||
|
return $Result
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
foreach($TargetLang in $TargetLanguages) {
|
foreach($TargetLanguage in $TargetLanguages) {
|
||||||
$Result = TranslateUsingGoogle $Text $SourceLang $TargetLang
|
$TargetLangCode = Language2Code $TargetLanguage
|
||||||
write-output "$TargetLang : $Result"
|
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
||||||
|
write-output "$TargetLanguage : $Result"
|
||||||
}
|
}
|
||||||
exit 0
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
|
Loading…
Reference in New Issue
Block a user