Language.cs 5.5 KB
using System;
using System.Xml;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Asa.Theme
{
    /// <summary>
    /// 多语言类
    /// </summary>
    public class Language
    {
        private XmlDocument doc;
        private XmlNode root;
        private bool save;         //是否保存
        private string[] _file;    //XML文件的路径

        //public Dictionary<string, string> dialog;

        /// <summary>
        /// 多语言
        /// </summary>
        public Language()
        { }

        /// <summary>
        /// 语言名称
        /// </summary>
        public string[] Name { set; get; }

        /// <summary>
        /// 加载语言目录
        /// </summary>
        /// <param name="path"></param>
        public void Load(string path)
        {
            if (!System.IO.Directory.Exists(path)) return;

            //dialog = new Dictionary<string, string>();
            _file = System.IO.Directory.GetFiles(path, "*.xml");
            Name = new string[_file.Length];
            for (int i = 0; i < Name.Length; i++)
            {
                doc = new XmlDocument();
                doc.Load(_file[i]);
                Name[i] = ((XmlElement)doc.LastChild).GetAttribute("Name");
            }
        }

        /// <summary>
        /// 语言切换
        /// </summary>
        /// <param name="index"></param>
        /// <param name="frm"></param>
        public void Shift(int index, params FlatForm[] frm)
        {
            if (index < 0 || index >= _file.Length) return;
            doc = new XmlDocument();
            doc.Load(_file[index]);
            save = false;

            for (int i = 0; i < frm.Length; i++)
            {
                XmlNode node = doc.LastChild.SelectSingleNode(frm[i].Name);
                if (node == null)
                {
                    XmlElement ele = doc.CreateElement(frm[i].Name);
                    ele.SetAttribute("Text", frm[i].Text);
                    doc.LastChild.AppendChild(ele);
                    root = ele;
                    save = true;
                }
                else
                {
                    root = node;
                    frm[i].DialogLang = new Dictionary<string, string>();
                    XmlNode dia = node.SelectSingleNode("Dialog");
                    if (dia != null)
                    {
                        foreach (XmlNode xn in dia)
                            frm[i].DialogLang.Add(xn.Name, ((XmlElement)xn).GetAttribute("Text"));
                    }
                }
                frm[i].Text = ((XmlElement)root).GetAttribute("Text");
                SetLang(frm[i]);
            }

            if (save)
                doc.Save(_file[index]);
            doc = null;
        }





        /// <summary>
        /// 设置控件的文本
        /// </summary>
        /// <param name="ctl"></param>
        private void SetLang(System.Windows.Forms.Control ctl)
        {
            string s;

            foreach (System.Windows.Forms.Control cc in ctl.Controls)
            {
                if (string.IsNullOrWhiteSpace(cc.Name)) continue;
                XmlNode xn = root.SelectSingleNode(cc.Name);
                if (xn == null)
                {
                    XmlElement ele = doc.CreateElement(cc.Name);
                    ele.SetAttribute("Text", cc.Text);
                    //ele.SetAttribute("Tag", cc.Tag == null ? "" : cc.Tag.ToString());
                    ele.SetAttribute("Font", ConvFont(cc.Font));
                    if (cc.Text.Length == 0)
                        ele.SetAttribute("Enabled", "false");
                    else
                        ele.SetAttribute("Enabled", "true");
                    root.AppendChild(ele);
                    save = true;
                }
                else
                {
                    s = ((XmlElement)xn).GetAttribute("Enabled");
                    if (Convert.ToBoolean(s))
                    {
                        cc.Text = ((XmlElement)xn).GetAttribute("Text");
                        s = ((XmlElement)xn).GetAttribute("Font");
                        if (s != "") cc.Font = ConvFont(s);
                    }
                }

                if (cc.HasChildren)
                {
                    root = root.SelectSingleNode(cc.Name);
                    SetLang(cc);
                }
            }
            root = root.ParentNode;
        }

        /// <summary>
        /// 转换字体
        /// </summary>
        /// <param name="s">格式:字体,字号,粗体,斜体</param>
        /// <returns></returns>
        private System.Drawing.Font ConvFont(string s)
        {
            string[] t = s.Split(',');
            float emSize = Convert.ToSingle(t[1]);
            System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
            if (t[2] == "B") style |= System.Drawing.FontStyle.Bold;
            if (t[3] == "I") style |= System.Drawing.FontStyle.Italic;
            System.Drawing.Font font = new System.Drawing.Font(t[0], emSize, style);
            return font;
        }

        /// <summary>
        /// 转换字体
        /// </summary>
        /// <param name="f">字体</param>
        /// <returns></returns>
        private string ConvFont(System.Drawing.Font f)
        {
            string[] s = new string[4];
            s[0] = f.Name;
            s[1] = f.Size.ToString();
            if (f.Bold) s[2] = "B";
            if (f.Italic) s[3] = "I";
            return string.Join(",", s);
        }

    }
}