PowerShell/Scripts/translate-files.ps1

51 lines
1.7 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Translates text files into any supported language
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script translates text files into any supported language.
2021-10-16 16:50:10 +02:00
.PARAMETER FilePattern
Specifies the file pattern
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-25 19:43:22 +02:00
PS> ./translate-files C:\Temp\*.txt
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
Author: Markus Fleschutz / License: CC0
2021-04-09 12:53:22 +02:00
#>
2021-07-15 15:51:22 +02:00
param([string]$FilePattern = "")
2021-04-09 12:53:22 +02:00
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 {
2021-07-15 15:51:22 +02:00
if ($FilePattern -eq "" ) { $FilePattern = read-host "Enter the file pattern" }
2021-04-09 12:53:22 +02:00
$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
}
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-09 12:53:22 +02:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-04-09 12:53:22 +02:00
exit 1
}