2022-12-04 10:40:18 +01:00
|
|
|
## The *translate-file.ps1* Script
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script translates a text file into another language.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
```powershell
|
2023-05-26 12:20:18 +02:00
|
|
|
/home/mf/Repos/PowerShell/Scripts/translate-file.ps1 [[-File] <String>] [[-SourceLang] <String>] [[-TargetLang] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-File <String>
|
|
|
|
Specifies the file to translate
|
|
|
|
|
|
|
|
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
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## 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
|
|
|
|
Translates a text file into another language
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script translates a text file into another language.
|
|
|
|
.PARAMETER File
|
|
|
|
Specifies the file to translate
|
|
|
|
.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.com/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json").content | ConvertFrom-Json
|
2023-05-26 12:20:18 +02:00
|
|
|
Start-Sleep -milliseconds 3000 # 20 requests per minute maximum
|
2022-11-17 20:02:26 +01:00
|
|
|
return $Result.translatedText
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($File -eq "" ) { $File = read-host "Enter path to file" }
|
|
|
|
if ($SourceLang -eq "" ) { $SourceLang = read-host "Enter language used in this file" }
|
|
|
|
if ($TargetLang -eq "" ) { $TargetLang = read-host "Enter language to translate to" }
|
|
|
|
|
|
|
|
$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
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2021-11-08 21:36:42 +01:00
|
|
|
*Generated by convert-ps2md.ps1 using the comment-based help of translate-file.ps1*
|