Class ProviderBasedLocalizationService
Provider based implementation of LocalizationService. Uses LocalizationProviders to load localized resources.
Inherited Members
Namespace: EPiServer.Framework.Localization
Assembly: EPiServer.Framework.dll
Version: 10.10.4Syntax
[ServiceConfiguration(typeof(LocalizationService), Lifecycle = ServiceInstanceScope.Singleton, FactoryMember = "CreateLocalizationService")]
[InitializableModule]
public class ProviderBasedLocalizationService : LocalizationService, IConfigurableModule, IInitializableModule
Examples
How to register a provider in web.config.
<episerver.framework>
...
<localization fallbackBehavior="FallbackCulture, MissingMessage, Echo" fallbackCulture="en">
<providers>
<add physicalPath="c:\temp\resourceFolder"
name="customResources"
type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider" />
</providers>
</localization>
...
</episerver.framework>
How to register a provider using a physical path in a IInitializableModule.
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 CustomLanguageProviderInitializationWithPhysicalPath : IInitializableModule
{
private const string ProviderName = "customResources";
public void Initialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
string langFolder = @"c:\temp\resourceFolder";
if (Directory.Exists(langFolder))
{
NameValueCollection configValues = new NameValueCollection();
//This config value tells the provider where to find XML language documents.
configValues.Add(FileXmlLocalizationProvider.PhysicalPathKey, langFolder);
FileXmlLocalizationProvider localizationProvider = new FileXmlLocalizationProvider();
//Instanciates the provider
localizationProvider.Initialize(ProviderName, configValues);
//Adds it at the end of the list of providers.
localizationService.Providers.Add(localizationProvider);
}
}
}
public void Uninitialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
//Gets any provider that has the same name as the one initialized.
LocalizationProvider localizationProvider = localizationService.Providers.FirstOrDefault(p => p.Name.Equals(ProviderName, StringComparison.Ordinal));
if (localizationProvider != null)
{
//If found, remove it.
localizationService.Providers.Remove(localizationProvider);
}
}
}
public void Preload(string[] parameters) { }
}
}
How to register a provider using a virtual path in a IInitializableModule.
using System;
using System.Linq;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.Localization;
using EPiServer.Framework.Localization.XmlResources;
using EPiServer.Web.Hosting;
namespace CodeSamples
{
[InitializableModule]
//A dependency to EPiServer CMS initialization is needed to be able to use a VPP
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class CustomLanguageProviderInitializationWithVirtualPath : IInitializableModule
{
private const string ProviderName = "CustomProviderName";
public void Initialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
VirtualPathXmlLocalizationProviderInitializer localizationProviderInitializer =
new VirtualPathXmlLocalizationProviderInitializer(GenericHostingEnvironment.VirtualPathProvider);
//a VPP with the path below must be registered in the sites configuration.
string virtualPath = "~/MyCustomLanguageVPP/";
FileXmlLocalizationProvider localizationProvider = localizationProviderInitializer.GetInitializedProvider(virtualPath, ProviderName);
//Inserts the provider first in the provider list so that it is prioritized over default providers.
localizationService.Providers.Insert(0, localizationProvider);
}
}
public void Uninitialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
//Gets any provider that has the same name as the one initialized.
LocalizationProvider localizationProvider = localizationService.Providers.FirstOrDefault(p => p.Name.Equals(ProviderName, StringComparison.Ordinal));
if (localizationProvider != null)
{
//If found, remove it.
localizationService.Providers.Remove(localizationProvider);
}
}
}
public void Preload(string[] parameters) { }
}
}
How to register a provider in web.config.
<episerver.framework>
...
<localization fallbackBehavior="FallbackCulture, MissingMessage, Echo" fallbackCulture="en">
<providers>
<add physicalPath="c:\temp\resourceFolder"
name="customResources"
type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider" />
</providers>
</localization>
...
</episerver.framework>
How to register a provider using a physical path in a IInitializableModule.
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 CustomLanguageProviderInitializationWithPhysicalPath : IInitializableModule
{
private const string ProviderName = "customResources";
public void Initialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
string langFolder = @"c:\temp\resourceFolder";
if (Directory.Exists(langFolder))
{
NameValueCollection configValues = new NameValueCollection();
//This config value tells the provider where to find XML language documents.
configValues.Add(FileXmlLocalizationProvider.PhysicalPathKey, langFolder);
FileXmlLocalizationProvider localizationProvider = new FileXmlLocalizationProvider();
//Instanciates the provider
localizationProvider.Initialize(ProviderName, configValues);
//Adds it at the end of the list of providers.
localizationService.Providers.Add(localizationProvider);
}
}
}
public void Uninitialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
//Gets any provider that has the same name as the one initialized.
LocalizationProvider localizationProvider = localizationService.Providers.FirstOrDefault(p => p.Name.Equals(ProviderName, StringComparison.Ordinal));
if (localizationProvider != null)
{
//If found, remove it.
localizationService.Providers.Remove(localizationProvider);
}
}
}
public void Preload(string[] parameters) { }
}
}
How to register a provider using a virtual path in a IInitializableModule.
using System;
using System.Linq;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Framework.Localization;
using EPiServer.Framework.Localization.XmlResources;
using EPiServer.Web.Hosting;
namespace CodeSamples
{
[InitializableModule]
//A dependency to EPiServer CMS initialization is needed to be able to use a VPP
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class CustomLanguageProviderInitializationWithVirtualPath : IInitializableModule
{
private const string ProviderName = "CustomProviderName";
public void Initialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
VirtualPathXmlLocalizationProviderInitializer localizationProviderInitializer =
new VirtualPathXmlLocalizationProviderInitializer(GenericHostingEnvironment.VirtualPathProvider);
//a VPP with the path below must be registered in the sites configuration.
string virtualPath = "~/MyCustomLanguageVPP/";
FileXmlLocalizationProvider localizationProvider = localizationProviderInitializer.GetInitializedProvider(virtualPath, ProviderName);
//Inserts the provider first in the provider list so that it is prioritized over default providers.
localizationService.Providers.Insert(0, localizationProvider);
}
}
public void Uninitialize(InitializationEngine context)
{
//Casts the current LocalizationService to a ProviderBasedLocalizationService to get access to the current list of providers
ProviderBasedLocalizationService localizationService = context.Locate.Advanced.GetInstance<LocalizationService>() as ProviderBasedLocalizationService;
if (localizationService != null)
{
//Gets any provider that has the same name as the one initialized.
LocalizationProvider localizationProvider = localizationService.Providers.FirstOrDefault(p => p.Name.Equals(ProviderName, StringComparison.Ordinal));
if (localizationProvider != null)
{
//If found, remove it.
localizationService.Providers.Remove(localizationProvider);
}
}
}
public void Preload(string[] parameters) { }
}
}
Constructors
ProviderBasedLocalizationService()
Initializes a new instance of the ProviderBasedLocalizationService class.
Declaration
public ProviderBasedLocalizationService()
ProviderBasedLocalizationService(ResourceKeyHandler)
Initializes a new instance of the ProviderBasedLocalizationService class.
Declaration
public ProviderBasedLocalizationService(ResourceKeyHandler keyHandler)
Parameters
Type | Name | Description |
---|---|---|
ResourceKeyHandler | keyHandler | The handler used to manipulate resource keys. |
Properties
AvailableLocalizations
Gets all available localizations that the LocalizationService has knowledge of.
Declaration
public override IEnumerable<CultureInfo> AvailableLocalizations { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Globalization.CultureInfo> |
Overrides
Providers
Gets a list of providers that are used by the ProviderBasedLocalizationService.
Declaration
public IList<LocalizationProvider> Providers { get; protected set; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IList<LocalizationProvider> | The list of providers. |
Remarks
Note that as the Providers are processed in prioritized order from first to last, you should insert you provider first if you want it to be called first.
Methods
ConfigureContainer(ServiceConfigurationContext)
Configure the IoC container before initialization.
Declaration
public void ConfigureContainer(ServiceConfigurationContext context)
Parameters
Type | Name | Description |
---|---|---|
ServiceConfigurationContext | context | The context on which the container can be accessed. |
CreateInstance(ResourceKeyHandler, EPiServerFrameworkSection)
Creates a provider based localization service with providers from the given configuration.
Declaration
[Obsolete("Use constructor and add instantiated providers through Providers property")]
public static LocalizationService CreateInstance(ResourceKeyHandler keyHandler, EPiServerFrameworkSection configuration)
Parameters
Type | Name | Description |
---|---|---|
ResourceKeyHandler | keyHandler | The key handler to use for the localization service. |
EPiServerFrameworkSection | configuration | The configuration to read. |
Returns
Type | Description |
---|---|
LocalizationService | A localization service. |
CreateLocalizationService(ResourceKeyHandler, LocalizationOptions, IServiceLocator)
Unsupported INTERNAL API! Not covered by semantic versioning; might change without notice.
Declaration
public static LocalizationService CreateLocalizationService(ResourceKeyHandler keyHandler, LocalizationOptions localizationOptions, IServiceLocator serviceLocator)
Parameters
Type | Name | Description |
---|---|---|
ResourceKeyHandler | keyHandler | |
LocalizationOptions | localizationOptions | |
IServiceLocator | serviceLocator |
Returns
Type | Description |
---|---|
LocalizationService |
GetAllStringsByCulture(String, String[], CultureInfo)
Gets all localized strings for the specified culture below the specified key.
Declaration
protected override IEnumerable<ResourceItem> GetAllStringsByCulture(string originalKey, string[] normalizedKey, CultureInfo culture)
Parameters
Type | Name | Description |
---|---|---|
System.String | originalKey | The original key that was passed into any GetString method. |
System.String[] | normalizedKey | The |
System.Globalization.CultureInfo | culture | The requested culture for the localized strings. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<ResourceItem> | All localized strings below the specified key. |
Overrides
See Also
Initialize(InitializationEngine)
Declaration
public void Initialize(InitializationEngine context)
Parameters
Type | Name | Description |
---|---|---|
InitializationEngine | context |
InitializeProviders(ProviderSettingsCollection)
Initializes a set of providers from the provided System.Configuration.ProviderSettingsCollection.
Declaration
[Obsolete("Add instantiated providers using through Providers property")]
public virtual void InitializeProviders(ProviderSettingsCollection settings)
Parameters
Type | Name | Description |
---|---|---|
System.Configuration.ProviderSettingsCollection | settings | The System.Configuration.ProviderSettingsCollection to initialize the provider collection with. |
LoadString(String[], String, CultureInfo)
Gets the localized string for the specified key in the specified culture.
Declaration
protected override string LoadString(string[] normalizedKey, string originalKey, CultureInfo culture)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | normalizedKey | The |
System.String | originalKey | The original key that was passed into any GetString method. |
System.Globalization.CultureInfo | culture | The requested culture for the localized string. |
Returns
Type | Description |
---|---|
System.String | A localized string or |
Overrides
See Also
Uninitialize(InitializationEngine)
Declaration
public void Uninitialize(InitializationEngine context)
Parameters
Type | Name | Description |
---|---|---|
InitializationEngine | context |