mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-09 21:07:40 +02:00
Improve translate-file.ps1 and translate-text.ps1
This commit is contained in:
@ -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 ($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 {
|
||||
$PathToRepo = "$PSScriptRoot/.."
|
||||
|
||||
$Engine = "google" # aspell | google | bing | spell | hunspell | apertium | yandex
|
||||
|
||||
start-process -filePath "$PathToRepo/Data/trans" -argumentList "-i $File -s $SourceLang -t $TargetLang -e $Engine -brief" -noNewWindow -wait
|
||||
|
||||
$Lines = Get-Content -path $File
|
||||
foreach($Line in $Lines) {
|
||||
if ("$Line" -eq "") { write-output "$Line"; continue }
|
||||
if ("$Line" -eq " ") { write-output "$Line"; continue }
|
||||
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
|
||||
} catch {
|
||||
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
|
Reference in New Issue
Block a user