NReed.Dev

Sitecore | Optimizely | .NET

Using Sitecore Powershell Extensions to Automate Language Versioning

In another post I showed how I used Sitecore Powershell Extensions (SPE) to automate translation of content. This is a fair bit simpler.

Here we will simply automate the creation of language versions. You may have noticed that when you create a language version that it copies over the data from the original language. For some of the versions of English or Spanish this can be incredibly helpful, as content may not significantly change at all. So let’s look at the script that will speed up this process for you or your content authors.

PowerShell
function Create-LanguageVersions {
    param (
        [Sitecore.Data.Items.Item]$item
    )
    # Check if item is a media item or of a template of your choice
    #{F1828A2C-7E5D-4BBD-98CA-320474871548} == Image
    #{DAF085E8-602E-43A6-8299-038FF171349F} == Jpeg
    if ($item.TemplateId -eq "{F1828A2C-7E5D-4BBD-98CA-320474871548}" || $item.TemplateId -eq "{DAF085E8-602E-43A6-8299-038FF171349F}") {
        # Iterate through target languages
        foreach ($lang in $targetLanguages) {
            # Check if language version already exists
            $languageItem = $item.Versions | Where-Object { $_.Language.Name -eq $lang }
            if ($languageItem -eq $null) {
                # Create language version
                $languageItem = Add-ItemVersion -Item $item -TargetLanguage $lang -IfExist Skip
            }
        }
    }
    # Recursively process child items
    foreach ($child in $item.Children) {
        Create-LanguageVersions-AltText -item $child
    }
}
function Process-MediaLibraryItems {
    param (
        [Sitecore.Data.Items.Item]$parentItem
    )
    # Process current item
    Create-LanguageVersions -item $parentItem
$descendants = Get-ChildItem -Path $parentItem.Paths.FullPath -Recurse
    # Recursively process child items
    foreach ($child in $descendants) {
        Create-LanguageVersions -item $child
    }
}
$contentPath = "/sitecore/content-path"
$targetLanguages = @("fr-CA, fr-FR") # Add target languages as needed
$contentRoot = Get-Item -Path $contentPath
Process-MediaLibraryItems -parentItem $contentRoot

Leave a Reply

Your email address will not be published. Required fields are marked *