本地化枚举描述属性 [英] Localizing enum descriptions attributes
本文介绍了本地化枚举描述属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是在.NET本地化枚举描述的最佳方法是什么?
What is the best way to localize enumeration descriptions in .net?
(请参见添加说明,以枚举常量,以枚举说明示例)
(See Adding descriptions to enumeration constants for enum description example)
在理想情况下,我想一些与应用程序的其他领域如何进行本地化使用的ResourceManager和资源文件,以便使其适合
Ideally I would like something that uses ResourceManager and resource files so it fits in with how other areas of the app are localized.
推荐答案
这是我结束了去,我没有看到添加自定义属性的值类来保存一个资源键,然后查找到资源文件 - 为什么不直接使用枚举类型名+值作为资源key
This is what I ended up going with, I didn't see the value in adding a custom attribute class to hold a resource key and then looking up into the resource files - why not just use the enums typename + value as a resource key?
using System;
using System.Resources;
using System.Reflection;
public class MyClass
{
enum SomeEnum {Small,Large};
private ResourceManager _resources = new ResourceManager("MyClass.myResources",
System.Reflection.Assembly.GetExecutingAssembly());
public string EnumDescription(Enum enumerator)
{
string rk = String.Format("{0}.{1}",enumerator.GetType(),enumerator);
string localizedDescription = _resources.GetString(rk);
if (localizedDescription == null)
{
// A localized string was not found so you can either just return
// the enums value - most likely readable and a good fallback.
return enumerator.ToString();
// Or you can return the full resourceKey which will be helpful when
// editing the resource files(e.g. MyClass+SomeEnum.Small)
// return resourceKey;
}
else
return localizedDescription;
}
void SomeRoutine()
{
// Looks in resource file for a string matching the key
// "MyClass+SomeEnum.Large"
string s1 = EnumDescription(SomeEnum.Large);
}
}
这篇关于本地化枚举描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文