为枚举提供本地化的最佳方式是什么? [英] What's the best way to provide localization for Enums?
问题描述
- 显示枚举名称作为本地化字符串
- 使用属性提供本地化描述
- 启用语言敏感的枚举枚举回到int值
我很想使用属性+资源文件装饰枚举,以便可以从运行时类型信息到达本地化的字符串。我已经在路由之前宣布一个静态成员而不是一个枚举的静态类,但这表明了许多问题,解决了。
是否适合基于TypeDescriptor的某种机制?或者甚至可能?
此外 - 以干净,通用,可重复使用的方式完成目标3?
由于提出这个问题,我已经完成了需要可本地化枚举显示的开源库。我去了实现TypeConverters的技术。完整来源可以在 http://measures.codeplex.com/ 获得
如何写 I'm writting a multi-lingual application that uses many enums, and I'd like to achieve the following objectives: I'm keen to to decorate the enum using attributes + a resource file, such that the localized strings can be reached from run-time type info. I've been down the route before of declaring a static class with static members instead of an enum, but this presented as many problems as it solved. Is some sort of a TypeDescriptor based mechanism appropriate? Or even possible? Moreover - how does one accomplish goal #3 in a clean, generic, re-usable manner? Since asking this question, I've completed the open source library that needed localizable enum displays. I went with the technique of implementing TypeConverters. Full source available at http://measures.codeplex.com/ How about writing TypeConvertor for your enums?
这篇关于为枚举提供本地化的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Parse< T>(string value)
,其中T将是枚举类型。实现将使用反射来构建(按需)用于给定枚举类型T的查找字典,以查找您的自定义属性。
Parse<T>(string value)
where T will be enum type. The implementation will build (on demand) a lookup dictionary for given enum type T using reflection to look up for your custom attribute.