2024-01-25 13:37:12 +01:00
|
|
|
Script: *translate-file.ps1*
|
|
|
|
========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 09:45:37 +02:00
|
|
|
This PowerShell script translates the given text file into another language and writes the output on the console.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-07-29 10:15:44 +02:00
|
|
|
PS> ./translate-file.ps1 [[-File] <String>] [[-SourceLang] <String>] [[-TargetLang] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-File <String>
|
2023-07-29 09:45:37 +02:00
|
|
|
Specifies the path to the file to be translated
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
|
|
|
PS> ./translate-file C:\Memo.txt en de
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01: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
|
|
|
|
Translates a text file into another language
|
|
|
|
.DESCRIPTION
|
2023-07-29 09:45:37 +02:00
|
|
|
This PowerShell script translates the given text file into another language and writes the output on the console.
|
2022-11-17 20:02:26 +01:00
|
|
|
.PARAMETER File
|
2023-07-29 09:45:37 +02:00
|
|
|
Specifies the path to the file to be translated
|
2022-11-17 20:02:26 +01:00
|
|
|
.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"; }
|
2023-07-29 09:45:37 +02:00
|
|
|
$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
|
2022-11-17 20:02:26 +01:00
|
|
|
return $Result.translatedText
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-07-29 09:45:37 +02:00
|
|
|
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" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
$Lines = Get-Content -path $File
|
|
|
|
foreach($Line in $Lines) {
|
2023-07-29 09:45:37 +02:00
|
|
|
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 }
|
2022-11-17 20:02:26 +01:00
|
|
|
$Result = UseLibreTranslate $Line $SourceLang $TargetLang
|
2023-07-29 09:45:37 +02:00
|
|
|
Write-Output $Result
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
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-file.ps1 as of 05/19/2024 10:25:26)*
|