3ya*_*bos 16 c# localization properties
我正在开设一个将被其他国家的一些人使用的课程.我必须本地化每条消息,警告ec,以便他们能理解我们的意思.在许多情况下,我实现了我的目标.但是像描述这些属性属性在屁股中是如此痛苦.
这就是我现在所拥有的:
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
Run Code Online (Sandbox Code Playgroud)
和
[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
{...}
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的.但是不可能以这种方式使用Localisations.关于我可以使用的东西的任何建议而不是它?
Mar*_*ell 21
子类:
[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;
}
}
Run Code Online (Sandbox Code Playgroud)
我结合了Marc Gravell和dsmith的答案:
首先:创建一个名为 LocalizedDescriptionAttribute 的 Attribute 类
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
static string Localize(string key)
{
return YourResourceClassName.ResourceManager.GetString(key);
}
public LocalizedDescriptionAttribute(string key)
: base(Localize(key))
{
}
}
Run Code Online (Sandbox Code Playgroud)
将其用作 LocalizedDescription(不需要“属性”一词)
public class Foo
{
// myString is a key that exists in your Resources file
[LocalizedDescription("myString")]
public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)