.NET中属性值的本地化

时间:2022-10-20 18:22:17

How can I localize/internationalize attribute values in .NET?

如何在.NET中本地化/国际化属性值?

My specific example is for ASP.NET web parts such as WebDisplayName, WebDescription, etc. where I inherit from the base class that uses these attributes.

我的具体示例是ASP.NET Web部件,例如WebDisplayName,WebDescription等,其中我从使用这些属性的基类继承。

Also, is there any difference to doing this with attributes declared in my own classes?

另外,使用我自己的类中声明的属性执行此操作是否有任何区别?

Thanks!

2 个解决方案

#1


Have you had a look at Localizing the Property Grid?

您是否看过本地化属性网格?

This seems pretty much the same thing, except you're inheriting from WebDescriptionAttribute (et al) instead of DescriptionAttribute. All of the important properties in the Web classes, like Description, are still overridable.

除了从WebDescriptionAttribute(et al)继承而不是DescriptionAttribute之外,这看起来几乎是一样的。 Web类中的所有重要属性(如Description)仍然可以覆盖。

The example on that page calls out to a static SR class, which retrieves string resources from a specific type. If you need to support multiple languages at the same time - and obviously take a minor performance hit in the process - then you would have to adjust the attribute itself to take the Type of the resource you expect, and use the appropriate constructor for the ResourceManager class.

该页面上的示例调用静态SR类,该类从特定类型检索字符串资源。如果您需要同时支持多种语言 - 并且显然在此过程中只需要轻微的性能影响 - 那么您必须调整属性本身以获取所需资源的类型,并为ResourceManager使用适当的构造函数类。

I can go into more detail if you want to expand a little on the intended uses.

如果你想在预期用途上稍微扩展一下,我可以详细介绍一下。

#2


Check out this answer:

看看这个答案:

Localization of DisplayNameAttribute

DisplayNameAttribute的本地化

You would have to add attributes to your derived classes to override the ones on the base classes.

您必须向派生类添加属性以覆盖基类上的属性。

#1


Have you had a look at Localizing the Property Grid?

您是否看过本地化属性网格?

This seems pretty much the same thing, except you're inheriting from WebDescriptionAttribute (et al) instead of DescriptionAttribute. All of the important properties in the Web classes, like Description, are still overridable.

除了从WebDescriptionAttribute(et al)继承而不是DescriptionAttribute之外,这看起来几乎是一样的。 Web类中的所有重要属性(如Description)仍然可以覆盖。

The example on that page calls out to a static SR class, which retrieves string resources from a specific type. If you need to support multiple languages at the same time - and obviously take a minor performance hit in the process - then you would have to adjust the attribute itself to take the Type of the resource you expect, and use the appropriate constructor for the ResourceManager class.

该页面上的示例调用静态SR类,该类从特定类型检索字符串资源。如果您需要同时支持多种语言 - 并且显然在此过程中只需要轻微的性能影响 - 那么您必须调整属性本身以获取所需资源的类型,并为ResourceManager使用适当的构造函数类。

I can go into more detail if you want to expand a little on the intended uses.

如果你想在预期用途上稍微扩展一下,我可以详细介绍一下。

#2


Check out this answer:

看看这个答案:

Localization of DisplayNameAttribute

DisplayNameAttribute的本地化

You would have to add attributes to your derived classes to override the ones on the base classes.

您必须向派生类添加属性以覆盖基类上的属性。