BaseResourceControl.cs
5.0 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
using log4net;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TSA_V.Common;
namespace TSA_V
{
public class BaseResourceControl
{
public static readonly ILog LOG = LogManager.GetLogger("LngResource");
public Dictionary<string, Dictionary<string, string>> LangMap = new Dictionary<string, Dictionary<string, string>>();
public Dictionary<string, string> defaultMap = new Dictionary<string, string>();
public virtual void SetCurrentCulture(string name)
{
//ResourceControl.GetStrEvent += GetString;
//ResourceControl.GetStringEvent += GetString;
//ResourceControl.GetLanEvent += GetLanguage;
//UserFromControl.UserControlResource.GetStrEvent += GetString;
//UserFromControl.UserControlResource.GetStringEvent += GetString;
if (string.IsNullOrEmpty(name))
{
name = "en-US";
}
//CurrLanguage = name;
Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
}
public virtual string GetString(string id, params object[] param)
{
return GetString(id, id, param);
}
public virtual string GetString(string id, string defaultStr)
{
string strCurLanguage = "";
try
{
// 使用入口程序集(EXE)来查找资源,避免在库(DeviceLibrary)程序集内找不到资源的问题
Assembly entryAsm = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly() ?? Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("TSA_V.Properties.Resource", entryAsm);
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
strCurLanguage = rm.GetString(id, ci).Trim();
if (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 virtual string GetString(string id, string defaultStr, params object[] param)
{
string strCurLanguage = "";
try
{
// 使用入口程序集(EXE)来查找资源,避免在库(DeviceLibrary)程序集内找不到资源的问题
Assembly entryAsm = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly() ?? Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("TSA_V.Properties.Resource", entryAsm);
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
strCurLanguage = rm.GetString(id, ci).Trim();
if (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 String.Format(strCurLanguage, param);
}
protected virtual void NoIdLog(string id, string defaultStr)
{
LogUtil.debug("No id:[" + id + "], please add,use default string :" + defaultStr);
if (!defaultMap.ContainsKey(id))
{
defaultMap.Add(id, defaultStr);
}
}
public virtual void LogDefaultMap()
{
if (defaultMap == null || defaultMap.Count <= 0)
{
return;
}
//LogUtil.info("开始打印缺少的文字配置" + defaultMap.Count);
StringBuilder builder = new StringBuilder();
builder.Append("开始打印缺少的文字配置" + defaultMap.Count + "\r\n");
foreach (string key in defaultMap.Keys)
{
string value = defaultMap[key];
builder.Append($"<data name = \"{key}\" xml:space = \"preserve\"> <value> {value} </value> </data> \r\n");
//LogUtil.info(" 缺少文字配置[" + key + "] 默认值[" + value + "]");
}
builder.Append("结束打印缺少的文字配置");
LogUtil.info(builder.ToString());
}
}
}