Globalization, categories and sorting using LINQ
I have been working with a globalized website for some time and have a couple of tips to share regarding globalization.
I’m using FindPagesWithCriteria to get pages based on categories defined on pages. As we teach during the EPiServer developer course FindPagesWithCriteria uses queries directly to the database. The method has got a performance boost in EPiServer CMS5, but should never be used on a typical landing page without caching.
Categories are quite useful if you do not need the complete Topic Maps structure. Anders Hattestad has written a nice post on how to create a custom property to display a part of the categorytree.
Well, back to the topic. Using FindPagesWithCriteria will not return the pages as they are displayed in editmode with fallback and replacement languages (which I hopefully thought). After some testing I found that the best way to get the pages I needed was to use LanguageSelector.AutoDetect(true) as the last parameter.
The pages collection will now also contain pages not translated to the current language. So, with a little help from reflector I removed the unwanted pages like this.
Then my collection was complete. The last step was to sort the collection so the pages of the origin language – Norwegian – came first in the list and the fallback language – English - at the bottom of the list. In addition the sorting inside the language should be PageName. I could write my own comparer or my own filter but a better, and much easier solution, is to use LINQ (thanks Ahsan at support who suggested this solution to a partner in a post I came across while surfing around).
To get this to work I needed to change the webcontrol which render this list from EPiServer:PageList to Asp:Repeater. EPiServer:PageList do not support this and you will get an error like this if you try.
System.NotSupportedException: Specified method is not supported.
After changing the webcontrol from EPiServer:PageList to Asp:Repeater, remember to use the FilterForVisitor.Filter(pages); to set the right security and remove the unpublished pages.
Globalization is cool, it works like a charm in most cases, but be aware of that the time used to develop could take a little longer than you think. Perhaps more important – put some effort when testing the site after the content is in place and real fallback and replacement language is set.