Multilanguage website could have many languages

Global companies often use dozens of languages in sitecore project.

Websites often use different set of languages per project

As new sites are added to sitecore in multisite environment. It is more probable that each site will use its own set of languages. The reason could be that it is some product site for product od service offered only in some countries, or the just the content is not ready yet to go live.

New domain requires configuration changes

The service or product websites often have its own domain, therefore additional site configuration, IIS, DNS is needed. You could just prepare the configuration in advance and have some configuration in sitecore and when the Release goes live you publish the configuration that allows a new language.

… Or you can customize the site configuration like we did.

New attribute availableLanguages in site configuration

We chose the configuration adjustment, so we can exactly control when the language fo a site goes public. For this we added a new attribute availableLanguages to site configuration. So there is no need to change and publish anything from sitecore when a new language goes live.

Of course sitecore does not know such attribute, but it is easy to add an extension method to extend the SiteContext.

<site name="swissproductsample" patch:before="site[@name='website']"
          hostName="swissproductsample"
          targetHostName="swissproductsample"
          language="en-GB"
          availableLanguages="de-CH,fr-CH"
          rootPath="/sitecore/content/swissproductsample"
          notFoundItem = "page-not-found"
          startItem="/website"
          urlLanguageEmbedding ="always"
          customCache ="true"
          inherits="website"/>

SiteContext Decorator source code

The code you see was simplified for clarity. What is does is. It reads the availableLanguages attribute and returns the list of language objects. This way you can easily go through all available languages.

For instance when you‘ve got a multi-language site running on one domain and you want to generate robots.txt file you can iterate through all available language to add links to their sitemap.xml files.

Or the most common use would be in the language switcher component, so it allows only languages specified in the site configuration.

public static List<Language> AvailableLanguages(this SiteContext site)
{
    Assert.IsNotNull(site, "Site cannot be null");
    var result = new List<Language>();
    if (!site.Properties.AllKeys.Contains("availableLanguages"))
    {
        return result;
    }

    var availableLanguages = site.Properties["availableLanguages"];
    var languages = availableLanguages.Split(',');
    foreach (var languageName in languages)
    {
        Language language;
        if (Language.TryParse(languageName, out language))
        {
            result.Add(language);
        }
    }
    return result;
}