There are many localization solutions. I decided for this:
http://geekswithblogs.net/brians/archive/2010/06/14/asp.net-mvc-localization-displaynameattribute-alternatives-a-better-way.aspx
public class LocalizedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(
IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType,
string propertyName)
{
var meta = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (string.IsNullOrEmpty(propertyName))
return meta;
if (meta.DisplayName != null)
GetLocalizedDisplayName(meta, propertyName);
if (string.IsNullOrEmpty(meta.DisplayName))
meta.DisplayName = string.Format("[[{0}]]", propertyName);
return meta;
}
private static void GetLocalizedDisplayName(ModelMetadata meta, string propertyName)
{
ResourceManager resourceManager = App_GlobalResources.Strings.ResourceManager;
CultureInfo culture = Thread.CurrentThread.CurrentUICulture;
meta.DisplayName = resourceManager.GetString(propertyName, culture);
}
}
I changed the line:
if (meta.DisplayName == null)
at
if (meta.DisplayName != null)
to enter the GetLocalizedDisplayName function
There are 2 files in App_GlobalResources:
Strings.resx and Strings.pl.resx . Both of them have an Open access modifier, and the build action is set to Embedded resource
The whole site has been translated, but I have a problem with Attributes
[Required]
[LocalizedDisplayName("UserName", NameResourceType = typeof(App_GlobalResources.Strings))]
public string UserName { get; set; }
I think the problem is in this line from the link I wrote above:
meta.DisplayName = resourceManager.GetString(propertyName, culture);
GetString always returns the default value from Strings.resx .
culture pl and property Name are correct Username , so the return value must be from strings. pl .resx, not from Strings.resx.Please help me:)