Getting the Sitecore Context Language Iso Code
Sitecore’s core functionality comes with the ability to localize content in multiple languages. This is done via the native language versioning of content items. Websites that leverage this multi-lingual capability typically provide a mechanism to switch between languages. Getting the context language that a user has selected is easy in Sitecore but it takes a bit more work to get the Iso code for the language. It’s nice to get the Iso code for the selected language so the language can be switched using Sitecore’s “sc_lang” query string key.
First, get the context language item. This is not merely the context language, but rather the context language definition item located under /sitecore/system/languages.
Use the LanguageManager to get the GUID of the Language item based on the context language. From this, you can get the actual language item from the context database:
ID contextLanguageId = LanguageManager.GetLanguageItemId(Sitecore.Context.Language, Sitecore.Context.Database); Item contextLanguage = Sitecore.Context.Database.GetItem(contextLanguageId);
Next, you can get the “Regional Iso Code” field if one is set for the language. The out-of-the-box English language does not have a “Regional Iso Code” so fallback to the “Iso” field if necessary:
string iso = contextLanguage["Regional Iso Code"];
if (string.IsNullOrEmpty(iso))
{
iso = contextLanguage["Iso"];
}
Now you’ll end up with something like “en” or “es-ES” for English and Spanish (Spain) respectively.
What can you use those Iso codes for? Good question. Sitecore provides a native way to switch the context language. Simply set the Iso code in the query string for the "sc_lang" query string key. This will change the context language. For example, you might want to create a language selector tool which given the languages defined in Sitecore switches the query string with the new Iso code (or Regional Iso Code). You can then use the context language’s Iso code to update your language selector tool to identify the current language.
Got anything to say? Go ahead and leave a comment!
Popular Posts
- Using the DataSource Field with Sitecore Sublayouts
- Tame Your Sitecore Treelists
- Write to a Custom Sitecore Log with log4net
- Sitecore Upgrade Strategy
- Rendering Fully Qualified Sitecore URLs
- Sitecore Admin Pages Explained
- Managing CSS in the Sitecore Media Library
- Use Any() Instead of Count() To See if an IEnumerable Has Any Objects
- Sitecore Front-End Development Best Practices
- Scaling Sitecore Presentation Component Data Sources
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under: