BaseResourceControl.cs 5.0 KB
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());
        }
    }
}