PowerShell/Scripts/translate-files.ps1

64 lines
2.4 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
Translates text files
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2023-06-12 11:36:56 +02:00
This PowerShell script translates text files into multiple languages.
.PARAMETER filePattern
Specifies the file pattern of the text file(s) to be translated
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
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-04-09 12:53:22 +02:00
#>
2023-06-12 11:36:56 +02:00
param([string]$filePattern = "")
2021-04-09 12:53:22 +02:00
function DetectSourceLang { param([string]$Filename)
2023-06-30 22:18:13 +02:00
if ("$Filename" -like "*-Deutsch*") { return "de" }
if ("$Filename" -like "*-English*") { return "en" }
if ("$Filename" -like "*-Español*") { return "es" }
2023-07-02 12:01:35 +02:00
if ("$Filename" -like "*-Français*") { return "fr" }
if ("$Filename" -like "*-Portuguese*") { return "pt" }
2021-04-09 12:53:22 +02:00
return "unknown"
}
function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
2023-05-26 09:39:57 +02:00
[string]$SourceLanguage = ""
2023-06-30 22:18:13 +02:00
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" }
2023-07-02 12:01:35 +02:00
if ($SourceLang -eq "pt") { $SourceLanguage = "-Portuguese" }
2023-06-30 22:18:13 +02:00
[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" }
2023-07-02 12:01:35 +02:00
if ($TargetLang -eq "pt") { $TargetLanguage = "-Portuguese" }
2021-04-09 12:53:22 +02:00
return $Filename.replace($SourceLanguage, $TargetLanguage)
}
try {
2023-06-12 11:36:56 +02:00
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern of the text file(s) to be translated" }
2021-07-15 15:51:22 +02:00
2021-04-09 12:53:22 +02:00
$TargetLanguages = "ar","zh","fr","de","hi","ga","it","ja","ko","pt","ru","es"
2023-06-12 11:36:56 +02:00
$SourceFiles = Get-ChildItem -path "$filePattern"
2021-04-09 12:53:22 +02:00
foreach($SourceFile in $SourceFiles) {
$SourceLang = DetectSourceLang $SourceFile
foreach($TargetLang in $TargetLanguages) {
if ($SourceLang -eq $TargetLang) { continue }
2023-06-30 22:18:13 +02:00
Write-Host "Translating $SourceFile from $SourceLang to $TargetLang ..."
2021-04-09 12:53:22 +02:00
$TargetFile = TranslateFilename $SourceFile $SourceLang $TargetLang
2023-06-30 22:18:13 +02:00
Write-Host "$TargetFile"
2021-04-09 12:53:22 +02:00
& "$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 {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-04-09 12:53:22 +02:00
exit 1
}