forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-text.ps1
executable file
·46 lines (42 loc) · 1.61 KB
/
translate-text.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
.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
}