HTML ISO Language Code for Sitecore

Here’s a short tip/suggestion for a Friday.  When building sites that support multiple languages, it’s good practice to give a hint to the rest of the world which language your current page is using.  The W3C recommendation is to include a ISO language code in the HTML tag at the top of your page.

<html lang="en">

But if we’re dealing with Sitecore, that situation could change from item to item, version to version.  If you’re using MVC Razor for your layouts, the default model passed in is Sitecore.Mvc.Presentation.RenderingModel.  I know this because of several blog posts you can find on Google.  I sure wish there was documentation to that effect but oh well.  In any event, that class has a property Item that is the current context item, ie the page and language that the user requested.  With that, I can drill down through the Language property.

<html lang="@Model.Item.Language.CultureInfo.TwoLetterISOLanguageName">

If you’re writing your own model, you can get to the same place by using Sitecore.Mvc.Presentation.RenderingContext.Current.Rendering.Item.

Even in cases where I’m dealing with a site that will only ever feasibly have one language, this is an easy step and can part of a boiler plate layout that you use on any project.

Leave a Reply