mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-04 22:01:26 +01:00
108 lines
3.1 KiB
Markdown
108 lines
3.1 KiB
Markdown
The *translate-text.ps1* Script
|
|
===========================
|
|
|
|
This PowerShell script translates text into other languages.
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/translate-text.ps1 [[-Text] <String>] [[-SourceLangCode] <String>] [[-TargetLangCode] <String>] [<CommonParameters>]
|
|
|
|
-Text <String>
|
|
Specifies the text to translate
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-SourceLangCode <String>
|
|
|
|
Required? false
|
|
Position? 2
|
|
Default value en
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-TargetLangCode <String>
|
|
|
|
Required? false
|
|
Position? 3
|
|
Default value all
|
|
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-text "Hello World" en all
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Translates text into other languages
|
|
.DESCRIPTION
|
|
This PowerShell script translates text into other languages.
|
|
.PARAMETER Text
|
|
Specifies the text to translate
|
|
.PARAMETER SourceLang
|
|
Specifies the source language (English by default)
|
|
.PARAMETER TargetLang
|
|
Specifies the target language (all by default)
|
|
.EXAMPLE
|
|
PS> ./translate-text "Hello World" en all
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
param([string]$Text = "", [string]$SourceLangCode = "en", [string]$TargetLangCode = "all")
|
|
|
|
function UseLibreTranslate { param([string]$Text, [string]$SourceLangCode, [string]$TargetLangCode)
|
|
$Parameters = @{"q"="$Text"; "source"="$SourceLangCode"; "target"="$TargetLangCode"; }
|
|
$Result = (Invoke-WebRequest -Uri https://libretranslate.de/translate -Method POST -Body ($Parameters|ConvertTo-Json) -ContentType "application/json" -useBasicParsing).content | ConvertFrom-Json
|
|
return $Result.translatedText
|
|
}
|
|
|
|
try {
|
|
if ($Text -eq "" ) { $Text = Read-Host "Enter the text to translate" }
|
|
|
|
if ($TargetLangCode -eq "all") {
|
|
$TargetLangCodes = "ar","de","es","fr","ga","hi","it","ja","ko","pt","ru","zh"
|
|
foreach($TargetLangCode in $TargetLangCodes) {
|
|
$Translation = UseLibreTranslate $Text $SourceLangCode $TargetLangCode
|
|
Write-Output "$($TargetLangCode): $Translation"
|
|
Start-Sleep -seconds 6 # 10 requests maximum per minute
|
|
}
|
|
} else {
|
|
$Translation = UseLibreTranslate $Text $SourceLangCode $TargetLangCode
|
|
Write-Output "$Translation"
|
|
}
|
|
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)*
|