SharePoint.Translate.psm1

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Function ConvertTo-AnotherLanguage {
# Copied from this Reddit post and adjusted to work with SharePoint:
# https://www.reddit.com/r/PowerShell/comments/b6n5pr/free_text_translation_using_powershell/
param (
    [Parameter(Mandatory=$false)]
    [String]
    $TargetLanguage = "Spanish", # See list of possible languages in $LanguageHashTable below.

    [Parameter(Mandatory=$false)]
    [String]$apiKey,

    [Parameter(Mandatory=$false)]
    [String]$fromLang = "en",

    [Parameter(Mandatory=$false)] 
    [String]
    $textToConvert = " " # This can either be the text to translate, or the path to a file containing the text to translate
)
# Create a Hashtable containing the full names of languages as keys and the code for that language as values
$LanguageHashTable = @{ 
Afrikaans='af' 
Albanian='sq' 
Arabic='ar' 
Azerbaijani='az' 
Basque='eu' 
Bengali='bn' 
Belarusian='be' 
Bulgarian='bg' 
Catalan='ca' 
'Chinese Simplified'='zh-CN' 
'Chinese Simplified SharePoint'='zh-chs' 
'Chinese Traditional'='zh-TW' 
Croatian='hr' 
Czech='cs' 
Danish='da' 
Dutch='nl' 
English='en' 
Esperanto='eo' 
Estonian='et' 
Filipino='tl' 
Finnish='fi' 
French='fr' 
Galician='gl' 
Georgian='ka' 
German='de' 
Greek='el' 
Gujarati='gu' 
Haitian ='ht' 
Creole='ht' 
Hebrew='iw' 
Hindi='hi' 
Hungarian='hu' 
Icelandic='is' 
Indonesian='id' 
Irish='ga' 
Italian='it' 
Japanese='ja' 
Kannada='kn' 
Korean='ko' 
Latin='la' 
Latvian='lv' 
Lithuanian='lt' 
Macedonian='mk' 
Malay='ms' 
Maltese='mt' 
Norwegian='no' 
Persian='fa' 
Polish='pl' 
Portuguese='pt' 
PortugueseBR='pt-br' 
Romanian='ro' 
Russian='ru' 
Serbian='sr' 
Slovak='sk' 
Slovenian='sl' 
Spanish='es' 
Swahili='sw' 
Swedish='sv' 
Tamil='ta' 
Telugu='te' 
Thai='th' 
Turkish='tr' 
Ukrainian='uk' 
Urdu='ur' 
Vietnamese='vi' 
Welsh='cy' 
Yiddish='yi' 
}
If (!$apiKey) { $apiKey = Get-Secret -Name TranslationAPI -AsPlainText }
# Translation API
$translateBaseURI = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
if (!$fromLang) { $fromLang = "en" }
# Convert from - en = English
# Determine the target language
if ($LanguageHashTable.ContainsKey($TargetLanguage)) {
    $TargetLanguageCode = $LanguageHashTable[$TargetLanguage]
}
elseif ($LanguageHashTable.ContainsValue($TargetLanguage)) {
    $TargetLanguageCode = $TargetLanguage
}
else {
    throw "Unknown target language. Use one of the languages in the `$LanguageHashTable hashtable."
}
# Create a list object to store the finished translation in.

# Adjusts the languages to match SharePoint input unfortunately they dont match with MS Translate endpoints.
if($TargetLanguage -eq 'zh-cns') {$TargetLanguage='zh-CN'}
if($TargetLanguage -eq 'pt-br') {$TargetLanguage='pt'}

$headers = @{}
$headers.Add("Ocp-Apim-Subscription-Key",$apiKey)
$headers.Add("Content-Type","application/json")
$headers.Add("Ocp-Apim-Subscription-Region","eastus2")
# Conversion URI
$convertURI = "$($translateBaseURI)&from=$($fromLang)&to=$($TargetLanguage)"

if ($textToConvert -eq " ") {
$textToConvert = @"
Translates text even even with special characters like " ' ` $ works without issue, though the API may mangle them a bit. Not sure what the limit of line length is, but it seems like you can get away with quite a bit.
It works fine for multiple lines as well.
You can inject code into this block too using the `$(code) format.
Here's the date as an example $(Get-Date).
Due to this you may want to escape the usual Powershell characters, like $ in certain cases like I did above.
If you're just going to have plain text here change the here-string to single quotes instead and anything goes.
Not sure if the API has a rate limit for how much it'll take, but I tried a whole bunch of lines (40+) with
success so at least shorter texts seems to work fine.
"@

}
#elseif (Test-Path $textToConvert -PathType Leaf) {
# $textToConvert = Get-Content $textToConvert -Raw
#}

$text = @{'Text' = $($textToConvert)}
$text = $text | ConvertTo-Json
# Convert
$conversionResult = Invoke-RestMethod -Method POST -Uri $convertURI -Headers $headers -Body "[$($text)]"
#write-host -ForegroundColor 'yellow' "'$($textToConvert)' converted to '$($conversionResult.translations[0].text)'
$Translation=$($conversionResult.translations[0].text)
#$Translation=$Translation.replace('?','') # Test to see if gets better formating when spaces are present.
#$Translation=$Translation.replace('& nbsp;','')
#$Translation=$Translation.replace(' ','')
return $Translation
}


# Translate function
function Start-Translation{
    param(
    [Parameter(Mandatory=$true)]
    [string]$Text,

    [Parameter(Mandatory=$true)]
    [string]$Language,

    [Parameter(Mandatory=$false)]
    [string]$apiKey,
    
    [Parameter(Mandatory=$false)]
    [string]$fromLang='en'

    )
 
    $baseUri = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
 
    if($Language -eq 'zh-cns') {$Language='zh-CN'}
    if($Language -eq 'pt-br') {$Language='pt'}
    
    $headers = @{}
    $headers.Add("Ocp-Apim-Subscription-Key",$apiKey)
    $headers.Add("Content-Type","application/json")
    $headers.Add("Ocp-Apim-Subscription-Region","eastus2")
    # Conversion URI
    $convertURI = "$($baseURI)&from=$($fromLang)&to=$($Language)&textType=html"
 
    # Clean up unsupported characters that return {"error":{"code":400000,"message":"One of the request inputs is not valid."}}
    $text=$text -replace "[^ -x7e]"," "

    # Create JSON array with 1 object for request body
    $textJson = @{
        "Text" = $text
        } | ConvertTo-Json
 
    $body = "[$textJson]"
 
    # Uri for the request includes language code and text type, which is always html for SharePoint text web parts
    #$uri = "$baseUri&to=$language&textType=html"
 
    # Send request for translation and extract translated text
    $results = Invoke-RestMethod -Method Post -Uri $convertURI -Headers $headers -Body $body
    $translatedText = $results[0].translations[0].text
    return $translatedText
}

Function New-SharePointTranslation{
 <#
        .Synopsis
            Translates a SharePoint Page into multiple languages. Must be SP Site Owner and provide credentials interactively.
 
        .DESCRIPTION
            Translates a SharePoint Page into multiple languages. Must be SP Site Owner and provide credentials interactively.
 
        .PARAMETER SharePointSite
            URL for the SharePoint site.
         
        .PARAMETER Languages
            Array of Languages to translate the text to. Source must be in english.
 
         .PARAMETER PageToTranslate
            Name of the SharePoint Page to be translated.
             
         .PARAMETER APIKey
            Azure Translation API Key. Get yours for free at https://azure.microsoft.com/en-us/free/cognitive-services/
 
        .EXAMPLE
            New-SharePointTranslation -SharePointSite https://xxx.sharepoint.com/sites/XYZ -Languages @('es','fr') -Page ThisPage.aspx -APIKey $APIKey
    #>

    [CmdletBinding()]
    param (
           [Parameter(Mandatory=$false)]
           [string]$SharePointSite,

           [Parameter(Mandatory=$false)]
           [array]$Languages,

           [Parameter(Mandatory=$false)]
           [pscredential]$Credential,

           [Parameter(Mandatory=$false)]
           [string]$PageToTranslate=$PageToTranslate,

           [Parameter(Mandatory=$false)]
           [string]$APIKey

    )



# Lets connect to SharePoint - it will prompt for authentication if no Credential was provided.
# Interactive policies where MFA is required with Conditional Access cant use Credential as parameters.
Try{
    If (!$Credential) { $Connection=Connect-PNPOnline -Url $SharePointSite -Interactive }
    Else {$Connection=Connect-PNPOnline -Url $SharePointSite -Credentials $Credential}
    } Catch{
    Write-Warning -Message "Could not connect to SharePoint! Make sure you have owner rights to the Site!"
    Write-Warning -Message "Details: $_"
    Break
}

Try{
$Page = Get-PnPClientSidePage $PageToTranslate # "$targetLanguage/$pageTitle.aspx"
#$textControls = $Page.Controls | Where-Object {$_.Type.Name -eq "ClientSideText"}
$textControls = $Page.Controls | Where-Object {$_.Type.Name -eq "PageText"}
} Catch {
    Write-Warning -Message "Could not find the Page - Aborting"
    Write-Warning -Message "Details: $_"
    Break
}
Write-Host "Translating content..." -NoNewline
 foreach ($Language in $Languages) {
    # Create a temporary copy of the translated version of the page
    Try{
    Copy-PnPFile -SourceUrl "SitePages/$($Language)/$($PageToTranslate)" -TargetUrl "SitePages/tmp-$($Language)-$($PageToTranslate)" -Overwrite -Force -ErrorAction Stop
    $NewPage=Get-PnPClientSidePage "tmp-$($Language)-$($PageToTranslate)"
    } Catch {
       # throw "There is no translation file available! Please go back on the Page and create a new Translation for the selected Language: $($PageToTranslate) "
       Write-Warning -Message "There is no translation file available! Please go back on the Page and create a new Translation for the selected Language: $($Language) "
       Break
    }
    
    # Translate the Title first
    #$translatedTitleText = ConvertTo-AnotherLanguage -TargetLanguage $Language -textToConvert $Page.PageTitle
    $translatedTitleText = Start-Translation -Language $Language -Text $Page.PageTitle -APIKey $APIKey
    $return=Set-PnPPage -Identity $NewPage -Title $translatedTitleText -Name "tmp-$($Language)-$($PageToTranslate)" 

    foreach ($textControl in $textControls){
        #$translatedControlText = Start-Translation -text $textControl.Text -language $targetLanguage
        # Lets clean up some unwanted characters from the text control before translating.
        #$textControl.Text=$textControl.Text.replace('&nbsp;','')
        #$translatedControlText = ConvertTo-AnotherLanguage -TargetLanguage $Language -textToConvert $textControl.Text
        $translatedControlText=Start-Translation -Language $Language -Text $textControl.Text -APIKey $APIKey       
       # $NewPage=Get-PnPClientSidePage "$($Language)-$($PageToTranslate)"

        # Translate the Title first
        #$translatedTitleText = ConvertTo-AnotherLanguage -TargetLanguage $Language -textToConvert $Page.PageTitle
        #Set-PnPPage -Identity $NewPage -Title $translatedTitleText -Name "$($Language)-$($PageToTranslate)"

        #Set-PnPClientSideText -Page $NewPage -InstanceId $textControl.InstanceId -Text $translatedControlText
        $return=Set-PnPPageTextPart -Page $NewPage -InstanceId $textControl.InstanceId -Text $translatedControlText

    }
    # Now that we are done with translation, copy the files a to language folder, and delete temporary file.
    #Pause
    #Copy-PnPFile -SourceUrl "SitePages/$($Language)-$($PageToTranslate)" -TargetUrl "SitePages/$($Language)/$($PageToTranslate)" -Overwrite -Force
    Copy-PnPFile -SourceUrl "SitePages/tmp-$($Language)-$($PageToTranslate)" -TargetUrl "SitePages/$($Language)/$($PageToTranslate)" -Overwrite -Force
    Remove-PnPFile -SiteRelativeUrl  "SitePages/tmp-$($Language)-$($PageToTranslate)" -Force
    
}
Write-Host "Translation completed, please review." 

}