LanguageWwitchover.cs 7.6 KB
using Asa.FaceControl;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml;
using Control = System.Windows.Controls.Control;
using Font = System.Drawing.Font;
using FontStyle = System.Drawing.FontStyle;

namespace SmartScan.SetControl.WPF
{
   
    public class LanguageWwitchover
    {
        private class ClsLangText
        {
            public string Name = "";

            public string Text = "";

            public Font Font = null;
        }
        public static List<string> Name { get; private set; } = new List<string>();
        private static readonly Dictionary<string, Dictionary<string, ClsLangText>> langDict = new Dictionary<string, Dictionary<string, ClsLangText>>();
        public static string CurrentLng { get; private set; } = "zh-CN";
        private static string filePath;

        public static void LoadPath(string path)
        {
            langDict.Clear();
            filePath = path;
            string[] files = Directory.GetFiles(path, "*.lngres");
            string[] array = files;
            foreach (var file in array)
            {
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
                //var langDictTemp = new Dictionary<string, string>();
                if (fileNameWithoutExtension.ToLower() == "en-US".ToLower())
                {
                    Name.Add("English");
                }
                else if (fileNameWithoutExtension.ToLower() == "zh-CN".ToLower())
                {
                    Name.Add("简体中文");
                }

                langDict.Add(fileNameWithoutExtension, new Dictionary<string, ClsLangText>());
                var lines = File.ReadAllLines(file);
                string[] array3 = lines;
                foreach (var line in array3)
                {
                    string[] parts = line.Split('\t');
                    if (parts.Length >= 3)
                    {
                        var key = parts[0];
                        var text = parts[2];
                        string s = ((parts.Length > 3) ? parts[3] : "");
                        ClsLangText clsLangText = new ClsLangText();
                        clsLangText.Font = ConvFont(s);
                        clsLangText.Name = key;
                        clsLangText.Text = text;
                        if (!langDict[fileNameWithoutExtension].ContainsKey(key))
                        {
                            langDict[fileNameWithoutExtension].Add(key, clsLangText);
                        }

                    }
                }
                //langDict.Add(fileNameWithoutExtension, langDictTemp);
            }
        }
        private static Font ConvFont(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return null;
            }

            string[] array = s.Split(',');
            float emSize = Convert.ToSingle(array[1]);
            FontStyle fontStyle = FontStyle.Regular;
            if (array[2] == "B")
            {
                fontStyle |= FontStyle.Bold;
            }

            if (array[3] == "I")
            {
                fontStyle |= FontStyle.Italic;
            }

            return new Font(array[0], emSize, fontStyle);
        }
        public static void LoadLanguage(string name)
        {
            CurrentLng = name;
        }

        public static string GetText(string key)
        {
            if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(key))
            {
                return langDict[CurrentLng][key].ToString();
            }
            return key; // Return key if text not found
        }

        public static void SetLanguage(DependencyObject parent)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                SetLanguage(child);

                if (child is Control control)
                {
                    // 执行你需要的操作,例如设置文本或字体
                    Console.WriteLine($"Control Type: {control.GetType().Name}, Name: {control.Name}, Content:{control.Name}");
                    var key = control.Name; // 假设控件名称是资源键
                    var text = LanguageWwitchover.GetText(key);
                    if (control is System.Windows.Controls.Button button)
                    {
                        try
                        {
                            text = LanguageWwitchover.GetText(langDict[CurrentLng][key].Text);
                            button.Content = text;
                        }
                        catch (Exception)
                        {

                        }
                       
                    }
                    // 示例:设置控件文本
                    // string text = LanguageSwitcher.GetText("YourKeyHere");
                    // control.Content = text;

                    // 示例:设置控件字体
                    // if (control is TextBlock textBlock)
                    // {
                    //     var font = new FontFamily("微软雅黑"), emSize = 12, fontStyle = FontStyles.Normal;
                    //     textBlock.FontFamily = font;
                    //     textBlock.FontSize = emSize;
                    //     textBlock.FontStyle = fontStyle;
                    // }
                }
            }
            //if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(frm.Name + "_" + frm.Name))
            //{
            //    frm.Text = langDict[CurrentLng][frm.Name + "_" + frm.Name].Text;
            //    frm.TitleFont = langDict[CurrentLng][frm.Name + "_" + frm.Name].Font;
            //    SetLang(frm.Name, frm);
            //}
        }
        private static void SetLang(string frmname, System.Windows.Forms.Control ctl)
        {
            if (!langDict.ContainsKey(CurrentLng))
            {
                return;
            }

            foreach (System.Windows.Forms.Control control in ctl.Controls)
            {
                if (control.Name == "")
                {
                    continue;
                }

                string key = frmname + "_" + control.Name;
               
               

                if (control.HasChildren)
                {
                    SetLang(frmname, control);
                }
            }
        }
        private static bool HasChinese(string txt)
        {
            if (Regex.IsMatch(txt.ToString(), "[\\u4E00-\\u9FA5]+"))
            {
                return true;
            }

            return false;
        }
        public static void UpdateUI(UIElement element, string key)
        {
            if (element is FrameworkElement fe)
            {
                fe.Dispatcher.Invoke(() =>
                {
                    var text = GetText(key);
                    if (fe is TextBlock tb)
                    {
                        tb.Text = text;
                    }
                    else if (fe is System.Windows.Controls.Button btn)
                    {
                        btn.Content = text;
                    }
                    // Add more controls as needed
                });
            }
        }

    }

  
}