mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-08 17:28:18 +02:00
Add translate-files.ps1
This commit is contained in:
parent
e0204a009b
commit
b351e570d9
@ -144,6 +144,7 @@ switch-shelly1.ps1, switches a Shelly1 device in the local network
|
|||||||
take-screenshot.ps1, takes a single screenshot
|
take-screenshot.ps1, takes a single screenshot
|
||||||
take-screenshots.ps1, takes multiple screenshots
|
take-screenshots.ps1, takes multiple screenshots
|
||||||
translate-file.ps1, translates the given text file into another language
|
translate-file.ps1, translates the given text file into another language
|
||||||
|
translate-files.ps1, translates the given text files into any supported language
|
||||||
translate-text.ps1, translates the given text into other languages
|
translate-text.ps1, translates the given text into other languages
|
||||||
turn-volume-up.ps1, turns the audio volume up (+10% by default)
|
turn-volume-up.ps1, turns the audio volume up (+10% by default)
|
||||||
turn-volume-down.ps1, turns the audio volume down (-10% by default)
|
turn-volume-down.ps1, turns the audio volume down (-10% by default)
|
||||||
|
|
@ -175,6 +175,7 @@ Mega Collection of PowerShell Scripts
|
|||||||
* [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars
|
* [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars
|
||||||
* [switch-shelly1.ps1](Scripts/switch-shelly1.ps1) - switches a Shelly1 device in the local network
|
* [switch-shelly1.ps1](Scripts/switch-shelly1.ps1) - switches a Shelly1 device in the local network
|
||||||
* [translate-file.ps1](Scripts/translate-file.ps1) - translates the given text file into other languages
|
* [translate-file.ps1](Scripts/translate-file.ps1) - translates the given text file into other languages
|
||||||
|
* [translate-files.ps1](Scripts/translate-files.ps1) - translates the given text files into any supported language
|
||||||
* [translate-text.ps1](Scripts/translate-text.ps1) - translates the given text in English into other languages
|
* [translate-text.ps1](Scripts/translate-text.ps1) - translates the given text in English into other languages
|
||||||
* [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast
|
* [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast
|
||||||
* [weather-alert.ps1](Scripts/weather-alert.ps1) - checks the current weather for critical values
|
* [weather-alert.ps1](Scripts/weather-alert.ps1) - checks the current weather for critical values
|
||||||
|
42
Scripts/translate-files.ps1
Executable file
42
Scripts/translate-files.ps1
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
#!/bin/powershell
|
||||||
|
<#
|
||||||
|
.SYNTAX ./translate-files.ps1 [<file-pattern>]
|
||||||
|
.DESCRIPTION translates the given text files into any supported language
|
||||||
|
.LINK https://github.com/fleschutz/PowerShell
|
||||||
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
|
#>
|
||||||
|
|
||||||
|
param($FilePattern = "")
|
||||||
|
if ($FilePattern -eq "" ) { $FilePattern = read-host "Enter the file pattern" }
|
||||||
|
|
||||||
|
function DetectSourceLang { param([string]$Filename)
|
||||||
|
if ("$Filename" -like "*Deutsch*") { return "de" }
|
||||||
|
if ("$Filename" -like "*English*") { return "en" }
|
||||||
|
if ("$Filename" -like "*Français*") { return "fr" }
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
|
||||||
|
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)
|
||||||
|
return $Filename.replace($SourceLanguage, $TargetLanguage)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$TargetLanguages = "ar","zh","fr","de","hi","ga","it","ja","ko","pt","ru","es"
|
||||||
|
$SourceFiles = get-childItem -path "$FilePattern"
|
||||||
|
foreach($SourceFile in $SourceFiles) {
|
||||||
|
$SourceLang = DetectSourceLang $SourceFile
|
||||||
|
foreach($TargetLang in $TargetLanguages) {
|
||||||
|
if ($SourceLang -eq $TargetLang) { continue }
|
||||||
|
$TargetFile = TranslateFilename $SourceFile $SourceLang $TargetLang
|
||||||
|
& "$PSScriptRoot/translate-file.ps1" $SourceFile $SourceLang $TargetLang > $TargetFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit 0
|
||||||
|
} catch {
|
||||||
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
exit 1
|
||||||
|
}
|
@ -1,15 +1,13 @@
|
|||||||
#!/usr/bin/pwsh
|
#!/usr/bin/pwsh
|
||||||
<#
|
<#
|
||||||
.SYNTAX translate-text.ps1 [<text>] [<source-lang>]
|
.SYNTAX translate-text.ps1 [<text>] [<source-lang>] [<target-lang>]
|
||||||
.DESCRIPTION translates the given text in English into other languages
|
.DESCRIPTION translates the given text into other languages
|
||||||
.LINK https://github.com/fleschutz/PowerShell
|
.LINK https://github.com/fleschutz/PowerShell
|
||||||
.NOTES Author: Markus Fleschutz / License: CC0
|
.NOTES Author: Markus Fleschutz / License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param($Text = "", $SourceLang = "en")
|
param($Text = "", $SourceLang = "en", $TargetLang = "any")
|
||||||
|
|
||||||
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 = "Arabic","Chinese","French","German","Hindi","Irish","Italian","Japanese","Korean","Portuguese","Russian","Spanish"
|
|
||||||
|
|
||||||
function Language2Code { param([string]$Language)
|
function Language2Code { param([string]$Language)
|
||||||
$Code = switch($Language) {
|
$Code = switch($Language) {
|
||||||
@ -47,10 +45,17 @@ function UseArgosTranslateCLI { param([string]$Text, [string]$SourceLang, [strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
foreach($TargetLanguage in $TargetLanguages) {
|
if ($TargetLang -eq "any") {
|
||||||
$TargetLangCode = Language2Code $TargetLanguage
|
$TargetLanguages = "Arabic","Chinese","French","German","Hindi","Irish","Italian","Japanese","Korean","Portuguese","Russian","Spanish"
|
||||||
|
foreach($TargetLang in $TargetLanguages) {
|
||||||
|
$TargetLangCode = Language2Code $TargetLang
|
||||||
|
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
||||||
|
write-output "$TargetLang : $Result"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$TargetLangCode = Language2Code $TargetLang
|
||||||
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
$Result = UseLibreTranslate $Text $SourceLang $TargetLangCode
|
||||||
write-output "$TargetLanguage : $Result"
|
write-output "$Result"
|
||||||
}
|
}
|
||||||
exit 0
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
|
Loading…
Reference in New Issue
Block a user