Quantcast
Channel: Brian Pedersen's Sitecore and .NET Blog » language
Viewing all articles
Browse latest Browse all 6

Sitecore Language selector – creating a country selector

$
0
0

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;
        _countries.Add(country);
      }
      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" %>
<div>
  <asp:Repeater runat="server" DataSource="<%# Countries %>" EnableViewState="false">
    <HeaderTemplate>
      <ul>
    </HeaderTemplate>
    <ItemTemplate>
      <li>
        <a href="<%# Eval("Url") %>"><img src="<%# Eval("ImageUrl") %>" alt="<%# Eval("Name") %>" title="<%# Eval("Name") %>" /></a>
      </li>
    </ItemTemplate>
    <FooterTemplate>
      </ul>
    </FooterTemplate>
  </asp:Repeater>
</div>

And the codebehind for the usercontrol is:

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

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

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


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles



Latest Images