Adding Custom Language in Sitecore

We have a set of language cultures defined in .NET & Sitecore, but frequently we would need to create custom language culture that isn’t provided. For example we might need to use this culture identifier in the url. (Eg: www.mysitecoresite.com/en-GB/).

When we try to add a custom language culture (Eg: en-IC, where IC stands for IndoChina) we would get an error:

‘The name “en-IC” is not a valid or supported culture identifier’.

In this scenario, we would usually do a work around by updating the Language Resolver, but this is actually not required. Before adding a new language in Sitecore, we have to register the custom culture in .NET using the CultureAndRegionInfoBuilder API. We can create CultureInfo & RegionInfo with an existing culture and update with required custom details.

Here is the code sample,

string culture = "en-IC";
string name = "English (IndoChina)";
 
//Create Culture & Region Info with an existing Language Culture (Eg: en-GB)
CultureInfo cultureInfo = new CultureInfo("en-GB");
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);
CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder(culture, CultureAndRegionModifiers.None);
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(cultureInfo);
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(regionInfo);
cultureAndRegionInfoBuilder.CultureEnglishName = cultureAndRegionInfoBuilder.CultureNativeName = name;
                 
//Register the Custom Language Culture
cultureAndRegionInfoBuilder.Register();

But the problem doesn’t end here. When we register a new culture,

An NLP file will be created in the system’s Globalization folder
An entry will be added to the Registry under HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Nls\
By default the IIS User will not have access to these paths, and we will get an Unauthorized Access Exception.

We will have to ensure that IIS User has appropriate privileges to access these paths before calling Register() method. So we have to assign Read & Write permissions to the \Windows\Globalization folder & HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Nls\ in RegistryEditor.

If the Owner of the folder is “TrustedInstaller”, we will not be able to edit permissions. In such scenario, we will have to change the owner before giving access to the folder.

We can also give permissions programmatically using RegistrySecurity and DirectorySecurity APIs.

Finally we need to add the language in LanguageDefinitions.config,

<language id="en" region="IC" codepage="65001" encoding="utf-8" charset="iso-8859-1" icon="Flags/24x24/flag_generic.png"></language>

Now the custom language is added to the Sitecore’s Predefined Language list, we can now add the custom language in Sitecore using ‘Add Language Wizard’.

In this way we can have some minor improvement in performance which will not be the case while using Language Resolver, since Language Resolver will be executed at least once for each and every request.

Originally Posted at:  https://www.nttdatasitecore.com/Blog/2016/September/Adding-Custom-Language-in-Sitecore

Leave a Reply

Your email address will not be published. Required fields are marked *