Get Cultures

Get Cultures

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • System.Globalization.CultureInfo
  • System.Globalization.CultureTypes

Cultures C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

System.Globalization.CultureInfo[] cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures);
if (cultures == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("<br/>");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Border, "0");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Cellpadding, "3");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Table);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Thead);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Display Name");
output.RenderEndTag(); //Th
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("ISO");
output.RenderEndTag(); //Th
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Name");
output.RenderEndTag(); //Th
output.RenderEndTag(); //Thead
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tbody);

int i = 0;
var sortedList = (from entry in cultures orderby entry.DisplayName ascending select entry); 
foreach (System.Globalization.CultureInfo culture in sortedList)
{
    i++;
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
    output.Write("{0}", culture.DisplayName);
    output.RenderEndTag(); //Td

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
    output.Write("{0}", culture.ThreeLetterISOLanguageName);
    output.RenderEndTag(); //Td

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
    output.Write("{0}", culture.Name);
    output.RenderEndTag(); //Td

    output.RenderEndTag(); //Tr
    if (i >= 10) break;
}
output.RenderEndTag(); //Tbody
output.RenderEndTag(); //Table
output.RenderEndTag(); //Div


Cultures C# Web Control Output


Display Name ISO Name
Afrikaans (South Africa) afr af-ZA
Albanian (Albania) sqi sq-AL
Alsatian (France) gsw gsw-FR
Amharic (Ethiopia) amh am-ET
Arabic (Algeria) ara ar-DZ
Arabic (Bahrain) ara ar-BH
Arabic (Egypt) ara ar-EG
Arabic (Iraq) ara ar-IQ
Arabic (Jordan) ara ar-JO
Arabic (Kuwait) ara ar-KW

Notes

  • This code uses Linq to obtain the culture field values
  • This code only displays the first ten cultures

Sources