为了支持Localization,同样借鉴XML这一强大技术,我的本地资源化非常简单:
Resource.GetModule(“Menu”)[“New”]
通过这样一个句代码,你便可获取Unicode的字符了。
当然,你要问这是怎么来的呢?下面让我们看一下配置文件:
< config module ="Product" >
< item key ="Title" > 智睿方案框架管理系统 </ item >
< item key ="SubTitle" > ——基于面向服务的多层架构 </ item >
< item key ="Copyright" > 版权所有 ZIVSOFT 2000年-2008年 </ item >
< item key ="Architect" > 沪ICP备07505171号 </ item >
</ config >
< config module ="Login" >
< item key ="Login" > 登陆 </ item >
< item key ="Logout" > 退出 </ item >
< item key ="UserName" > 用户 </ item >
< item key ="Password" > 密码 </ item >
< item key ="Title" > 登陆系统 </ item >
</ config >
</ Resource >
这样的文件你可以写多个,比如ResourceCN.xml,ResourceEN.xml等等。
2 < en-us > App_Data"Localization"ResourceEn.xml </ en-us >
3 < zh-cn > App_Data"Localization"ResourceCn.xml </ zh-cn >
4 < zh-tw > App_Data"Localization"ResourceTw.xml </ zh-tw >
5 < jp-wh > App_Data"Localization"ResourceJp.xml </ jp-wh >
6 < ko-kr > App_Data"Localization"ResourceKo.xml </ ko-kr >
7 < es-mx > App_Data"Localization"ResourceEs.xml </ es-mx >
8 </ Localization >
注意:当loc类型设置为”en-us”,系统控件的UI默认值便是en-us了。
为满足回帖人要求,把最主要的Resource类帖出来了:
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Xml;
5 using System.IO;
6 using System.Reflection;
7 using System.Diagnostics;
8
9 /*
10 * Created by Lihua at 2006-11-26
11 * http://www.zivsoft.com
12 */
13
14 namespace Zivsoft.Localization
15 {
16 /// <summary>
17 /// Resource Files, Language Version Solution.
18 /// </summary>
19 public class Resource:IResource
20 {
21 /// <summary>
22 /// string is stored in hashtable
23 /// </summary>
24 private static Hashtable _htStrRes;
25
26 /// <summary>
27 ///
28 /// </summary>
29 private Hashtable _htData;
30 /// <summary>
31 ///
32 /// </summary>
33 private string _module;
34
35 /// <summary>
36 ///
37 /// </summary>
38 private Resource(string module)
39 {
40 this._module = module;
41 this._htData = new Hashtable();
42 }
43
44 /// <summary>
45 ///
46 /// </summary>
47 static Resource()
48 {
49 _htStrRes = new Hashtable();
50 }
51
52 public static string CultureId
53 {
54 get
55 {
56 var loader = new ResourceFileLoader();
57 return loader.GetCurrentLanguage();
58 }
59 }
60
61 /// <summary>
62 /// Get Resource By Module ID
63 /// </summary>
64 public static Resource GetModule(string module)
65 {
66
67 if (module == null || module.Trim() == "")
68 {
69 throw new Exception("Resource-Instance: Module's value is null in resource files.");
70 }
71 //
72 if (_htStrRes.ContainsKey(module))
73 {
74 return _htStrRes[module] as Resource;
75 }
76 Resource strRes = new Resource(module);
77 strRes.LoadData();
78 _htStrRes.Add(module, strRes);
79 return strRes;
80 }
81
82 /// <summary>
83 ///
84 /// </summary>
85 private void LoadData()
86 {
87 //clear
88 this._htData.Clear();
89 //read XML file, and store the value in hashtable
90 string configName = null;
91 ResourceFileLoader loader = new ResourceFileLoader();
92 configName = loader.GetFullFilePath();
93 if (configName == string.Empty)
94 {
95 this._htData.Clear();
96 return;
97 }
98
99 //configuration file
100 XmlDocument xmlDoc = new XmlDocument();
101 try
102 {
103 xmlDoc.Load(configName);
104 XmlNode root = xmlDoc.DocumentElement;
105 foreach (XmlNode xNode in root.ChildNodes)
106 {
107 //node is Config
108 if (xNode.Name != "config")
109 {
110 continue;
111 }
112
113 //node is module
114 XmlAttribute attr = xNode.Attributes["module"];
115 if (null == attr)
116 {
117 continue;
118 }
119
120 //if it is current module
121 if (this._module == xNode.Attributes["module"].Value)
122 {
123 foreach (XmlNode item in xNode.ChildNodes)
124 {
125 attr = item.Attributes["key"];
126 if (null == attr || this._htData.ContainsKey(attr.Value))
127 {
128 continue;
129 }
130 this._htData.Add(attr.Value, item.InnerText);
131 }
132 break;
133 }
134 }
135 }
136 catch (Exception e)
137 {
138 Debug.WriteLine(e);
139 this._htData.Clear();
140 }
141 }
142
143 /// <summary>
144 ///
145 /// </summary>
146 public string this[string key]
147 {
148 get
149 {
150 string s = this._htData[key] as string;
151 string def = "[" + this._module + "][" + key + "]";
152 return s == null ? def : s;
153 }
154 }
155 /// <summary>
156 ///
157 /// </summary>
158 public string GetKey(string key)
159 {
160 return this[key];
161 }
162
163 }
164 }