简体   繁体   English

为Enums提供本地化的最佳方式是什么?

[英]What's the best way to provide localization for Enums?

I'm writting a multi-lingual application that uses many enums, and I'd like to achieve the following objectives: 我正在编写一个使用很多枚举的多语言应用程序,我希望实现以下目标:

  1. Display Enum names as localized strings 将枚举名称显示为本地化字符串
  2. Provide localized descriptions using attributes 使用属性提供本地化描述
  3. Enable language sensitive parsing of enums back to int values 启用对枚举的语言敏感解析回到int值

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? 某种基于TypeDescriptor的机制是否合适? Or even possible? 甚至可能吗?

Moreover - how does one accomplish goal #3 in a clean, generic, re-usable manner? 此外 - 如何以干净,通用,可重复使用的方式完成目标#3?


Since asking this question, I've completed the open source library that needed localizable enum displays. 自从提出这个问题以来,我已经完成了需要可本地化的枚举显示的开源库。 I went with the technique of implementing TypeConverters. 我选择了实现TypeConverters的技术。 Full source available at http://measures.codeplex.com/ 完整资料来源于http://measures.codeplex.com/

How about writing TypeConvertor for your enums? 如何为你的枚举编写TypeConvertor

  1. Define some custom attribute that will identify resource string for enum value. 定义一些自定义属性,用于标识枚举值的资源字符串。
  2. Write TypeConvertor that will look up this attribute to get resource id and then convert the value to string. 写TypeConvertor,它将查找此属性以获取资源ID,然后将该值转换为字符串。 (You can have helper class for the same but using TypeConvertor and TypeConvertorAttribute allows to use it more transparently). (您可以使用相同的辅助类,但使用TypeConvertor和TypeConvertorAttribute允许更透明地使用它)。
  3. You can write a generic class/method that will do reverse conversion (from string to enum). 您可以编写一个将执行反向转换的通用类/方法(从字符串到枚举)。 The method will be something like Parse<T>(string value) where T will be enum type. 该方法将类似于Parse<T>(string value) ,其中T将是枚举类型。 The implementation will build (on demand) a lookup dictionary for given enum type T using reflection to look up for your custom attribute. 该实现将使用反射来查找您的自定义属性(按需)构建给定枚举类型T的查找字典。

I just wrote an answer to another thread touching on some of the stuff. 我刚刚写了另一个涉及某些东西的线程的答案。 Look here for some code samples. 在这里查看一些代码示例。

I think you are in the exactly right direction — use attributes to decorate the enumeration members. 我认为你的方向正确 - 使用属性来装饰枚举成员。 The only think you probably need to do is forget about System.ComponentModel and design your own set of attributes that will respect your requirements and overall application architecture. 您可能需要做的唯一想法是忘记System.ComponentModel并设计您自己的一组属性,这些属性将尊重您的需求和整体应用程序架构。

We used the same approach and it works as expected “in a clean, generic, re-usable manner”. 我们使用相同的方法,它以“干净,通用,可重复使用的方式”按预期工作。 The only aspect we didn't implemented because we didn't need it was actual internationalization. 我们没有实施的唯一方面是因为我们不需要实际国际化。 However, that's just some mechanics that requires you to keep track of the current culture and decide where to select resource files. 但是,这只是一些需要您跟踪当前文化并决定在何处选择资源文件的机制。 Basically, you might need to select between “multiple attributes” (one per language) or “single attribute” (one for all languages encapsulating resource file selection) approach. 基本上,您可能需要在“多个属性”(每种语言一个)或“单个属性”(一个用于封装资源文件选择的所有语言)方法之间进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定枚举(列表)的最佳方法是什么 <Enum> )到DropDownList? - What's Best way to Bind collection of Enums ( List<Enum> ) to a DropDownList? 在C#中为外部开发人员提供库的最佳方法是什么? - What's the best way to provide a library to external developers in C#? 在 C# 中向其他人提供接口定义的最佳方式是什么? - What's the best way to provide an interface definition to others in C#? 比较两个不同枚举的最佳方法是什么? - What is the best way to compare two different enums? 比较 c# 中的枚举的最佳和高效方法是什么? - What is the best and performant way to compare enums in c#? 在类之间编写共享枚举的最佳实践是什么? - What's the best practice to code shared enums between classes 在 Unity 中进行本地化的最佳方法 - The best way to do localization in Unity 什么是多线程的最佳方式? - What's the best way to multithread? EF 4.1使用枚举的最佳方法是什么,并能够在列上查询它们? - EF 4.1 what is the best way to use enums and be able to query them on columns? 将 object 提供给 static 类的 static 方法的正确方法是什么? - What is the proper way to provide an object to a static class's static method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM