From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
HMI.Core/Util/LangUtil.cs | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 177 insertions(+), 0 deletions(-)
diff --git a/HMI.Core/Util/LangUtil.cs b/HMI.Core/Util/LangUtil.cs
new file mode 100644
index 0000000..bed038e
--- /dev/null
+++ b/HMI.Core/Util/LangUtil.cs
@@ -0,0 +1,177 @@
+锘縰sing HMI.Core.Entity;
+using HMI.Core.Setting;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Windows;
+using System.Windows.Markup;
+
+namespace HMI.Core.Util
+{
+ public class LangUtil
+ {
+ public const string DefaultLang = "zh-CN";
+ public static List<LangInfo> langs = new List<LangInfo>() {
+ new LangInfo(DefaultLang,"涓枃"),
+ new LangInfo("en-US","English")
+ };
+
+ private static ResourceDictionary currentResource;
+
+ private static Dictionary<string, ResourceDictionary> ResourceMap
+ = new Dictionary<string, ResourceDictionary>();
+ private static Dictionary<string, JObject> JsonMap = new Dictionary<string, JObject>();
+
+ static LangUtil()
+ {
+
+ }
+
+ private static string GetLang()
+ {
+ var lang = SystemSetting.Instance.CurrentLanguage;
+ if (string.IsNullOrEmpty(lang)) lang = "zh-CN";
+ return lang;
+ }
+
+ public static void LoadLang()
+ {
+ var lang = GetLang();
+ if (!ResourceMap.ContainsKey(lang)) return;
+ var newLang = ResourceMap[lang];
+ if (currentResource == newLang)
+ {
+ return;
+ }
+ if (currentResource != null)
+ {
+ Application.Current.Resources.MergedDictionaries.Remove(currentResource);
+ }
+ currentResource = newLang;
+ Application.Current.Resources.MergedDictionaries.Add(currentResource);
+ }
+
+ public static void Load()
+ {
+ try
+ {
+ LoadAllData();
+ LoadLang();
+ }
+ finally
+ {
+
+ }
+ }
+
+ public static string Get(string key)
+ {
+ var lang = GetLang();
+ if (!JsonMap.ContainsKey(lang)) return key;
+ if (!JsonMap[lang].ContainsKey(key)) return key;
+ return JsonMap[lang][key].ToString();
+ }
+
+ private static void LoadDataByJson(string lang)
+ {
+ string langFolder = PathHelper.LangFolder;
+ string folder = Path.Combine(langFolder, lang);
+ if (!Directory.Exists(folder))
+ {
+ return;
+ }
+
+ string[] files = Directory.GetFiles(folder, "*.json", SearchOption.AllDirectories);
+
+ foreach (var path in files)
+ {
+ try
+ {
+ using (FileStream stream = File.Open(path, FileMode.Open))
+ {
+ using (StreamReader streamReader = new StreamReader(stream))
+ {
+ JsonTextReader jsonTextReader = new JsonTextReader(streamReader);
+ JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
+
+ if (!JsonMap.ContainsKey(lang))
+ {
+ JsonMap[lang] = jsonObject;
+ }
+ else
+ {
+ foreach (var item in jsonObject)
+ {
+ JsonMap[lang][item.Key] = item.Value;
+ }
+ }
+ }
+ }
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ private static void LoadXamlFromJson(string lang)
+ {
+ LoadDataByJson(lang);
+ if (!JsonMap.ContainsKey(lang)) return;
+ JObject json = JsonMap[lang];
+ var resource = new ResourceDictionary();
+ foreach (var item in json)
+ {
+ resource.Add(item.Key, item.Value.ToString());
+ }
+ ResourceMap[lang] = resource;
+ }
+
+ private static void LoadXamlFromJson2(string lang)
+ {
+ LoadDataByJson(lang);
+
+ if (!JsonMap.ContainsKey(lang)) return;
+ JObject json = JsonMap[lang];
+ var resource = new ResourceDictionary();
+ foreach (var item in json)
+ {
+ resource.Add(item.Key, item.Value.ToString());
+ }
+ ResourceMap[lang] = resource;
+ if (lang != DefaultLang)
+ {
+ var defaultMap = new ResourceDictionary();
+ foreach (var item in json)
+ {
+ defaultMap.Add(item.Key, item.Key);
+ }
+ ResourceMap[DefaultLang] = defaultMap;
+ }
+ }
+
+ private static void LoadAllData()
+ {
+ foreach (var lang in langs)
+ {
+ LoadXamlFromJson(lang.Code);
+ }
+ }
+
+ public static string Format(string key, params object[] datas)
+ {
+ return string.Format(Get(key), datas);
+ }
+ }
+
+ public static class LangReader
+ {
+ public static string lang(this object dp, string key, params object[] datas)
+ {
+ return string.Format(LangUtil.Get(key), datas);
+ }
+ }
+}
--
Gitblit v1.9.1