, , DI, IStringLocalizer, . . GitHub.
using System;
using System.Collections.Specialized;
using System.Resources;
namespace MvcSiteMapProvider.Globalization
{
public class ResourceManagerStringLocalizer
: IStringLocalizer
{
public ResourceManagerStringLocalizer(
ResourceManager resourceManager
)
{
if (resourceManager == null)
throw new ArgumentNullException("resourceManager");
this.resourceManager = resourceManager;
}
protected readonly ResourceManager resourceManager;
public virtual string GetResourceString(string attributeName, string value, bool enableLocalization, string classKey, string implicitResourceKey, NameValueCollection explicitResourceKeys)
{
if (attributeName == null)
{
throw new ArgumentNullException("attributeName");
}
if (enableLocalization)
{
string result = string.Empty;
if (explicitResourceKeys != null)
{
string[] values = explicitResourceKeys.GetValues(attributeName);
if ((values == null) || (values.Length <= 1))
{
result = value;
}
else if (this.resourceManager.BaseName.Equals(values[0]))
{
try
{
result = this.resourceManager.GetString(values[1]);
}
catch (MissingManifestResourceException)
{
if (!string.IsNullOrEmpty(value))
{
result = value;
}
}
}
}
if (!string.IsNullOrEmpty(result))
{
return result;
}
}
if (!string.IsNullOrEmpty(value))
{
return value;
}
return string.Empty;
}
}
}
DI ( StructureMap, DI ).
, , IStringLocalizer , excludeTypes.
var excludeTypes = new Type[] {
typeof(IStringLocalizer)
};
ResourceManagerStringLocalizer ( ).
string resourceBaseName = "SomeAssembly.Resources.Resource1";
Assembly resourceAssembly = typeof(SomeAssembly.Class1).Assembly;
this.For<ResourceManager>().Use(() => new ResourceManager(resourceBaseName, resourceAssembly));
this.For<IStringLocalizer>().Use<ResourceManagerStringLocalizer>();
. ( SomeAssembly.Resources.Resource1), .
<mvcSiteMapNode title="$resources:SomeAssembly.Resources.Resource1,ContactTitle" controller="Home" action="Contact"/>
[MvcSiteMapNode(Title = "$resources:SomeAssembly.Resources.Resource1,ContactTitle", Controller = "Home", Action = "Contact)]
, BaseName . . MSDN: http://msdn.microsoft.com/en-us/library/yfsz7ac5(v=vs.110).aspx