Try our conversational search powered by Generative AI!

Øyvind Hognestad
Aug 4, 2009
  14561
(1 votes)

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.

   1: PageDataCollection pages = 
   2: DataFactory.Instance.FindPagesWithCriteria(searchFrom, crits, 
   3: language, LanguageSelector.AutoDetect(true));

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.

   1: public static PageDataCollection FilterForFallbackLanguages(PageReference pageLink, string languageBranch, PageDataCollection result )
   2:        {
   3:            PageLanguageSetting setting = PageLanguageSettingsTree.Instance.Get(pageLink, languageBranch);
   4:  
   5:            if (setting != null)
   6:            {
   7:                List<string> fallback = new List<string>(setting.LanguageBranchFallback);
   8:  
   9:                for (int i = result.Count - 1; i >= 0; i--)
  10:                {
  11:                    if (result[i].LanguageBranch != setting.LanguageBranch && fallback.Contains(result[i].LanguageBranch) == false)
  12:                        result.RemoveAt(i);
  13:                }         
  14:            }
  15:  
  16:            return result;
  17:  
  18:        }

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).

   1: var sorted = from page in pages
   2: orderby page.LanguageID descending, page.PageName ascending 
   3: select page;
   4:  
   5: pageList.DataSource = sorted;
   6: pageList.DataBind();

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.

Aug 04, 2009

Comments

Apr 3, 2017 11:14 AM

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024