生成中文语言包
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Krypton.Navigator;
|
||||
using Krypton.Toolkit;
|
||||
using LeadDataBaseUtility;
|
||||
using Newtonsoft.Json;
|
||||
using NX_LogUtility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -20,7 +21,8 @@ namespace CleanMachineHMI.Utils
|
||||
/// </summary>
|
||||
public static class frmUtil
|
||||
{
|
||||
|
||||
public static List<ThemeLanguage> znCNLang = new List<ThemeLanguage>();
|
||||
|
||||
/// <summary>
|
||||
/// 给一个字符串进行MD5加密
|
||||
/// </summary>
|
||||
@@ -118,44 +120,25 @@ namespace CleanMachineHMI.Utils
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteFile(string strDir, string filePath, string txt)
|
||||
{
|
||||
FileStream fs = null;
|
||||
StreamWriter sw = null;
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(strDir))//如果日志文件存放的文件夹不存在,就新建一个文件夹。
|
||||
{
|
||||
Directory.CreateDirectory(strDir);
|
||||
}
|
||||
|
||||
if (File.Exists(filePath))//日志文件存放路径存在,向日志文件追加记录。
|
||||
{
|
||||
fs = new FileStream(filePath, FileMode.Append);
|
||||
}
|
||||
else//日志文件存放路径不存在,创建日志文件。
|
||||
{
|
||||
fs = new FileStream(filePath, FileMode.Create);
|
||||
}
|
||||
sw = new StreamWriter(fs);
|
||||
//开始写入
|
||||
sw.Write(txt);
|
||||
//清空缓冲区
|
||||
sw.Flush();
|
||||
//关闭流
|
||||
sw.Close();
|
||||
fs.Close();
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
/// <summary>
|
||||
/// 递归获取子控件
|
||||
/// </summary>
|
||||
/// <param name="control"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Control> GetAllControls(Control control)
|
||||
{
|
||||
var list = new List<Control>();
|
||||
foreach (Control con in control.Controls)
|
||||
{
|
||||
list.Add(con);
|
||||
if (con.Controls.Count > 0)
|
||||
{
|
||||
list.AddRange(GetAllControls(con));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sw != null)
|
||||
sw.Close();
|
||||
if (fs != null)
|
||||
fs.Close();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -167,24 +150,24 @@ namespace CleanMachineHMI.Utils
|
||||
{
|
||||
try
|
||||
{
|
||||
Control[] tmpC = form.Controls.Find("label2", true);
|
||||
foreach (Control item in form.Controls)
|
||||
foreach (Control item in GetAllControls(form))
|
||||
{
|
||||
var key = form.Name + "|" + item.Name;
|
||||
if (!GlobalHelper.ThemeLanguage.ContainsKey(key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
AddzhCN(key, item.Text, "");
|
||||
if (item is KryptonGroupBox)
|
||||
{
|
||||
((KryptonGroupBox)item).Values.Heading = GlobalHelper.ThemeLanguage[key];
|
||||
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
item.Text = GlobalHelper.ThemeLanguage[key];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -199,7 +182,7 @@ namespace CleanMachineHMI.Utils
|
||||
/// <param name="dataGrid"></param>
|
||||
/// <param name="formName"></param>
|
||||
/// <param name="languageCode"></param>
|
||||
public static void SetDataViewColumnText(this DataGridView dataGrid, string formName, string languageCode)
|
||||
public static void SetDataViewColumnText(this DataGridView dataGrid, string formName)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -210,7 +193,8 @@ namespace CleanMachineHMI.Utils
|
||||
{
|
||||
continue;
|
||||
}
|
||||
item.HeaderText=GlobalHelper.ThemeLanguage[key];
|
||||
AddzhCN(key, item.HeaderText, "");
|
||||
item.HeaderText = GlobalHelper.ThemeLanguage[key];
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -237,6 +221,7 @@ namespace CleanMachineHMI.Utils
|
||||
}
|
||||
else
|
||||
{
|
||||
AddzhCN(key, defaultValue, "");
|
||||
return GlobalHelper.ThemeLanguage[key];
|
||||
}
|
||||
}
|
||||
@@ -247,17 +232,24 @@ namespace CleanMachineHMI.Utils
|
||||
}
|
||||
}
|
||||
|
||||
public static List<string> GetComBoboxItemArr(this Control form, string code, string controlType)
|
||||
public static List<string> GetComBoboxItemArr(this Control form, string controlCode, string controlType)
|
||||
{
|
||||
try
|
||||
{
|
||||
var key = form.Name + "|" + code;
|
||||
var key = form.Name + "|" + controlCode;
|
||||
if (!GlobalHelper.ThemeLanguage.ContainsKey(key))
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
var control = (KryptonComboBox)form.Controls.Find(controlCode,true)[0];
|
||||
var str = "";
|
||||
foreach (var item in control.Items) {
|
||||
str += $"{item},";
|
||||
}
|
||||
|
||||
AddzhCN(key, "", string.IsNullOrEmpty(str) ? GlobalHelper.ThemeLanguage[key] : str.TrimEnd(new char[] { ',' }));
|
||||
return GlobalHelper.ThemeLanguage[key].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
}
|
||||
|
||||
@@ -268,6 +260,85 @@ namespace CleanMachineHMI.Utils
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void DB2Json()
|
||||
{
|
||||
var tmpThemeLanguage = new List<ThemeLanguage>();
|
||||
var table = LeadDbHelperSQLite.Query("SELECT FORM_CODE,CONTROL_CODE,CONTROL_NAME FROM SYS_LANGUAGE_CONTROL WHERE FORM_CODE <> 'frmMain'").Tables[0];
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
|
||||
var part = tmpThemeLanguage.FirstOrDefault(x => x.formCode == row["FORM_CODE"].ToString() && x.controlCode == row["CONTROL_CODE"].ToString());
|
||||
if (part == null)
|
||||
{
|
||||
tmpThemeLanguage.Add(new ThemeLanguage
|
||||
{
|
||||
formCode = row["FORM_CODE"].ToString(),
|
||||
controlCode = row["CONTROL_CODE"].ToString(),
|
||||
controlName = row["CONTROL_NAME"].ToString(),
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
if (part.options == null)
|
||||
{
|
||||
part.options = new List<string> { part.controlName, row["CONTROL_NAME"].ToString() };
|
||||
part.controlName = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
part.options.Add(row["CONTROL_NAME"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(tmpThemeLanguage);
|
||||
WriteLangFile("en.json", str1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写文本文件
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="txt"></param>
|
||||
private static void WriteLangFile(string fileName, string txt)
|
||||
{
|
||||
var fileDir = System.Windows.Forms.Application.StartupPath.Split(new string[] { @"bin\Debug" }, StringSplitOptions.RemoveEmptyEntries)[0] + "Lang\\";
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(fileDir))//如果文件夹不存在,就新建一个文件夹。
|
||||
{
|
||||
Directory.CreateDirectory(fileDir);
|
||||
}
|
||||
File.WriteAllText(fileDir + fileName, txt);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddzhCN(string key, string name, string options)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(options)) {
|
||||
Console.WriteLine(key+"空");
|
||||
return;
|
||||
}
|
||||
var codes = key.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (znCNLang.Any(x => x.formCode == codes[0] && x.controlCode == codes[1]))
|
||||
{
|
||||
return;
|
||||
}
|
||||
znCNLang.Add(new ThemeLanguage
|
||||
{
|
||||
formCode = codes[0],
|
||||
controlCode = codes[1],
|
||||
controlName = string.IsNullOrEmpty(options) ? name : null,
|
||||
options = string.IsNullOrEmpty(options) ? null : options.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(),
|
||||
});
|
||||
var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(znCNLang);
|
||||
WriteLangFile("zh-CN.json", str1);
|
||||
}
|
||||
}
|
||||
|
||||
public class ThemeLanguage
|
||||
@@ -283,8 +354,10 @@ namespace CleanMachineHMI.Utils
|
||||
/// <summary>
|
||||
/// 控件名称
|
||||
/// </summary>
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string controlName { get; set; }
|
||||
|
||||
public List<string> options { get; set; } = new List<string>();
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public List<string> options { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user