Skip to content

Instantly share code, notes, and snippets.

@AlexKasaku
Last active January 1, 2019 19:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexKasaku/596f336e421d0fc44227fe3088cf6988 to your computer and use it in GitHub Desktop.
Save AlexKasaku/596f336e421d0fc44227fe3088cf6988 to your computer and use it in GitHub Desktop.
Remove a language from an SXA site. Based on the "Add Site Language" set of scripts that comes with SXA.
# Functions Site Language/Remove Site Language
Import-Function Validate-PowerShell
Test-PowerShell
Try
{
$ctx = Get-Item .
Import-Function Remove-SiteLanguage
Import-Function Show-RemoveSiteLanguageDialog
$result = Show-RemoveSiteLanguageDialog $ctx
Remove-SiteLanguage $ctx $result.Language | Out-Null
}
Catch
{
$ErrorRecord=$Error[0]
Write-Log -Log Error $ErrorRecord
Show-Alert "Something went wrong. See SPE logs for more details."
Close-Window
}
# Functions Site Language/Remove-SiteLanguage
function Remove-SiteLanguage {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[Item]$Site,
[Parameter(Mandatory = $true, Position = 1)]
[string]$Language
)
begin {
Write-Verbose "Cmdlet Remove-SiteLanguage - Begin"
}
process {
Write-Verbose "Cmdlet Remove-SiteLanguage - Process"
$Site = Get-Item -Path $Site.Paths.Path -Language $Language
Get-ChildItem -Path $Site.Paths.Path -Recurse | ForEach-Object {
Remove-ItemLanguage -Item $_ -Language $Language
}
}
end {
Write-Verbose "Cmdlet Remove-SiteLanguage - End"
}
}
# Content Editor/Context Menu/Show-RemoveSiteLanguageDialog
# Use a Show Rule to only show for the Site Item template {6669DC16-F106-44B5-96BE-7A31AE82B5B5}
function Show-RemoveSiteLanguageDialog {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[Item]$Site
)
begin {
Write-Verbose "Cmdlet Show-RemoveSiteLanguageDialog - Begin"
}
process {
Write-Verbose "Cmdlet Show-RemoveSiteLanguageDialog - Process"
$languages = [ordered]@{}
$Site.Languages | ? { $Site.Versions.GetLatestVersion($_).Versions.Count -gt 0 } | ForEach-Object {
$displayName = [Sitecore.Globalization.Language]::GetDisplayName($_.CultureInfo)
$languages[$displayName] = $_.Name
} | Out-Null
$result = Read-Variable -Parameters `
@{ Name = "language"; Options = $languages; Value = "en"; Title = "Existing lanuage"; } `
-Description "This script removes a language from an existing site" `
-Title "Remove language from the site" -Width 500 -Height 600 `
-OkButtonName $([Sitecore.Globalization.Translate]::Text("Ok")) -CancelButtonName $([Sitecore.Globalization.Translate]::Text("Cancel")) `
if ($result -ne "ok") {
Close-Window
Exit
}
@{
Language = $language
}
}
end {
Write-Verbose "Cmdlet Show-RemoveSiteLanguageDialog - End"
}
}
@AlexKasaku
Copy link
Author

Note that this script uses the Site item to determine what languages are in the site, so if that particular item does not have a version in a given language, it will not show there. This is the part that determines what languages show in the dialog, if you want to change this behaviour:

$languages = [ordered]@{}

$Site.Languages | ? { $Site.Versions.GetLatestVersion($_).Versions.Count -gt 0 } | ForEach-Object {
            $displayName = [Sitecore.Globalization.Language]::GetDisplayName($_.CultureInfo)
            $languages[$displayName] = $_.Name
        } | Out-Null

@michaellwest
Copy link

Brilliant!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment