The trick is to implement an infrastructure component calls into MVC's DependencyResolver.Current
, so you can register the real ResourceProviderFactory
using your favorite DI container.
Here is such a class that will do the trick:
public class MvcResourceProviderFactory : ResourceProviderFactory
{
public override IResourceProvider CreateGlobalResourceProvider(
string classKey)
{
return GetFactory().CreateGlobalResourceProvider(classKey);
}
public override IResourceProvider CreateLocalResourceProvider(
string virtualPath)
{
return GetFactory().CreateLocalResourceProvider(virtualPath);
}
private static ResourceProviderFactory GetFactory()
{
var resolver = DependencyResolver.Current;
var factory = resolver.GetService<ResourceProviderFactory>();
if (factory != null)
{
return factory;
}
throw new InvalidOperationException(string.Format(
"No {0} has been registered in the {1}.",
typeof(ResourceProviderFactory).FullName,
DependencyResolver.Current.GetType().FullName));
}
}
This class can be configured as follows:
<globalization
resourceProviderFactoryType="MyApp.MvcResourceProviderFactory, MyApp"
enableClientBasedCulture="true" uiCulture="auto" culture="auto"
/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…