Reputation: 645
Can somebody tell me what I'm doing wrong in this code:
public class LocalizationDisplayNameAttribute : DisplayNameAttribute
{
public LocalizationDisplayNameAttribute(string resourceKey)
{
ResourceKey = resourceKey;
}
public override string DisplayName
{
get
{
string displayName = App_GlobalResources.Global.ResourceManager.GetString(ResourceKey);
return string.IsNullOrEmpty(displayName)
? string.Format("[[{0}]]", ResourceKey)
: displayName;
}
}
private string ResourceKey { get; set; }
}
The culture is set to cs. I have two resources: Global.resx and Global.cs.resx, but when I run this application I always get string from Global.resx (it should be Global.cs.resx)
Upvotes: 1
Views: 515
Reputation: 1038810
The following works fine for me:
public class LocalizationDisplayNameAttribute : DisplayNameAttribute
{
public LocalizationDisplayNameAttribute(string resourceKey)
{
ResourceKey = resourceKey;
}
public override string DisplayName
{
get
{
string displayName = Global.ResourceManager.GetString(ResourceKey);
return string.IsNullOrEmpty(displayName)
? string.Format("[[{0}]]", ResourceKey)
: displayName;
}
}
private string ResourceKey { get; set; }
}
View model:
public class MyViewModel
{
[LocalizationDisplayName("foo")]
public string Foo { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
}
View:
@model MyViewModel
@Html.LabelFor(x => x.Foo)
@Html.EditorFor(x => x.Foo)
Resources:
~/App_GlobalResources/Global.resx
:
foo: foo
~/App_GlobalResources/Global.cs.resx
:
foo: localized foo
~/web.config
:
<system.web>
<globalization culture="cs" uiCulture="cs"/>
...
</system.web>
prints the correct localized value.
Upvotes: 1