Last active
February 17, 2017 15:40
-
-
Save bmcdavid/79caec34d36a0d763df2b3e69d9a2a85 to your computer and use it in GitHub Desktop.
Custom Episerver Localization Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer.Framework; | |
using EPiServer.Framework.Localization; | |
using EPiServer.Framework.Localization.Internal; | |
using EPiServer.ServiceLocation; | |
using System; | |
using System.Globalization; | |
using System.Linq; | |
namespace WSOL.Web.Business | |
{ | |
[InitializableModule] | |
[ModuleDependency(typeof(ServiceContainerInitialization))] // used to override default provider | |
public class CustomProviderBasedLocalizationService : ProviderBasedLocalizationService, IConfigurableModule | |
{ | |
private static LocalizationService _localizationService; | |
public CustomProviderBasedLocalizationService() : this(null) { } | |
public CustomProviderBasedLocalizationService(ResourceKeyHandler keyHandler) : base(keyHandler) { } | |
protected override string LoadString(string[] normalizedKey, string originalKey, CultureInfo culture) | |
{ | |
var list = Providers.ToList(); // important fix for collection was modified issue | |
foreach (LocalizationProvider provider in list) | |
{ | |
string str = provider.GetString(originalKey, normalizedKey, culture); | |
if (str != null) | |
return str; | |
} | |
return null; | |
} | |
/// <summary> | |
/// Used to create service container instance | |
/// </summary> | |
/// <param name="keyHandler"></param> | |
/// <param name="localizationOptions"></param> | |
/// <param name="serviceLocator"></param> | |
/// <returns></returns> | |
public static LocalizationService CreateCustomProviderBasedLocalizationService(ResourceKeyHandler keyHandler, LocalizationOptions localizationOptions, IServiceLocator serviceLocator) | |
{ | |
if (_localizationService == null) | |
{ | |
ProviderBasedLocalizationService localizationService = new CustomProviderBasedLocalizationService(keyHandler); | |
foreach (Func<IServiceLocator, LocalizationProvider> providerFactory in localizationOptions.ProviderFactories) | |
{ | |
localizationService.Providers.Add(providerFactory(serviceLocator)); | |
} | |
localizationService.FallbackBehavior = localizationOptions.FallbackBehavior; | |
localizationService.FallbackCulture = localizationOptions.FallbackCulture; | |
_localizationService = localizationService; | |
} | |
return _localizationService; | |
} | |
public new void ConfigureContainer(ServiceConfigurationContext context) | |
{ | |
// register as a factory | |
context.Services.AddSingleton(typeof(LocalizationService), (locator) => | |
CreateCustomProviderBasedLocalizationService(locator.GetInstance<ResourceKeyHandler>(), locator.GetInstance<LocalizationOptions>(), locator)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment