Last updated: Dec 18 2017

Area: Episerver CMS Applies to versions: 11.3 and higher
Other versions:

Configuring a localization provider

This topic explains how to configure the initialization of localization providers used by the default LocalizationService in Episerver CMS. There are two recommended ways of configuring them.

  • Modify the <localization> element in the <episerver.framework> section of your web.config.
  • Configure the LocalizationOptions class through code during website initialization.

The following sections show how to achieve this.

Note: A large number of providers increases the time needed to find strings. So, for best performance, keep the number of localization providers to a minimal.

Using the configuration method

You can register a localization provider by adding it to the <providers> element under the <localization> element in the <episerver.framework> section. If you want to add a provider that reads XML language documents from a folder on disk, you can do this by adding a provider of the type EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider and provide it with the path to the folder. This can either be a physical path using the physicalPath attribute, or a virtual path using the virtualPath attribute. You might be required to grant access to the folder to the IIS application pool user. For details about the attributes available with the <localization> element, see the EPiServer.Framework.Configuration.LocalizationElement class.

The following example configuration demonstrates this method.

<episerver.framework>
  ...
  <localization>
    <providers>
      <add physicalPath="c:\temp\resourceFolder"
           name="customResources"
           type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider, EPiServer.Framework.AspNet" />
    </providers>
  </localization>
  ...
</episerver.framework>

Using the code method

Localization providers can also be registered during the configuration phase of the site initialization. This can be done by creating an initialization module that implements the IConfigurableModule interface and handle the registration in the ConfigureContainer method either by configuring the LocalizationOptions class or through the AddLocalizationProvider extension method available on the context.Services property.

The following example shows how to create an module that registers a similar provider as in the web.config version above.

using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.Localization;
using EPiServer.Framework.Localization.XmlResources;

namespace CodeSamples
{
    [InitializableModule]
    [ModuleDependency(typeof(FrameworkInitialization))]
    public class CustomLocalizationProviderInitialization : IConfigurableModule
    {
public void ConfigureContainer(ServiceConfigurationContext context) {
context.Services.AddLocalizationProvider<FileXmlLocalizationProvider, NameValueCollection>(o =>
{
o[FileXmlLocalizationProvider.PhysicalPathKey] = @"c:\temp\resourceFolder";
});
} public void Initialize(InitializationEngine context) { } public void Uninitialize(InitializationEngine context) { } } }

Episerver also includes a provider for reading localization resources from embedded XML files, called EPiServer.Framework.Localization.XmlResources.EmbeddedXmlLocalizationProviderInitializer.

The following example demonstrates how to add a localization provider that reads all embedded XML localization files from an assembly.

using System;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.Localization;

namespace CodeSamples
{
    [InitializableModule]
    [ModuleDependency(typeof(FrameworkInitialization))]
    public class CustomLocalizationProviderInitialization : IConfigurableModule
    {
public void ConfigureContainer(ServiceConfigurationContext context) {
// ClassInMyAssembly can be any class in the Assembly where the resources are embedded
context.Services.AddEmbeddedLocalization<ClassInMyAssembly>();
} public void Initialize(InitializationEngine context) { } public void Uninitialize(InitializationEngine context) { } } }

ProviderBasedLocalizationService

Note: It is also possible to modify the LocalizationService directly through the API of the ProviderBasedLocalizationService, but this method is no longer recommended and this API is likely to be deprecated in a future version.

Comments