如何本地化 C# 中的属性描述?
我正在开发一个课程,该课程将被来自其他国家的一些人使用。我必须本地化每条消息,警告 ec,以便他们能够理解我们的意思。在很多情况下我都实现了我的目标。但是像描述这样的属性属性实在是太麻烦了。
这就是我现在所拥有的:
[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
{...}
}
就是我正在努力做的事情。但不可能以这种方式使用本地化。关于我可以用来代替它的东西有什么建议吗?
I'm working on a class that is going to be used by some people from another countries. I have to localize every message, warning e.c. so that they can understand what we mean. In many cases i achieved my goal. But these property attributes like descriptions are such a pain in the ass.
Here`s what I have right now:
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
and
[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
{...}
}
That's what I'm trying to do. But it's not possible to use Localisations this way. Any Suggestions about something that I can use instead of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
子类:
Subclasses:
我结合了 Marc Gravell 和 dsmith的回答:
首先:创建一个名为LocalizedDescriptionAttribute的Attribute类,
将其用作LocalizedDescription(“Attribute”一词不是必需的)
I've combined Marc Gravell and dsmith's answers:
First: create an Attribute class named as LocalizedDescriptionAttribute
Use it as LocalizedDescription (the word "Attribute" is not necessary)
请参阅:http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx
see: http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx
让我们看一下这个示例类:
现在您可以实现自定义属性类:
现在您有了本地化的描述:
Let's take this sample class:
Now you can implement a custom attribute class:
Now you have the localized description:
稍微扩展贾尼的答案(这对我来说效果很好):
可以更清楚地完成,因为
它含糊地暗示,但不清楚原始代码中缺少什么。资源类的
.ResourceManager
属性是自动生成的。然后,要访问它,请使用 Glennular 在此描述的扩展方法: StackOverflow 而不是在字段/类名称中进行硬编码(只需使用
LocDescriptionAttribute
而不是DescriptionAttribute
)。我所做的唯一的其他调整是基于
this IComparable enum
使其静态,以允许它被任何枚举使用,而不是被锁定到特定类型;这是我能做的最好的事情,因为我无法应用接口将其限制为仅包含我添加了描述的枚举。Slight extension to Jani's answer (which has worked well for me):
can be more clearly done as
It was vaguely implied, but wasn't clear what was missing from the original code. The
.ResourceManager
property of the resource class is auto-generated.Then, to access it, use the extension method described by Glennular here: StackOverflow instead of hardcoding in field/class names (just use
LocDescriptionAttribute
instead ofDescriptionAttribute
).The only other adjustment I made was to make it static based on
this IComparable enum
, to allow it to be used by any enum instead of being locked to a particular type; it was the best I could do given that I can't apply an interface to limit it to just the enums I've added descriptions to.似乎
CategoryAttribute
具有根据内部资源查找本地化字符串的代码,但DescriptionAttribute
没有本地化。来自 Reflector:
我认为您可以扩展属性并创建自己的实现,传入 ResourceType。像这样的东西,模糊地基于 DataAnnotation 逻辑。显然需要更多的清理工作。
It seems the
CategoryAttribute
has code to look for an localized string based on internal resources, butDescriptionAttribute
has no localization.From Reflector:
I think you would to extend the attributes and create your own implementation, passing in the ResourceType. Something like this, based vaguely on
DataAnnotation
logic. Obviously needs a bit more clean up.