PowerShell/docs/translate-files.md

109 lines
3.4 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *translate-files.ps1*
========================
2022-11-17 20:02:26 +01:00
2023-07-29 09:45:37 +02:00
This PowerShell script translates text files into multiple languages.
2021-10-17 14:33:27 +02:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-10-17 14:33:27 +02:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./translate-files.ps1 [[-filePattern] <String>] [<CommonParameters>]
2021-10-17 14:33:27 +02:00
2023-07-29 09:45:37 +02:00
-filePattern <String>
Specifies the file pattern of the text file(s) to be translated
2023-05-26 12:20:18 +02:00
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-07-29 10:04:38 +02:00
Example
-------
2023-05-26 12:20:18 +02:00
```powershell
PS> ./translate-files C:\Temp\*.txt
```
2023-07-29 10:04:38 +02:00
Notes
-----
2023-05-26 12:20:18 +02:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2023-05-26 12:20:18 +02:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
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
2023-07-29 09:45:37 +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
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./translate-files C:\Temp\*.txt
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-07-29 09:45:37 +02:00
param([string]$filePattern = "")
2022-11-17 20:02:26 +01:00
function DetectSourceLang { param([string]$Filename)
2023-07-29 09:45:37 +02:00
if ("$Filename" -like "*-Deutsch*") { return "de" }
if ("$Filename" -like "*-English*") { return "en" }
if ("$Filename" -like "*-Español*") { return "es" }
if ("$Filename" -like "*-Français*") { return "fr" }
if ("$Filename" -like "*-Portuguese*") { return "pt" }
2022-11-17 20:02:26 +01:00
return "unknown"
}
function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
2023-05-26 12:20:18 +02:00
[string]$SourceLanguage = ""
2023-07-29 09:45:37 +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" }
if ($SourceLang -eq "pt") { $SourceLanguage = "-Portuguese" }
[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" }
if ($TargetLang -eq "pt") { $TargetLanguage = "-Portuguese" }
2022-11-17 20:02:26 +01:00
return $Filename.replace($SourceLanguage, $TargetLanguage)
}
try {
2023-07-29 09:45:37 +02:00
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern of the text file(s) to be translated" }
2022-11-17 20:02:26 +01:00
$TargetLanguages = "ar","zh","fr","de","hi","ga","it","ja","ko","pt","ru","es"
2023-07-29 09:45:37 +02:00
$SourceFiles = Get-ChildItem -path "$filePattern"
2022-11-17 20:02:26 +01:00
foreach($SourceFile in $SourceFiles) {
$SourceLang = DetectSourceLang $SourceFile
foreach($TargetLang in $TargetLanguages) {
if ($SourceLang -eq $TargetLang) { continue }
2023-07-29 09:45:37 +02:00
Write-Host "Translating $SourceFile from $SourceLang to $TargetLang ..."
2022-11-17 20:02:26 +01:00
$TargetFile = TranslateFilename $SourceFile $SourceLang $TargetLang
2023-07-29 09:45:37 +02:00
Write-Host "$TargetFile"
2022-11-17 20:02:26 +01:00
& "$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
2024-05-19 10:25:56 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of translate-files.ps1 as of 05/19/2024 10:25:26)*