CodeResourceControl.cs
9.5 KB
1
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
namespace OnlineStore
{
public class CodeResourceControl
{
public static readonly ILog LOG = LogManager.GetLogger("LngResource");
//public delegate string GetStrDelegate(string id, string defaultStr);
//public static event GetStrDelegate GetStrEvent;
//public delegate string GetStringDelegate(string id, string defaultStr, params object[] param);
//public static event GetStringDelegate GetStringEvent;
public static bool OpenResourceLog = false;
public static string China = "zh-CN";
public static string English = "en-US";
//private static Dictionary<string, string> chineseMap = new Dictionary<string, string>();
//private static Dictionary<string, string> englishMap = new Dictionary<string, string>();
private static Dictionary<string,Dictionary<string, string>> LangMap = new Dictionary<string, Dictionary<string, string>>();
public delegate string GetLanguageDelegate();
public static event GetLanguageDelegate GetLanguageEvent;
public delegate void RefreshLanguageDelegate();
public static event RefreshLanguageDelegate RefreshLanguageEvent;
public static string GetLanguage()
{
if (GetLanguageEvent == null)
{
return China;
}
string result = GetLanguageEvent?.Invoke();
if (result == null)
{
return "";
}
return result;
}
private static string spiltStr = "_";
private static string Text = "Text";
public static string GetTextIdStr(string className, string controlName)
{
return className + spiltStr + controlName + spiltStr + Text;
}
public static string GetTextIdStr(string className)
{
return className + spiltStr + Text;
}
public static string GetString(string id, string defaultStr)
{
string strCurLanguage = defaultStr;
var haslang = getLangRes(CurrLanguage);
if (!haslang) {
return strCurLanguage;
}
try
{
LangMap[CurrLanguage].TryGetValue(id, out strCurLanguage);
if ((strCurLanguage == null || strCurLanguage.Equals("")) && (!defaultStr.Equals("")))
{
strCurLanguage = defaultStr;
NoIdLog(id, defaultStr);
}
}
catch (Exception ex)
{
if (defaultStr.Equals(""))
{
}
else
{
strCurLanguage = "No id:[" + id + "], please add.";
strCurLanguage = defaultStr;
NoIdLog(id, defaultStr);
}
}
if (strCurLanguage == null)
{
strCurLanguage = "";
}
return strCurLanguage;
}
public static string GetString(string id, string defaultStr, params object[] param)
{
string strCurLanguage = GetString(id, defaultStr);
return String.Format(strCurLanguage, param);
}
private static void NoIdLog(string id, string defaultStr)
{
if (OpenResourceLog)
{
if (!LangMap["zh-CN"].ContainsKey(id) && checkInterid(id))
LOG.Info("No Res id:" + id + "#" + defaultStr);
}
}
private static bool checkInterid(string id) {
string[] interstring = new string[] { "_txt", "_lbl" };
var x = from a in interstring
where id.IndexOf(a) > -1
select a;
return x.Count() == 0;
}
static CodeResourceControl()
{
}
private static bool getLangRes(string lang) {
if (!LangMap.ContainsKey(lang)) {
string path = Application.StartupPath + @"\resources\"+lang+".txt";
if (!File.Exists(path))
{
return false;
}
string[] lines = File.ReadAllLines(path);
var map = new Dictionary<string, string>();
foreach (string line in lines)
{
string[] array = line.Split('#');
if (array.Length >= 3)
{
string key = array[0];
string chinese = array[1];
string english = array[2];
if (map.ContainsKey(key))
{
map.Remove(key);
}
map.Add(key, english);
}
}
LangMap.Add(lang, map);
return true;
}
return true;
}
public static string CurrLanguage = "";
public static void LanguageProcess(ContainerControl cc, string className)
{
if (CurrLanguage.Equals(CodeResourceControl.GetLanguage()))
{
//return;
}
//string className = cc.ClassName;
CurrLanguage = CodeResourceControl.GetLanguage();
string name = CodeResourceControl.GetString(CodeResourceControl.GetTextIdStr(className), cc.Text);
if (!name.Equals("")) { cc.Text = name; }
PreControlLanaguage(cc, className);
RefreshLanguageEvent?.Invoke();
}
private static void PreControlLanaguage(Control partentControl, string className)
{
//string className = this.ClassName;
Con_GetTxt(partentControl, out string title);
string newStr = GetString(GetTextIdStr(className, partentControl.Name), title);
if (!newStr.Equals(""))
{
Con_SetTxt(partentControl, newStr.Replace("\\n", "\n"));
}
foreach (Control con in partentControl.Controls)
{
string txt = "";
bool haslang = false;
if (con.Tag != null && con.Tag.ToString() == "not")
{
continue;
}
if (Con_GetTxt(con, out txt))
{
newStr = GetString(GetTextIdStr(className, con.Name), txt);
if (!newStr.Equals(""))
{
Con_SetTxt(con, newStr.Replace("\\n", "\n"));
//haslang = true;
}
}
if (con.Tag !=null && con.Tag.ToString() == "nochiled")
{
continue;
}
if (con.Controls.Count > 0 && !haslang)
{
PreControlLanaguage(con, className+"_"+ con.Name);
}
//Console.WriteLine(con is IList<Component>);
//Console.WriteLine(con is IList<Component>);
}
}
public static void ProcessListItem<T>(T con,string ClassName) where T: ToolStripItemCollection
{
for (int i = 0; i < con.Count; i++) {
if (con[i].Tag != null && con[i].Tag.ToString() == "not")
{
continue;
}
string txt = con[i].Text;
string newStr = GetString(GetTextIdStr(ClassName, con[i].Name), txt);
if (!newStr.Equals(""))
{
con[i].Text= newStr.Replace("\\n", "\n");
//haslang = true;
}
if (con[i] is ToolStripMenuItem) {
var tt = ((ToolStripMenuItem)con[i]);
ProcessListItem(tt.DropDownItems, tt.Name);
}
}
}
static bool Con_GetTxt(Control con, out string txt)
{
txt = "";
if (con is Label || con is Button || con is RadioButton || con is CheckBox)
{
txt = con.Text;
return true;
}
string methodname = getMethodname(con);
var t = con.GetType();
var p = t.GetProperty(methodname);
if (p != null)
{
txt = p.GetValue(con, null).ToString();
return true;
}
return false;
}
static void Con_SetTxt(Control con, string txt)
{
if (con is Label || con is Button || con is RadioButton || con is CheckBox)
{
con.Text = txt;
return;
}
string methodname = getMethodname(con);
var t = con.GetType();
var p = t.GetProperty(methodname);
if (p == null)
return;
else
p.SetValue(con, txt,null);
}
static string getMethodname(Control con) {
string methodname = "Text";
//if (con is UCBtnExt) methodname = "BtnText";
//if (con is UCTextBoxEx) methodname = "InputText";
//if (con is UCCheckBox) methodname = "TextValue";
//if (con is UCPanelTitle || con is FrmWithTitle) methodname = "Title";
//if (con is Form) methodname = "Title";
return methodname;
}
}
}