diff --git a/SunnyUI/Common/UIniFile.cs b/SunnyUI/Common/UIniFile.cs
index 7b1abade..54530f67 100644
--- a/SunnyUI/Common/UIniFile.cs
+++ b/SunnyUI/Common/UIniFile.cs
@@ -149,7 +149,7 @@ namespace Sunny.UI
/// 结果
public string Read(string section, string key, string Default)
{
- byte[] buffer = new byte[4096];
+ byte[] buffer = new byte[4096 * 16];
if (Default == null)
{
Default = "";
diff --git a/SunnyUI/Forms/UIBaseForm.cs b/SunnyUI/Forms/UIBaseForm.cs
index befb33b4..8cfa01a1 100644
--- a/SunnyUI/Forms/UIBaseForm.cs
+++ b/SunnyUI/Forms/UIBaseForm.cs
@@ -1083,10 +1083,5 @@ namespace Sunny.UI
SelectedPage?.Translate();
this.TranslateOther();
}
-
- public class CodeTranslator : BaseCodeTranslator
- {
- public string ButtonInfo { get; set; }
- }
}
}
diff --git a/SunnyUI/Style/UTranslate.cs b/SunnyUI/Style/UTranslate.cs
index c3338204..f74532e8 100644
--- a/SunnyUI/Style/UTranslate.cs
+++ b/SunnyUI/Style/UTranslate.cs
@@ -19,10 +19,13 @@
* 2021-07-23: V3.0.5 增加文件说明
******************************************************************************/
+using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
+using System.Text;
using System.Windows.Forms;
using System.Xml;
@@ -49,19 +52,6 @@ namespace Sunny.UI
bool ShowBuiltInResources { get; set; }
}
- public interface ICodeTranslator
- {
- void Load(IniFile ini);
- }
-
- public abstract class BaseCodeTranslator : ICodeTranslator
- {
- public void Load(IniFile ini)
- {
-
- }
- }
-
public static class TranslateHelper
{
private static List Ingores =
@@ -210,7 +200,7 @@ namespace Sunny.UI
ini.Write(section, key, UIStyles.CultureInfo.DisplayName);
key = UIStyles.CultureInfo.LCID.ToString() + ".EnglishName";
if (ini.Read(section, key, "") != UIStyles.CultureInfo.EnglishName)
- ini.Write(section, UIStyles.CultureInfo.LCID.ToString() + ".EnglishName", UIStyles.CultureInfo.EnglishName);
+ ini.Write(section, key, UIStyles.CultureInfo.EnglishName);
Dictionary Ctrls2 = new Dictionary();
Dictionary Ctrls3 = new Dictionary();
@@ -231,7 +221,7 @@ namespace Sunny.UI
}
var formControls = form.GetTranslateControls().Where(p => p.FormTranslatorProperties != null); ;
- section = UIStyles.CultureInfo.LCID + ".Form";
+ section = UIStyles.CultureInfo.LCID + ".FormResources";
foreach (var control in formControls)
{
if (control.ShowBuiltInResources) continue;
@@ -261,7 +251,161 @@ namespace Sunny.UI
}
}
+ ini.UpdateFile();
ini.Dispose();
}
}
+
+ public class IniCodeTranslator where TConfig : IniCodeTranslator, new()
+ {
+ ///
+ /// 当前实例。通过置空可以使其重新加载。
+ ///
+ public static TConfig Current
+ {
+ get
+ {
+ if (current != null) return current;
+ current = new TConfig();
+ current.SetDefault();
+ return current;
+ }
+ }
+
+ ///
+ /// 设置默认值
+ ///
+ public virtual void SetDefault()
+ {
+ }
+
+ ///
+ /// 实体对象
+ ///
+ private static TConfig current;
+
+ public void Load(Form form)
+ {
+ if (!UIStyles.MultiLanguageSupport) return;
+ if (!(form is UIBaseForm || form is UIPage)) return;
+ Dir.CreateDir(Dir.CurrentDir() + "Language");
+
+ string thisFullName = form.GetType().FullName;
+
+ string filename = Dir.CurrentDir() + "Language\\" + thisFullName + ".ini";
+ bool exists = File.Exists(filename);
+
+ try
+ {
+ IniFile ini = new IniFile(filename, Encoding.UTF8);
+ string section = "Info";
+ if (!exists)
+ {
+ const string warning = "注意:请先关闭应用程序,然后再修改此文档。否则修改可能会应用程序生成代码覆盖。";
+ ini.Write(section, "Warning", warning);
+ }
+
+ string key = UIStyles.CultureInfo.LCID.ToString() + ".DisplayName";
+ if (ini.Read(section, key, "") != UIStyles.CultureInfo.DisplayName)
+ ini.Write(section, key, UIStyles.CultureInfo.DisplayName);
+ key = UIStyles.CultureInfo.LCID.ToString() + ".EnglishName";
+ if (ini.Read(section, key, "") != UIStyles.CultureInfo.EnglishName)
+ ini.Write(section, key, UIStyles.CultureInfo.EnglishName);
+
+ ConcurrentDictionary idents = InitIdents(current);
+
+ foreach (var ident in idents.Values)
+ {
+ ident.Section = UIStyles.CultureInfo.LCID + ".CodeResources";
+
+ if (ini.KeyExists(ident.Section, ident.Key))
+ {
+ ident.Value = ini.Read(ident.Section, ident.Key, "");
+ ident.Show = true;
+ }
+ else
+ {
+ ini.Write(ident.Section, ident.Key, ident.Value);
+ ident.Show = false;
+ }
+ }
+
+ ini.UpdateFile();
+ LoadConfigValue(current, idents);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
+
+ private static ConcurrentDictionary InitIdents(T config)
+ {
+ ConcurrentDictionary concurrentDictionary = new ConcurrentDictionary();
+ foreach (PropertyInfo needProperty in config.GetType().GetNeedProperties())
+ {
+ Ident ident = new Ident
+ {
+ Key = needProperty.Name,
+ Show = true,
+ Description = needProperty.Description(),
+ IsList = needProperty.PropertyType.IsList(),
+ Value = needProperty.GetValue(config, null).ToString()
+ };
+
+ ConfigSectionAttribute customAttribute = needProperty.GetCustomAttribute();
+ ident.Section = ((customAttribute != null) ? customAttribute.Section : "");
+ ConfigPropertyAttribute customAttribute2 = needProperty.GetCustomAttribute();
+ ident.Caption = ((customAttribute2 != null) ? customAttribute2.Caption : "");
+ ident.Unit = ((customAttribute2 != null) ? customAttribute2.Unit : "");
+ ident.Description = ((customAttribute2 != null) ? customAttribute2.Description : "");
+ ConfigIndexAttribute customAttribute3 = needProperty.GetCustomAttribute();
+ ident.Index = customAttribute3?.Index ?? (32767 + concurrentDictionary.Count);
+ ident.Show = customAttribute3?.Show ?? true;
+ if (ident.Description.IsNullOrEmpty())
+ {
+ ident.Description = needProperty.DisplayName() ?? needProperty.Description();
+ }
+
+ if (ident.Description.IsNullOrEmpty())
+ {
+ ident.Description = "";
+ }
+
+ if (!concurrentDictionary.ContainsKey(ident.Key))
+ {
+ concurrentDictionary.TryAdd(ident.Key, ident);
+ }
+ }
+
+ return concurrentDictionary;
+ }
+
+ private static void LoadConfigValue(T config, ConcurrentDictionary idents)
+ {
+ foreach (PropertyInfo needProperty in config.GetType().GetNeedProperties())
+ {
+ object value = needProperty.GetValue(config, null);
+ if (idents.TryGetValue(needProperty.Name, out Ident ident) && ident.Show)
+ {
+ Type propertyType = needProperty.PropertyType;
+ if (propertyType == typeof(string))
+ {
+ object value2 = idents[needProperty.Name].Value;
+ needProperty.SetValue(config, Convert.ChangeType((value2 == null) ? value : value2, propertyType), null);
+ continue;
+ }
+
+ if (ConvertEx.CanConvent(propertyType))
+ {
+ object value3 = ConvertEx.StringToObject(idents[needProperty.Name].Value, propertyType, value);
+ needProperty.SetValue(config, Convert.ChangeType(value3, propertyType), null);
+ continue;
+ }
+
+ throw new ApplicationException("不支持此类型: " + propertyType.FullName);
+ }
+ }
+ }
+ }
}