Kentico 9 - How to define language in <html>

Henry Kwan asked on May 4, 2016 02:22

I am using Kentico Multi-Language function in one of my project, but it not define the language in

Correct Answer

Brenden Kehren answered on May 4, 2016 02:38

In the master page layout I place this code:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    if (CurrentDocument != null)
    {          
        CMS.UIControls.ContentPage page= this.Page as CMS.UIControls.ContentPage;
        if (page != null)
        {
            string lang= CMS.Localization.LocalizationContext.CurrentCulture.CultureCode;
            page.XmlNamespace += " lang=\"" + lang + "\"";
        }
    }
}
</script>
5 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on May 4, 2016 11:20

Hi Henry,

Though from today I would also prefer the way Brenden mentioned but here is a quick and dirty way to do this which I use to set multiple tags using Javascript.

   (function() {
       document.getElementsByTagName('html')[0].setAttribute('lang', 'en');
       document.getElementsByTagName('html')[0].setAttribute('xml:lang','en');
       document.getElementsByTagName('html')[0].setAttribute('xmlns:fb','http://ogp.me/ns/fb#');
   })(); 

The first two statements is answer to your question.

I have kept third in case you want to add library to include open graph tags on your website.

By using this simple pure javascript code you may add as many tags you want.

You can put this code in a HEAD HTML webpart. Since it's purely in Javascript it doesn't require need for any other library to be loaded first.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 12, 2016 18:01 (last edited on October 12, 2016 18:04)

Good solution except you need to get the language code not a culture code:

        System.Globalization.CultureInfo c = new System.Globalization.CultureInfo(CMS.Localization.LocalizationContext.CurrentCulture.CultureCode);
        page.XmlNamespace += " lang=\"" + c.TwoLetterISOLanguageName + "\"";
2 votesVote for this answer Mark as a Correct answer

Shaun Butler answered on March 16, 2017 10:56

I have gone a different way and created a resource string which displays the language and then added the localize string to the PortalTemplate.aspx HTML tag.

Image Text

<html <%= ResHelper.LocalizeString("{$HTML.Language$}") %>>

2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.