PowerShell/Docs/translate-files.md

91 lines
2.7 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *translate-files.ps1* Script
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
This PowerShell script translates text files into any supported language.
2021-10-17 14:33:27 +02:00
## Parameters
```powershell
2023-05-26 12:20:18 +02:00
/home/mf/Repos/PowerShell/Scripts/translate-files.ps1 [[-FilePattern] <String>] [<CommonParameters>]
2021-10-17 14:33:27 +02:00
2023-05-26 12:20:18 +02:00
-FilePattern <String>
Specifies the file pattern
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2021-10-17 14:33:27 +02:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-05-26 12:20:18 +02:00
## Example
```powershell
PS> ./translate-files C:\Temp\*.txt
```
## Notes
Author: Markus Fleschutz | License: CC0
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Translates text files
2022-11-17 20:02:26 +01:00
.DESCRIPTION
This PowerShell script translates text files into any supported language.
.PARAMETER FilePattern
Specifies the file pattern
.EXAMPLE
PS> ./translate-files C:\Temp\*.txt
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$FilePattern = "")
function DetectSourceLang { param([string]$Filename)
2023-05-26 12:20:18 +02:00
if ("$Filename" -like "*Deutsch*") { return "de" }
if ("$Filename" -like "*English*") { return "en" }
2022-11-17 20:02:26 +01:00
if ("$Filename" -like "*Français*") { return "fr" }
return "unknown"
}
function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
2023-05-26 12:20:18 +02:00
[string]$SourceLanguage = ""
2022-11-17 20:02:26 +01:00
if ($SourceLang -eq "de") { $SourceLanguage = "Deutsch" }
if ($SourceLang -eq "en") { $SourceLanguage = "English" }
if ($SourceLang -eq "fr") { $SourceLanguage = "Français" }
2023-05-26 12:20:18 +02:00
$TargetLanguage = ("$PSScriptRoot/translate-text.ps1 $SourceLanguage $SourceLang $TargetLang")
2022-11-17 20:02:26 +01:00
return $Filename.replace($SourceLanguage, $TargetLanguage)
}
try {
if ($FilePattern -eq "" ) { $FilePattern = read-host "Enter the file pattern" }
$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 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2021-10-17 14:33:27 +02:00
*Generated by convert-ps2md.ps1 using the comment-based help of translate-files.ps1*