Sharepoint translation PowerShell script

Kamil Buszmann 5 Reputation points
2023-06-12T08:11:19.4233333+00:00

Hi, I found a solution that can translate SharePoint Page. It works for English but I want to make it to work with Polish. Problem is with coding polish caracters. If there are polish characters it returns ?? for each character and translation doesn't work. Can you help me with this coding ? If i change "ASCII.GetString($enc)" to "UTF8.GetString($enc)" it returns empty array.

PS Code

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

#Install-Module "PNP.PowerShell" -AcceptLicense -Force -Verbose -Scope "CurrentUser"
Import-Module "PNP.PowerShell"

# Comments
# https://michalsacewicz.com/automatically-translate-news-on-multilingual-sharepoint-sites/
# https://www.youtube.com/watch?v=plS_1BsQAto&t=457s

# Sample
$req = @'
{
    "siteURL": "https://spjeff.sharepoint.com/sites/Portal",
    "language": "es",
    "pageTitle": "Contoso"
}
'@

# POST method: $req
#DEBUG $Request   = $req | ConvertFrom-Json
$clientId = ""
$clientSecret = ""
 
# Interact with body of the request
$siteURL = $Request.Body.siteURL
$language = $Request.Body.language
$pageTitle = $Request.Body.pageTitle
 
# Translate function
function Start-Translation {
    param(
        [Parameter(Mandatory = $true)]
        [string]$text,
        [Parameter(Mandatory = $true)]
        [string]$language
    )
 
    # Config
    $baseUri = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
    $headers = @{
        'Ocp-Apim-Subscription-Key'    = ''
        'Ocp-Apim-Subscription-Region' = 'westus'
        'Content-Type'                 = 'application/json'
    }
 
    # Encoding clean
    $enc = [System.Text.Encoding]::UTF8.GetBytes($text)
    $text = [System.Text.Encoding]::ASCII.GetString($enc) | Out-String

    # 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&from=en&to=$language&textType=html"
 
    # Send request for translation and extract translated text
    $results = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $body
    $translatedText = $results[0].translations[0].text
    return $translatedText
}
 
#---START SCRIPT---#
Connect-PnPOnline $siteURL -ClientId $clientId -ClientSecret $clientSecret -WarningAction SilentlyContinue

$newPage = Get-PnPClientSidePage "$language/$pageTitle.aspx"
$newPage
$newPage.Controls | Ft -a
$newPage.Controls | select Type | Ft -a
$textControls = $newPage.Controls | Where-Object { $_.Type.Name -eq "ClientSideText" -or $_.Type.Name -eq "PageText" }
$textControls
 
Write-Host "Translating content..." -NoNewline

foreach ($textControl in $textControls) {
    $translatedControlText = Start-Translation -text $textControl.Text -language $language
    $translatedControlText 
    Set-PnPClientSideText -Page $newPage -InstanceId $textControl.InstanceId -Text $translatedControlText
}
    
Write-Host "Done!" -ForegroundColor "Green"
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-06-13T02:30:26.2466667+00:00

    Hi @Kamil Buszmann

    If you want to Encode a string in UTF-8, you could use following powershell script.

    $encodetxt = [System.Text.Encoding]::UTF8.GetBytes($text)
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.