NCsvResourceControl.cs
12.7 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using TSA_V.Common;
namespace TSA_V
{
public class csvR : NCsvResourceControl
{
}
public class NCsvResourceControl : BaseResourceControl
{
public NCsvResourceControl()
{
LoadData();
var lngjson = JsonHelper.SerializeObject(LangMap);
Task.Run(() =>
{
return;
try
{
Task.Delay(10 * 1000).Wait();
if (SMF.UploadLangJsonData(lngjson, out SmfResult smfResult))
{
LogUtil.info("语言资源上传成功");
}
else
{
LogUtil.info("语言资源上传失败");
}
var lngdatas = SMF.DownloadLngData();
foreach (var lngdata in lngdatas)
{
var datalines = lngdata.resourceMap.ToList().Select((kv) => { return kv.Key + "\t\t" + kv.Value; }).ToArray();
string lngfilename = "server\\" + lngdata.lanCode + ".lngres";
Directory.CreateDirectory(Path.Combine(rPath, "server"));
File.WriteAllLines(Path.Combine(rPath, lngfilename), datalines);
}
LoadData(true);
//SetCurrentCulture(CurrLanguage);
//LanguageChangeEvent?.Invoke(null, EventArgs.Empty);
}
catch (Exception e)
{
LogUtil.info("语言资源处理出错:" + e.ToString());
}
});
//LoadData(true);
}
public static bool HasFile()
{
string file = Application.StartupPath + @"\languageResources\" + "resourcesAll.lngres";
if (File.Exists(file))
{
return true;
}
return false;
}
private char spilt = '\t';
private string rPath = Application.StartupPath + @"\languageResources\";
private void LoadData(bool loadserverdir = false)
{
try
{
string loadpath = rPath;
if (loadserverdir)
loadpath = Path.Combine(rPath, "server");
Directory.CreateDirectory(loadpath);
string[] lngresfiles = Directory.GetFiles(loadpath, "*.lngres");
string csvFileName = "";
foreach (var lngresfile in lngresfiles)
{
var lngname = Path.GetFileNameWithoutExtension(lngresfile);
if (lngname.Equals("resourcesAll"))
{
csvFileName = lngresfile;
}
}
if (File.Exists(csvFileName))
{
LoadCsvFile(csvFileName);
}
else
{
foreach (var lngresfile in lngresfiles)
{
Dictionary<string, string> newLins = new Dictionary<string, string>();
List<string> keys = new List<string>();
Dictionary<string, string> lngMap;
var lngname = Path.GetFileNameWithoutExtension(lngresfile);
if (!LangMap.ContainsKey(lngname))
{
LangMap.Add(lngname, new Dictionary<string, string>());
}
lngMap = LangMap[lngname];
string[] lines = File.ReadAllLines(lngresfile);
foreach (string line in lines)
{
if (string.IsNullOrEmpty(line))
continue;
string nline = line.Replace("\t\t", spilt + "");
string[] array = nline.Split(spilt);
if (array.Length >= 4 && array[2] == "")
{
LogUtil.error("line=" + line);
}
if (array.Length >= 2)
{
string key = array[0].Trim();
if (key.Equals("Res.OutStringMoveInfo"))
{
}
string value = array[1];
if (array.Length >= 3)
{
value = array[2];
}
if (lngMap.ContainsKey(key))
{
lngMap[key] = value;
newLins[key] = line;
}
else
{
lngMap.Add(key, value);
keys.Add(key);
newLins.Add(key, line);
}
}
}
LOG.Info($"加载{lngresfile}完成,{lngname}共{lngMap.Count}条配置");
}
}
//try
//{
// if (newLins.Count > 0)
// {
// string newFileName = lngresfile+".back"+ DateTime.Now.ToString("yyMMddHHmmss");
// List<string> newlineStr = new List<string>();
// foreach (string key in keys)
// {
// newlineStr.Add(newLins[key]);
// }
// File.WriteAllLines(newFileName, newlineStr.ToArray());
// }
//}
//catch (Exception ex)
//{
// LOG.Error("backfile error :" + ex.ToString());
//}
}
catch (Exception ex)
{
LOG.Error(ex.ToString());
}
}
private void LoadCsvFile(string csvFileName)
{
string[] lines = File.ReadAllLines(csvFileName);
int index = -1;
foreach (string line in lines)
{
index++;
if (index == 0)
{
continue;
}
if (string.IsNullOrEmpty(line))
continue;
string nline = line.Replace("\t\t", spilt + "");
string[] array = nline.Split(spilt);
if (array.Length >= 4 && array[0] == "")
{
LogUtil.error("line=" + line);
continue;
}
//顺序:key,中文,英文,法文
if (array.Length >= 2)
{
string key = array[0].Trim();
string chStr = array[1].Trim();
updateLanguageMap(ResourceCulture.China, key, chStr);
if (array.Length >= 3)
{
string enStr = array[2].Trim();
updateLanguageMap(ResourceCulture.English, key, enStr);
}
if (array.Length >= 4)
{
string frStr = array[3].Trim();
updateLanguageMap(ResourceCulture.Russian, key, frStr);
}
}
}
LOG.Info($"加载{csvFileName}完成, 共{index}条配置");
}
private void updateLanguageMap(string language, string key, string value)
{
if (!LangMap.ContainsKey(language))
{
LangMap.Add(language, new Dictionary<string, string>());
}
LangMap[language][key] = value;
}
public override string GetString(string id, string defaultStr)
{
string strCurLanguage = defaultStr;
var haslang = getLangRes(ResourceCulture.CurrLanguage);
if (!haslang)
{
return strCurLanguage;
}
try
{
LangMap[ResourceCulture.CurrLanguage].TryGetValue(id, out strCurLanguage);
if ((strCurLanguage == null || strCurLanguage.Equals("")) && (!defaultStr.Equals("")))
{
strCurLanguage = defaultStr;
NoIdLog(id, defaultStr);
}
else if (strCurLanguage != null && (!strCurLanguage.Equals("")))
{
//如果中文值和英文值和默认值都不一样,需要重新打印
if (!strCurLanguage.Equals(defaultStr) && LangMap.ContainsKey("zh-CN") && LangMap.ContainsKey("en-US"))
{
LangMap["zh-CN"].TryGetValue(id, out string zhStr);
if (zhStr != null && zhStr != "" && zhStr != defaultStr)
{
LangMap["en-US"].TryGetValue(id, out string enStr);
if (enStr != null && enStr == defaultStr)
{
}
else
{
ChangeLog(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 override string GetString(string id, string defaultStr, params object[] param)
{
string strCurLanguage = GetString(id, defaultStr);
return String.Format(strCurLanguage, param);
}
static HashSet<string> hss = new HashSet<string>();
protected override void NoIdLog(string id, string defaultStr)
{
getLangRes("zh-CN");
if (!LangMap["zh-CN"].ContainsKey(id) && checkInterid(id) && !hss.Contains(id))
{
hss.Add(id);
LOG.Info("" + id + "\t" + defaultStr);
}
}
static HashSet<string> changeSss = new HashSet<string>();
private static void ChangeLog(string id, string defaultStr)
{
//if (OpenResourceLog && !changeSss.Contains(id))
//{
// changeSss.Add(id);
// LOG.Info("CHANGE:" + id + "\t" + defaultStr);
//}
}
private static bool checkInterid(string id)
{
return true;
string[] interstring = new string[] { "_txt", "_lbl" };
var x = from a in interstring
where id.IndexOf(a) > -1
select a;
return x.Count() == 0;
}
private bool getLangRes(string lang)
{
if (!LangMap.ContainsKey(lang))
{
string path = Application.StartupPath + @"\resources\" + lang + ".lngres";
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('\t');
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;
}
}
}