-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-Translation.ps1
180 lines (141 loc) · 11.5 KB
/
Get-Translation.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function Get-Translation
{
<#
.Synopsis
Translates text
.Description
Translates text, using Bing Translator
.Example
Get-Translation "Start-Automating"
.Example
Get-Translation "Hello World" -To "German"
.Link
Get-Web
.Link
http://bing.com/Translator/
.Notes
It is recommended that you use the default secure setting to store your data market key, AzureDataMarketAccountKey
#>
[CmdletBinding(DefaultParameterSetName='Bing')]
[OutputType([PSObject])]
param(
# The text to be translated
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipelineByPropertyName=$true,
ValueFromPipeline=$true)]
[string]
$Text,
# The language to translate to
[Parameter(
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({
$allCultures = [Globalization.CultureInfo]::GetCultures("AllCultures")
$lang = $_
$matchingCulture = $allCultures | Where-Object { $_.DisplayName -like $lang -or $_.Name -like $lang }
if (-not $matchingCulture) {
throw "Language not found"
}
return $true
})]
[string]
$To= "de",
# The language text will be translated from
[Parameter(
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({
$allCultures = [Globalization.CultureInfo]::GetCultures("AllCultures")
$lang = $_
$matchingCulture = $allCultures | Where-Object { $_.DisplayName -like "$lang*" -or $_.Name -like "$lang*" }
if (-not $matchingCulture) {
throw "Language not found"
}
return $true
})]
[string]
$From= "",
# The Azure Data Market Key Or Setting
[Parameter(ParameterSetName='Bing')]
[string]
$AzureDataMarketSetting = "AzureDataMarketAccountKey",
# The Azure Data Market Key. If this is not provided, then the AzureDataMarketSetting, or it's default value, will be used
[Parameter(ParameterSetName='Bing')]
[string]
$AzureDataMarketAccountKey
)
begin {
Set-StrictMode -Off
Add-Type -AssemblyName System.Web
#region Create a cache for translations if one is not found
if (-not ($script:CachedTranslation)) {
$script:CachedTranslation = @{}
}
#endregion Create a cache for translations if one is not found
}
process {
#region Bing
$allCultures = [Globalization.CultureInfo]::GetCultures("AllCultures")
$realTo = $allCultures |
Where-Object { $_.DisplayName -like "$to*" -or $_.Name -like "$to*" } |
Select-Object -First 1 |
ForEach-Object {
if ($_.Name -like "zh-*") {
$_.Name
} else {
$_.Name.Substring(0, 2)
}
}
$realFrom = if ($From) {
$allCultures |
Where-Object { $_.DisplayName -like "$from*" -or $_.Name -like "$from*" } |
Select-Object -First 1 |
ForEach-Object {
if ($_.Name -like "zh-*") {
$_.Name
} else {
$_.Name.Substring(0, 2)
}
}
} else {
$null
}
$admk = if ($AzureDataMarketAccountKey) {
$AzureDataMarketAccountKey
} else {
if ($script:CachedAzureDataMarketAccountKey) {
$script:CachedAzureDataMarketAccountKey
} else {
Get-SecureSetting -Name $AzureDataMarketSetting -ValueOnly
}
}
$script:CachedAzureDataMarketAccountKey = $admk
$cred = New-Object Management.Automation.PSCredential $admk, (ConvertTo-SecureString -AsPlainText -Force "$admk")
$result =
if ($script:CachedTranslation["${Text}_${RealTo}_${RealFrom}"] -and (-not $Force)) {
$script:CachedTranslation["${Text}_${RealTo}_${RealFrom}"]
} else {
$script:CachedTranslation["${Text}_${RealTo}_${RealFrom}"] = Get-Web -Url "https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?Text=%27$([Web.HttpUtility]::UrlEncode("$Text").Replace('+', '%20'))%27&To=%27$($RealTo)%27$(if ($Realfrom) { "&From=%27$($Realfrom)%27"})" -WebCredential $cred -UseWebRequest
$script:CachedTranslation["${Text}_${RealTo}_${RealFrom}"]
}
if ($result) {
$rx = [xml]$result
$feed= $rx.feed
$entries = foreach($e in $feed.entry) { $e }
foreach ($e in $entries) {
$translation = New-Object PSObject -Property @{
Original = $text;
Translation= $e.content.properties.Text.'#text';
Language = ([Globalization.CultureInfo]$RealTo).DisplayName
From = if ($From) {
([Globalization.CultureInfo]$RealFrom).DisplayName
} else {
"Auto-Detect"
}
}
$translation.pstypenames.clear()
$translation
}
}
#endregion Bing
}
}