mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-01 12:24:33 +01:00
112 lines
3.4 KiB
Markdown
112 lines
3.4 KiB
Markdown
*translate-file.ps1*
|
|
================
|
|
|
|
This PowerShell script translates the given text file into another language and writes the output on the console.
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
PS> ./translate-file.ps1 [[-File] <String>] [[-SourceLang] <String>] [[-TargetLang] <String>] [<CommonParameters>]
|
|
|
|
-File <String>
|
|
Specifies the path to the file to be translated
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-SourceLang <String>
|
|
Specifies the source language
|
|
|
|
Required? false
|
|
Position? 2
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-TargetLang <String>
|
|
Specifies the target language
|
|
|
|
Required? false
|
|
Position? 3
|
|
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
|
|
-------
|
|
```powershell
|
|
PS> ./translate-file C:\Memo.txt en de
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Translates a text file into another language
|
|
.DESCRIPTION
|
|
This PowerShell script translates the given text file into another language and writes the output on the console.
|
|
.PARAMETER File
|
|
Specifies the path to the file to be translated
|
|
.PARAMETER SourceLang
|
|
Specifies the source language
|
|
.PARAMETER TargetLang
|
|
Specifies the target language
|
|
.EXAMPLE
|
|
PS> ./translate-file C:\Memo.txt en de
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
param([string]$File = "", [string]$SourceLang = "", [string]$TargetLang = "")
|
|
|
|
function UseLibreTranslate { param([string]$Text, [string]$SourceLang, [string]$TargetLang)
|
|
$Parameters = @{"q"="$Text"; "source"="$SourceLang"; "target"="$TargetLang"; }
|
|
$Result = (Invoke-WebRequest -Uri https://libretranslate.de/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json" -useBasicParsing).content | ConvertFrom-Json
|
|
Start-Sleep -seconds 6 # 10 requests per minute maximum
|
|
return $Result.translatedText
|
|
}
|
|
|
|
try {
|
|
if ($File -eq "" ) { $File = Read-Host "Enter the file path" }
|
|
if ($SourceLang -eq "" ) { $SourceLang = Read-Host "Enter the source language" }
|
|
if ($TargetLang -eq "" ) { $TargetLang = Read-Host "Enter the target language" }
|
|
|
|
$Lines = Get-Content -path $File
|
|
foreach($Line in $Lines) {
|
|
if ("$Line" -eq "") { Write-Output "$Line"; continue }
|
|
if ("$Line" -eq " ") { Write-Output "$Line"; continue }
|
|
if ("$Line" -like "===*") { Write-Output "$Line"; continue }
|
|
if ("$Line" -like "---*") { Write-Output "$Line"; continue }
|
|
if ("$Line" -like "!*(/*)") { Write-Output "$Line"; continue }
|
|
$Result = UseLibreTranslate $Line $SourceLang $TargetLang
|
|
Write-Output $Result
|
|
}
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of translate-file.ps1 as of 08/06/2023 21:36:17)*
|