Skip to content

Instantly share code, notes, and snippets.

@navancommits
Last active October 8, 2023 22:36
Show Gist options
  • Save navancommits/3a2af4a765247a60158c5ac8f7c5f2c5 to your computer and use it in GitHub Desktop.
Save navancommits/3a2af4a765247a60158c5ac8f7c5f2c5 to your computer and use it in GitHub Desktop.
Convert Sitecore GUI to any of the listed languages in Sitecore Control Panel
#https://navansitecorenotes.blogspot.com/2023/10/powershell-script-for-sitecore-gui.html
#https://stackoverflow.com/questions/13975053/how-to-get-an-escaped-xml-attribute-value-in-powershell-without-it-being-unescap
#https://www.alexandrumarin.com/translate-text-with-powershell-and-google-translate/
#.\TranslateGuiContent.ps1 -file ".\texts.ja-JP.xml" -destfileLocation . -langCode 'ta-IN' -targetLanguage 'ta'
param(
[string]$File = ".\texts.ja-JP.xml",
[string]$DestfileLocation = ".",
[string]$LangCode = "af-ZA",
[string]$TargetLanguage = "af"
)
$destfilePath=$destfileLocation + "\texts." + $LangCode + ".xml"
[XML]$xmlfile = Get-Content $File
$startTime=(Get-Date)
$concatenatedString+="<?xml version=`"1.0`" encoding=`"utf-8`"?>`n"
$concatenatedString+= "<sitecore>`n"
For ($i=0; $i -lt $xmlfile.sitecore.phrase.Count; $i++)
{
$encodedText=$xmlfile.sitecore.phrase[$i].attributes['key'].OuterXml.Split("=")[1]
$array=$xmlfile.sitecore.phrase[$i].attributes['key'].OuterXml -split '='
$actualText=($array[1..($array.Length -1)] -join "=") #without this, if there are multiple = in the string, the string was truncated
$Text=$xmlfile.sitecore.phrase[$i].key
$Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$Text"
$Response = Invoke-RestMethod -Uri $Uri -Method Get
$translatedText = $Response[0].SyncRoot | foreach { $_[0] }
$translatedText=$translatedText.Replace("<","&lt;") #to overcome decoding in case of these two signs
$translatedText=$translatedText.Replace(">","&gt;") #to overcome decoding in case of these two signs
$concatenatedString+= "`t<phrase key=" + $actualText + ">`n"
$concatenatedString+= "`t`t<" + $LangCode + ">" + $translatedText + "</" + $LangCode + ">`n"
$concatenatedString+= "`t</phrase>`n"
write-host "Node number: " $i " out of " $xmlfile.sitecore.phrase.Count
}
$concatenatedString+= "</sitecore>`n"
$concatenatedString | Out-File -FilePath $DestfilePath -Encoding utf8
$endTime=(Get-Date)
$ElapsedTime = (($endTime-$startTime).TotalSeconds)
Write-Host "Execution time (seconds): " $ElapsedTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment