Update some translate-*.ps1 scripts

This commit is contained in:
Markus Fleschutz 2023-07-02 12:01:35 +02:00
parent 1eec45fb63
commit 65e833fc68
3 changed files with 18 additions and 14 deletions

View File

@ -2,9 +2,9 @@
.SYNOPSIS
Translates a text file into another language
.DESCRIPTION
This PowerShell script translates a text file into another language.
This PowerShell script translates the given text file into another language and writes the output on the console.
.PARAMETER File
Specifies the file to translate
Specifies the path to the file to be translated
.PARAMETER SourceLang
Specifies the source language
.PARAMETER TargetLang
@ -27,9 +27,9 @@ function UseLibreTranslate { param([string]$Text, [string]$SourceLang, [string]$
}
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 the file path" }
if ($SourceLang -eq "" ) { $SourceLang = Read-Host "Enter the source language" }
if ($TargetLang -eq "" ) { $TargetLang = Read-Host "Enter the target language" }
$Lines = Get-Content -path $File
foreach($Line in $Lines) {

View File

@ -19,7 +19,8 @@ function DetectSourceLang { param([string]$Filename)
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" }
if ("$Filename" -like "*-Français*") { return "fr" }
if ("$Filename" -like "*-Portuguese*") { return "pt" }
return "unknown"
}
@ -29,12 +30,14 @@ function TranslateFilename { param([string]$Filename, [string]$SourceLang, [stri
if ($SourceLang -eq "en") { $SourceLanguage = "-English" }
if ($SourceLang -eq "es") { $SourceLanguage = "-Español" }
if ($SourceLang -eq "fr") { $SourceLanguage = "-Français" }
if ($SourceLang -eq "pt") { $SourceLanguage = "-Portuguese" }
[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" }
if ($TargetLang -eq "pt") { $TargetLanguage = "-Portuguese" }
return $Filename.replace($SourceLanguage, $TargetLanguage)
}

View File

@ -6,18 +6,18 @@
.PARAMETER Text
Specifies the text to translate
.PARAMETER SourceLang
Specifies the source language
Specifies the source language (English by default)
.PARAMETER TargetLang
Specifies the target language
Specifies the target language (all by default)
.EXAMPLE
PS> ./translate-text "Hello World" de en
PS> ./translate-text "Hello World" en all
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Text = "", [string]$SourceLangCode = "en", [string]$TargetLangCode = "any")
param([string]$Text = "", [string]$SourceLangCode = "en", [string]$TargetLangCode = "all")
function UseLibreTranslate { param([string]$Text, [string]$SourceLangCode, [string]$TargetLangCode)
$Parameters = @{"q"="$Text"; "source"="$SourceLangCode"; "target"="$TargetLangCode"; }
@ -26,17 +26,18 @@ function UseLibreTranslate { param([string]$Text, [string]$SourceLangCode, [stri
}
try {
if ($Text -eq "" ) { $Text = read-host "Enter text to translate" }
if ($Text -eq "" ) { $Text = Read-Host "Enter the text to translate" }
if ($TargetLangCode -eq "any") {
if ($TargetLangCode -eq "all") {
$TargetLangCodes = "ar","de","es","fr","ga","hi","it","ja","ko","pt","ru","zh"
foreach($TargetLangCode in $TargetLangCodes) {
$Translation = UseLibreTranslate $Text $SourceLangCode $TargetLangCode
write-output "$($TargetLangCode): $Translation"
Write-Output "$($TargetLangCode): $Translation"
Start-Sleep -seconds 6 # 10 requests maximum per minute
}
} else {
$Translation = UseLibreTranslate $Text $SourceLangCode $TargetLangCode
write-output "$Translation"
Write-Output "$Translation"
}
exit 0 # success
} catch {