CodeResourceControl.cs
4.4 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CodeLibrary
{
public class CodeResourceControl
{
//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 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>();
public delegate string GetLanguageDelegate();
public static event GetLanguageDelegate GetLanguageEvent;
public static string GetLanguage()
{
return English;
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 = "";
try
{
if (GetLanguage().Equals(China))
{
chineseMap.TryGetValue(id,out strCurLanguage);
}else if (GetLanguage().Equals(English))
{
englishMap.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);
}
}
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)
{
HDLogUtil.info("No id:[" + id + "], please add,use default string :" + defaultStr);
}
static CodeResourceControl()
{
try
{
chineseMap = new Dictionary<string, string>();
englishMap = new Dictionary<string, string>();
string path = Application.StartupPath + @"\resources\resources.txt";
string[] lines = File.ReadAllLines(path);
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 (chineseMap.ContainsKey(key))
{
chineseMap.Remove(key);
}
if (englishMap.ContainsKey(key))
{
englishMap.Remove(key);
}
chineseMap.Add(key, chinese);
englishMap.Add(key, english);
}
}
HDLogUtil.error("加载中英文配置文件完成,长度【" + chineseMap.Count + "】");
}
catch (Exception ex)
{
HDLogUtil.error("CodeResourceControl" + ex.ToString());
}
}
}
}