Sitecore Language selector – creating a country selector

This is a little nifty thing I developed recently. I developed a small UserControl that displays all the flags from the languages in Sitecore, enabling me to create a list of clickable flags that will make the website switch language. The control is nice because I do not have to modify my code if the website gets another language.

The user defines the languages of the website here:

Language in Sitecore

Language in Sitecore

And each language is defined in the web.config as a seperate website:

Sites in Sitecore web.config

Sites in Sitecore web.config

Each language from Sitecore is displayed in the control:

Country Selector

Country Selector

The first thing I need is a class containing the country name, url to the flag and Url to the page in the current language:

public interface ICountry
  string Name { get; }
  string ImageUrl { get; }
  string Url { get; }

internal class Country : ICountry
  public string Name { get; set; }
  public string ImageUrl { get; set; }
  public string Url { get; set; }

Then I need a repository from where I can get all languages from Sitecore as ICountry elements:

using System.Collections.Generic;
using System.Linq;
using PT.Country.Model;
using Sitecore.Collections;
using Sitecore.Globalization;
using Sitecore.Sites;
using Sitecore.Web;

namespace PT.Country.Infrastructure
  internal class CountryRepository
    public IEnumerable<ICountry> GetCountries(SiteContext context)
      List<ICountry> _countries = new List<ICountry>();
      LanguageCollection languages = context.Database.GetLanguages();
      foreach (Language language in languages)
        Country country = new Country();
        country.Name = language.CultureInfo.DisplayName;
        country.ImageUrl = "/~/icon/" + language.GetIcon(context.Database);
        country.Url = "http://" + GetSite(language.Name).HostName;
      return _countries;

    private SiteInfo GetSite(string language)
      return SiteContextFactory.Sites.First(s => s.Language == language && s.HostName != string.Empty);

Please note that your website needs to run in integrated pipeline mode otherwise the icon url will fail. My repository assumes that this is a multisite solution, and each language has a Site defined in the web.config.

That was it. Now all I have to do is to create a UserControl containing a repeater that enumerates the flags on screen:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CountrySelector.ascx.cs" Inherits="PT.Country.Presentation.CountrySelector" %>
  <asp:Repeater runat="server" DataSource="<%# Countries %>" EnableViewState="false">
        <a href="<%# Eval("Url") %>"><img src="<%# Eval("ImageUrl") %>" alt="<%# Eval("Name") %>" title="<%# Eval("Name") %>" /></a>

And the codebehind for the usercontrol is:

using System;
using System.Collections.Generic;
using PT.Country.Infrastructure;
using PT.Country.Model;</pre>

namespace PT.Country.Presentation
  public partial class CountrySelector : System.Web.UI.UserControl
    protected void Page_Load(object sender, EventArgs e)

    protected IEnumerable<ICountry> Countries
        CountryRepository countries = new CountryRepository();
        return countries.GetCountries(Sitecore.Context.Site);

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, General .NET, Sitecore 6 and tagged , , , . Bookmark the permalink.

2 Responses to Sitecore Language selector – creating a country selector

  1. Brian,
    I need to do this exact thing in our website. Specific sections need to be in Spanish with a presentation flag context switcher. Will this code still be viable in Sitecore 7.2? Thank you very Much


  2. briancaos says:

    Yes I believe it will still work in 7.2


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.