HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Configure a custom localization provider

Describes how to configure the initialization of localization providers used by the default LocalizationService in Optimizely Content Management System (CMS).

The way to configure localization is through LocalizationOptions or through extension methods AddLocalization or AddEmbeddedLocalization to IServiceCollection during website initialization.

The following sections show how to achieve this.

📘

Note

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

You can register localization providers during the configuration phase of the site initialization from the Startup class using IServiceCollection. Or, you can create an initialization module that implements the IConfigurableModule interface and handle the registration in the ConfigureContainer method; by configuring the LocalizationOptions class or through the AddLocalizationProvider extension method available on the IServiceCollection interface.

The following example shows how to register a localization provider from method ConfigureServices in the Startup class.

services.AddLocalizationProvider<FileXmlLocalizationProvider,NameValueCollection>(o => {
    o[FileXmlLocalizationProvider.PhysicalPathKey] = @ "c:\temp\resourceFolder";
  });

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

The following example demonstrates adding a localization provider that reads 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

You can also directly modify LocalizationService through the API of the ProviderBasedLocalizationService, but you should use another method because this API is likely to be deprecated in a future version.