PowerShell/Scripts/translate-files.ps1

49 lines
1.6 KiB
PowerShell
Raw Normal View History

2021-08-26 10:46:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
translate-files.ps1 [<file-pattern>]
.DESCRIPTION
2021-08-29 17:50:03 +02:00
Translates the given text files into any supported language.
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\translate-files.ps1 C:\Temp\*.txt
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
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
}
}
exit 0
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-04-09 12:53:22 +02:00
exit 1
}