Developer FAQ
I would like my pages to display links to other language versions of the current page. How can I detect if a page has been translated to other languages and if so, how do I get the URLs?
Answer:
You can use the C# function shown below - it will provide your XSLT with input looking like this:
<in:result name="GetForeignPageInfo" xmlns:in="http://www.composite.net/ns/transformation/input/1.0"> <LanguageVersion Culture="da-DK" CurrentCulture="false" Id="5f418576-076e-48f5-874c-52d1644c347e" Title="Solskin" MenuTitle="Solskin" UrlTitle="Solskin" Abstract="Danskere er flittige!" URL="/Solskin.aspx?dataScope=administrated" /> <LanguageVersion Culture="en-US" CurrentCulture="true" Id="5f418576-076e-48f5-874c-52d1644c347e" Title="Sunny" MenuTitle="Sunny" UrlTitle="Sunny" Abstract="" URL="/en-US/Sunny.aspx?dataScope=administrated" /> <LanguageVersion Culture="nl-NL" CurrentCulture="false" Id="5f418576-076e-48f5-874c-52d1644c347e" Title="Zonning" MenuTitle="Zonning" UrlTitle="Zonning" Abstract="" URL="/nl-NL/Zonning.aspx?dataScope=administrated" /> </in:result>
Here is the C# code that builds the LanguageVersion
elements shown above:
using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading; using System.Xml.Linq; using Composite.Data; using Composite.Renderings.Page; /// <summary> /// Summary description for PageLanguageVersions /// </summary> public class PageLanguageVersions { public static IEnumerable<XElement> GetForeignPageInfo() { // Grab all active languages... foreach (CultureInfo culture in DataLocalizationFacade.ActiveLocalizationCultures) { // enter the 'data scope' of the next language using (var scope = new DataScope(culture)) { // fetch sitemap element for current page - if any var match = PageStructureInfo.GetSiteMap().DescendantsAndSelf().Where(f => f.Attribute("Id") != null && f.Attribute("Id").Value == PageRenderer.CurrentPageId.ToString()).FirstOrDefault(); if (match != null) { var annotatedMatch = new XElement("LanguageVersion" , new XAttribute("Culture", culture.Name) , new XAttribute("CurrentCulture", culture.Equals(Thread.CurrentThread.CurrentCulture)) , new XAttribute("Id", match.Attribute("Id").Value) , new XAttribute("Title", match.Attribute("Title").Value) , (match.Attribute("MenuTitle") == null ? null : new XAttribute("MenuTitle", match.Attribute("MenuTitle").Value)) , new XAttribute("UrlTitle", match.Attribute("UrlTitle").Value) , new XAttribute("Abstract", match.Attribute("Abstract").Value) , new XAttribute("URL", match.Attribute("URL").Value) ); yield return annotatedMatch; } } } } }
To install the function execute the following steps:
- Copy the C# file to your
~/App_Code
directory. - In the Functions perspective, add a new C# Function
- Type = "PageLanguageVersions", Next
- Method Name = "GetForeignPageInfo", Next
- Namespace Name = "DevUtils", Finish
At this point you can call the C# Function from your XSLT Function using the name "DevUtils.GetForeignPageInfo" and get XML with title and URL info about translated versions of the current page.