我正在研究一个课程,这个课程将被其他国家的一些人使用。我得定位每条信息警告e.c.这样他们就能理解我们的意思了。在很多情况下,我实现了我的目标。但是,像描述这样的属性属性是非常麻烦的。
这就是我现在所拥有的:
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}和
[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
public Ampere Simin
{
get
{...}
set
{...}
}这就是我想要做的。但是不可能以这种方式使用本地化。有什么建议我可以用来代替它吗?
发布于 2011-09-13 16:15:57
子类:
[STAThread]
static void Main()
{ // just some example code to show it working in winforms, but
// anything using System.ComponentModel should see the change
Application.EnableVisualStyles();
Application.Run(new Form {Controls = {new PropertyGrid {Dock = DockStyle.Fill, SelectedObject = new Foo()}}});
}
class Foo
{ // assume the following literals are keys, for example to a RESX
[LocalizedCategory("cat")]
[LocalizedDescription("desc")]
[LocalizedDisplayName("disp name")]
public string Bar { get; set; }
}
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter)]
class LocalizedDescriptionAttribute : DescriptionAttribute
{
static string Localize(string key)
{
// TODO: lookup from resx, perhaps with cache etc
return "Something for " + key;
}
public LocalizedDescriptionAttribute(string key)
: base(Localize(key))
{
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
static string Localize(string key)
{
// TODO: lookup from resx, perhaps with cache etc
return "Something for " + key;
}
public LocalizedDisplayNameAttribute(string key)
: base(Localize(key))
{
}
}
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter)]
class LocalizedCategoryAttribute : CategoryAttribute
{
public LocalizedCategoryAttribute(string key) : base(key) { }
protected override string GetLocalizedString(string value)
{
// TODO: lookup from resx, perhaps with cache etc
return "Something for " + value;
}
}发布于 2015-12-24 04:17:00
我综合了Marc Gravell和dsmith的回答:
首先:创建一个名为LocalizedDescriptionAttribute的属性类
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
static string Localize(string key)
{
return YourResourceClassName.ResourceManager.GetString(key);
}
public LocalizedDescriptionAttribute(string key)
: base(Localize(key))
{
}
}将其用作属性(不需要“LocalizedDescription”一词)
public class Foo
{
// myString is a key that exists in your Resources file
[LocalizedDescription("myString")]
public string Bar { get; set; }
}发布于 2011-09-13 16:11:01
[Required(ErrorMessageResourceName = "LogOnModel_UserName_Required",
ErrorMessageResourceType = typeof(Resources.Global))]
[Display(Name = "LogOnModel_UserName_Required",resourceType = typeof(Resources.Global))]
public string UserName { get; set; }请参阅:http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx
https://stackoverflow.com/questions/7398653
复制相似问题