mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-23 21:20:51 +01:00
Update the translate-*.ps1 scripts
This commit is contained in:
parent
44ad2aecf8
commit
1dbc01e8a3
@ -21,25 +21,25 @@ param([string]$File = "", [string]$SourceLang = "", [string]$TargetLang = "")
|
||||
|
||||
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
|
||||
$Result = (Invoke-WebRequest -Uri https://libretranslate.de/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json" -useBasicParsing).content | ConvertFrom-Json
|
||||
Start-Sleep -seconds 6 # 10 requests per minute maximum
|
||||
return $Result.translatedText
|
||||
}
|
||||
|
||||
try {
|
||||
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" }
|
||||
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" }
|
||||
|
||||
$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 }
|
||||
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
|
||||
Write-Output $Result
|
||||
}
|
||||
exit 0 # success
|
||||
} catch {
|
||||
|
@ -16,18 +16,25 @@
|
||||
param([string]$filePattern = "")
|
||||
|
||||
function DetectSourceLang { param([string]$Filename)
|
||||
if ("$Filename" -like "*Deutsch*") { return "de" }
|
||||
if ("$Filename" -like "*English*") { return "en" }
|
||||
if ("$Filename" -like "*Français*") { return "fr" }
|
||||
if ("$Filename" -like "*-Deutsch*") { return "de" }
|
||||
if ("$Filename" -like "*-English*") { return "en" }
|
||||
if ("$Filename" -like "*-Español*") { return "es" }
|
||||
if ("$Filename" -like "*_Français*") { return "fr" }
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
|
||||
[string]$SourceLanguage = ""
|
||||
if ($SourceLang -eq "de") { $SourceLanguage = "Deutsch" }
|
||||
if ($SourceLang -eq "en") { $SourceLanguage = "English" }
|
||||
if ($SourceLang -eq "fr") { $SourceLanguage = "Français" }
|
||||
$TargetLanguage = ("$PSScriptRoot/translate-text.ps1 $SourceLanguage $SourceLang $TargetLang")
|
||||
if ($SourceLang -eq "de") { $SourceLanguage = "-Deutsch" }
|
||||
if ($SourceLang -eq "en") { $SourceLanguage = "-English" }
|
||||
if ($SourceLang -eq "es") { $SourceLanguage = "-Español" }
|
||||
if ($SourceLang -eq "fr") { $SourceLanguage = "-Français" }
|
||||
[string]$TargetLanguage = "-Unknown"
|
||||
if ($TargetLang -eq "ar") { $TargetLanguage = "-Arabic" }
|
||||
if ($TargetLang -eq "de") { $TargetLanguage = "-Deutsch" }
|
||||
if ($TargetLang -eq "en") { $TargetLanguage = "-English" }
|
||||
if ($TargetLang -eq "es") { $TargetLanguage = "-Español" }
|
||||
if ($TargetLang -eq "fr") { $TargetLanguage = "-Français" }
|
||||
return $Filename.replace($SourceLanguage, $TargetLanguage)
|
||||
}
|
||||
|
||||
@ -40,7 +47,9 @@ try {
|
||||
$SourceLang = DetectSourceLang $SourceFile
|
||||
foreach($TargetLang in $TargetLanguages) {
|
||||
if ($SourceLang -eq $TargetLang) { continue }
|
||||
Write-Host "Translating $SourceFile from $SourceLang to $TargetLang ..."
|
||||
$TargetFile = TranslateFilename $SourceFile $SourceLang $TargetLang
|
||||
Write-Host "$TargetFile"
|
||||
& "$PSScriptRoot/translate-file.ps1" $SourceFile $SourceLang $TargetLang > $TargetFile
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ param([string]$Text = "", [string]$SourceLangCode = "en", [string]$TargetLangCod
|
||||
|
||||
function UseLibreTranslate { param([string]$Text, [string]$SourceLangCode, [string]$TargetLangCode)
|
||||
$Parameters = @{"q"="$Text"; "source"="$SourceLangCode"; "target"="$TargetLangCode"; }
|
||||
$Result = (Invoke-WebRequest -Uri https://translate.mentality.rip/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json" -useBasicParsing).content | ConvertFrom-Json
|
||||
$Result = (Invoke-WebRequest -Uri https://libretranslate.de/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json" -useBasicParsing).content | ConvertFrom-Json
|
||||
return $Result.translatedText
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user