PowerShell/docs/translate-files.md
2024-11-20 11:52:20 +01:00

3.4 KiB

The translate-files.ps1 Script

This PowerShell script translates text files into multiple languages.

Parameters

/home/markus/Repos/PowerShell/scripts/translate-files.ps1 [[-filePattern] <String>] [<CommonParameters>]

-filePattern <String>
    Specifies the file pattern of the text file(s) to be translated
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./translate-files C:\Temp\*.txt

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Translates text files
.DESCRIPTION
	This PowerShell script translates text files into multiple languages.
.PARAMETER filePattern
	Specifies the file pattern of the text file(s) to be translated
.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)
	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" }
	return "unknown"
}

function TranslateFilename { param([string]$Filename, [string]$SourceLang, [string]$TargetLang)
	[string]$SourceLanguage = ""
	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" }
	return $Filename.replace($SourceLanguage, $TargetLanguage)
}

try {
	if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern of the text file(s) to be translated" }

	$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 }
			Write-Host "Translating $SourceFile from $SourceLang to $TargetLang ..."
			$TargetFile = TranslateFilename $SourceFile $SourceLang $TargetLang
			Write-Host "$TargetFile"
			& "$PSScriptRoot/translate-file.ps1" $SourceFile $SourceLang $TargetLang > $TargetFile
		}
	}
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}

(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:01)