枚举的多语言显示
早上看到jhh0111昨天关于枚举的双语显示问题的帖子,中午想了想,其实这就是一个Globalization的问题,虽然jhh0111的方案可以工作,但是扩展性不强——当枚举数量很大时可用性很低;另外,如果绑定到其他控件(比如Grid控件)也会有一些问题。
这里给出一个通用的解决方案,虽然复杂了一些,但是可用性和扩张性均有大大提高。
显然这里自己去实现自定义格式化,即通过IFormatable、IFormatProvider、ICustomFormatter等接口已达到Globalization有点小题大作了,而另外一个很容易想到的点是通过DiaplayMember实现显示值得自定义(对于简单Binding,例如ComboBox、ListBox等只用重载ToString就可以了)。
既然有了方向,那就动手了。
首先,我们希望Binding整个枚举类型的每一个值,也就是说,我们需要把这个枚举的所有值变成一个数据源,为了实现这一点,我们可以使用Enum上的helper方法Enum.GetValues(Type)来返回一个对所有值得枚举,然后依次添加到IList对象或者IListSource接口即可。
https://files.cnblogs.com/winkingzhang/Demo_2008_03_25.rar
这里给出一个通用的解决方案,虽然复杂了一些,但是可用性和扩张性均有大大提高。
显然这里自己去实现自定义格式化,即通过IFormatable、IFormatProvider、ICustomFormatter等接口已达到Globalization有点小题大作了,而另外一个很容易想到的点是通过DiaplayMember实现显示值得自定义(对于简单Binding,例如ComboBox、ListBox等只用重载ToString就可以了)。
既然有了方向,那就动手了。
首先,我们希望Binding整个枚举类型的每一个值,也就是说,我们需要把这个枚举的所有值变成一个数据源,为了实现这一点,我们可以使用Enum上的helper方法Enum.GetValues(Type)来返回一个对所有值得枚举,然后依次添加到IList对象或者IListSource接口即可。
1
if (!typeof(EnumType).IsEnum)
2
{
3
throw new NotSupportedException("Can not support type: " + typeof(EnumType).FullName);
4
// It's better use resource version as below.
5
// throw new NotSupportedException(SR.GetString("TYPE_NOT_SUPPORT", typeof(EnumType).FullName));
6
}
7
8
// Use Enum helper enumerator list all enum values and add to current context.
9
foreach (EnumType value in Enum.GetValues(typeof(EnumType)))
10
{
11
//TODO: add each value to IList
12
base.Add(new EnumAdapter(value));
13
}
然后,取到了值,由于我们希望自定义Binding显示,那么需要对枚举值进行封装,而在这个封装里面,我们可以实现多语言的支持。
2

3

4

5

6

7

8

9

10

11

12

13

1
/// <summary>
2
/// Enum value adapter, used to get values from each Cultures.
3
/// </summary>
4
public sealed class EnumAdapter
5
{
6
/// <summary>
7
/// Storage the actual Enum value.
8
/// </summary>
9
private EnumType _value;
10
11
/// <summary>
12
/// Constructor an <see cref="EnumAdapter"/>.
13
/// </summary>
14
/// <param name="value">The enum value.</param>
15
/// <exception cref="">
16
///
17
/// </exception>
18
public EnumAdapter(EnumType value)
19
{
20
if (!Enum.IsDefined(typeof(EnumType), value))
21
{
22
throw new ArgumentException(string.Format("{0} is not defined in {1}", value, typeof(EnumType).Name), "value");
23
// It's better use resource version as below.
24
// throw new ArgumentException(SR.GetString("ENUM_NOT_DEFINED_FMT_KEY", value, typeof(EnumType).Name), "value");
25
}
26
_value = value;
27
}
28
29
/// <summary>
30
/// Gets the actual enum value.
31
/// </summary>
32
public EnumType Value
33
{
34
get { return _value; }
35
}
36
37
/// <summary>
38
/// Gets the display value for enum value by search local resource with currrent UI culture
39
/// and special key which is concated from Enum type name and Enum value name.
40
/// </summary>
41
/// <remarks>
42
/// This would get correct display value by accessing location resource with current UI Culture.
43
/// </remarks>
44
public string DisplayValue
45
{
46
get { return SR.GetString(string.Format("{0}.{1}", typeof(EnumType).Name, _value.ToString())); }
47
}
48
49
//TODO: If you want more, please add below
50
}
至此,整个功能的框架已经完成,下面我们来看看一些细节——如何对资源读取和管理的封装:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49


50

1
/// <summary>
2
/// Constructor a new <see cref="SR"/>.
3
/// </summary>
4
internal SR()
5
{
6
//TODO: If you modified resource location, please update here
7
this.resources = new System.Resources.ResourceManager(
8
string.Concat(typeof(EnumAdapter).Namespace, ".Resource"),
9
base.GetType().Assembly);
10
}
11
12
/// <summary>
13
/// Get singleton instance.
14
/// </summary>
15
/// <returns>A singleton <see cref="SR"/></returns>
16
private static SR GetLoader()
17
{
18
if (loader == null)
19
{
20
lock (SR.InternalSyncObject)
21
{
22
if (loader == null)
23
{
24
loader = new SR();
25
}
26
}
27
}
28
return loader;
29
}
30
31
/// <summary>
32
/// Gets an object from resources by special key, which provided by <paramref name="name"/>.
33
/// </summary>
34
/// <param name="name">Resource accessed key</param>
35
/// <returns>return stored object in resource. if resource not found, return <paramref name="name"/> as object.</returns>
36
public static object GetObject(string name)
37
{
38
SR loader = GetLoader();
39
if (loader == null)
40
{
41
return null;
42
}
43
try
44
{
45
return loader.resources.GetObject(name, Culture);
46
}
47
catch { }
48
return name;
49
}
50
51
/// <summary>
52
/// Gets a string from resources by special key, which provided by <paramref name="name"/>.
53
/// </summary>
54
/// <param name="name">Resource accessed key</param>
55
/// <returns>return stored string in resource. If resource not found, retuen <paramref name="name"/> as result.</returns>
56
public static string GetString(string name)
57
{
58
SR loader = GetLoader();
59
if (loader == null)
60
{
61
return null;
62
}
63
try
64
{
65
return loader.resources.GetString(name, Culture);
66
}
67
catch { }
68
return name;
69
}
70
71
/// <summary>
72
/// Gets a formatted string from resources by special key, which provided by <paramref name="name"/> and optional parameters.
73
/// </summary>
74
/// <param name="name">Resource accessed key</param>
75
/// <param name="args">format arguments.</param>
76
/// <returns>return stored string in resource. If resource not found, use <paramref name="name"/> as formator, return the formatted string.</retur
77
public static string GetString(string name, params object[] args)
78
{
79
SR loader = GetLoader();
80
if (loader == null)
81
{
82
return null;
83
}
84
string format = name;
85
try
86
{
87
format = loader.resources.GetString(name, Culture);
88
}
89
catch { }
90
91
if ((args == null) || (args.Length <= 0))
92
{
93
return format;
94
}
95
96
// It's better cut long arg for formating.
97
for (int i = 0; i < args.Length; i++)
98
{
99
string arg = args[i] as string;
100
if ((arg != null) && (arg.Length > 0x400))
101
{
102
args[i] = arg.Substring(0, 0x3fd) + "
";
103
}
104
}
105
return string.Format(System.Globalization.CultureInfo.CurrentCulture, format, args);
106
}
OK,大功告成,有了这么一个封装,在应用里就可以简单的这么几句够搞定。
2

3

4

5

6


7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102


103

104

105

106

1
private void Form1_Load(object sender, EventArgs e)
2
{
3
this.comboBox1.DataSource = new EnumDataSource<Sex>();
4
this.comboBox1.DisplayMember = "DisplayValue";
5
this.comboBox1.ValueMember = "Value";
6
}
7
8
public enum Sex
9
{
10
Male,
11
Female
12
}
你可以从这里下载本篇中的相关示例代码:
2

3

4

5

6

7

8

9

10

11

12

https://files.cnblogs.com/winkingzhang/Demo_2008_03_25.rar
To be the apostrophe which changed “Impossible” into “I’m possible”
----------------------------------------------------
WinkingZhang's Blog (http://winkingzhang.cnblogs.com)
GCDN(http://gcdn.grapecity.com/cs)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述