Translating Sitecore items’ field values into different language with PowerShell and Translate API

Recently, we encountered a situation where we needed to translate multiple Sitecore field values across numerous items into different languages. Manually handling each item and field was both time-consuming and complex—especially since actual translation was required, not just copying values between language versions.

We explored various approaches to streamline the process. While AI-based solutions seemed promising, but client side Compliances for AI usage ruled out that option.

Another practical alternative was to use spreadsheets like Excel or CSV files containing translated content and then import those values into Sitecore using PowerShell scripts.

During our exploration, we discovered a freely accessible translation API that doesn't require an API key. By leveraging this in our PowerShell script, we were able to automate the translation process and directly apply the translated content to the appropriate language versions of the Sitecore items—saving both time and effort.

Below PowerShell script does the same in mentioned steps:

  • Set item path and languages, and get Source and Target language version item
  • If Target language version item does not exist, then create new version in target language
  • Perform the translation for all Sitecore fields in the specified item

# Set Item Path & Languages
$itemPath = "/sitecore/content/Home/Demo/DemoItem1"
$sourceLanguage = "en"
$targetLanguage = "ar-AE" 

#Function to translate content using Google Translate API (No API Key required)
function translateTextValue {
    param (
        [string]$text,
        [string]$targetLang
    )
    $encodedText = [uri]::EscapeDataString($text)
    $url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$targetLang&dt=t&q=$encodedText"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get
        return ($response[0][0][0])
    } catch {
        Write-Warning "Failed to translate: $text"
        return $text
    }
}

#Get Source and Target language version item
$sourceLang = [Sitecore.Globalization.Language]::Parse($sourceLanguage)
$targetLang = [Sitecore.Globalization.Language]::Parse($targetLanguage)
$sourceItem = Get-Item -Path $itemPath -Language $sourceLang

#Check if the Source language version item exists
if ($sourceItem -eq $null) {
    Write-Host "Item not found at path: $itemPath for language $sourceLanguage"
    return
}

#Get Target language version item
$targetItem = Get-Item -Path $itemPath -Language $targetLang

# If the target language version does not exist, create one
if (-not $targetItem -or $targetItem.Versions.Count -eq 0) {
    # Switch context to the target language
    $itemInTargetLang = $sourceItem.Database.GetItem($sourceItem.ID, $targetLang)
    if ($itemInTargetLang -eq $null) {
        Write-Host "Unable to get item in target language context"
        return
    }
    # Add a version in the target language
    $targetItem = $itemInTargetLang.Versions.AddVersion()
	Write-Host "Language version '$targetLanguage' created successfully."
}

# Start Editing block 
$targetItem.Editing.BeginEdit()
foreach ($field in $sourceItem.Fields) {
if (-not $field.ReadOnly -and -not $field.Name.StartsWith("__")) {
    $valueToTranslate = $field.Value
        if ($valueToTranslate -and $valueToTranslate.Trim().Length -gt 0) {
            $translatedValue = translateTextValue -text $valueToTranslate -targetLang $targetLanguage
            $targetItem.Fields[$field.ID].Value = $translatedValue
        }
    }
}
$targetItem.Editing.EndEdit()
#Editing block End

Write-Host "Language version '$targetLanguage' translated successfully."

Sitecore Item with "en" language version

A screenshot of a computer

AI-generated content may be incorrect.

Sitecore Item with "ar-AE" language version before script execution

A screenshot of a computer

AI-generated content may be incorrect.

Script Execution

A screenshot of a computer

AI-generated content may be incorrect.

















Sitecore Item with "ar-AE" language version after script execution

A screenshot of a computer

AI-generated content may be incorrect.

This script helps us to

  • Automatically translate field values
  • Write them in the correct language version
  • Save time and reduce manual efforts

The reason to share this is that there are times that anyone else might also face some compliance issues for clients and hope this helps

Stay connected for more updates. We will see some more useful PowerShell scripts in the next blogs.

Thank you!

Comments

Popular posts from this blog

Setting Up Sitecore Headless with Next.js in Disconnected Mode

Create Sitecore Ribbon Button using Sitecore PowerShell Extensions