实现多国语言处理

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web.UI;
using AjaxControlToolkit;

/// <summary>
/// 基页面,实现多国语言处理等处理
/// </summary>
public abstract class BasePage : System.Web.UI.Page
{
    private ArrayList arrPageList = new ArrayList();

    //CultureInfo对象
    protected System.Globalization.CultureInfo cultureInfo;

    //ContentPlaceHolder控件
    protected ContentPlaceHolder mpContentPlaceHolder;

    //页面ID
    protected string pageId = "";

    //语言设置(默认中文)
    protected string language = "zh-CN";

    //操作类型字符串
    protected string operationTypeString = "";

    #region 构造函数
    /// <summary>
    /// 构造函数
    /// </summary>
    public BasePage()
    {
        //根据类名取得画面ID
        pageId = this.GetType().Name.Replace("_aspx", "");
        pageId = pageId.Substring(0, 1).ToUpper() + pageId.Substring(1);
    }
    #endregion

    #region 画面初始化前处理
    /// <summary>
    /// 画面初始化前处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Page_PreLoad(object sender, EventArgs e)
    {
        //设置CultureInfo信息
        cultureInfo = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;

        //从资源文件取得画面信息
        string resourceValue = (string)base.GetLocalResourceObject("pageInfoResource");

        //设置画面Title
        f_setPageTitle(resourceValue);

        if (!IsPostBack)
        {

            //设定页面控件,实现多语言和权限控制
            f_SetControls(this.Page, "", "", pageId);

            //删除检索标识
            string clearSession = (string)Request["ClearSession"];

            if (clearSession != null && clearSession.ToLower() == "yes")
            {
                // 关键字列表
                ArrayList keys = new ArrayList();
                int keycount = 0;

                keycount = Session.Keys.Count;
                for (int i = 0; i < keycount; i++)
                {
                    keys.Add((string)Session.Keys[i]);
                }

                foreach (string key in keys)
                {
                    // 保证SYSTEM_DATA不被删除。
                    if (key == "SYSTEM_DATA")
                    {
                        continue;
                    }
                    // 删除其余的Session
                    Session.Remove(key);
                }

            }
        }

    }
    #endregion

    #region 画面控件设置,包括多语言设置和权限设置
    /// <summary>
    /// 画面控件设置,包括多语言设置和权限设置
    /// </summary>
    /// <param name="page">画面</param>
    /// <param name="globalMenuId">主菜单ID</param>
    /// <param name="localMenuId">子菜单ID</param>
    /// <param name="pageId">页面ID</param>
    public void f_SetControls(System.Web.UI.Page page, string globalMenuId, string localMenuId, string pageId)
    {
        //若当前画面使用了MasterPage,则从MasterPage的ContentPlaceHolder取得个别页面
        if (page.Master != null)
        {

            //从资源文件取得画面信息
            string resourceValue = (string)base.GetLocalResourceObject("pageInfoResource");

            //在MasterPage设置画面信息
            Label lbltitle = (Label)page.Master.FindControl("lbltitle");
            lbltitle.Text = resourceValue;

            //设置画面Title
            //f_setPageTitle(resourceValue);

            //从MasterPage取得当前画面
            mpContentPlaceHolder = (ContentPlaceHolder)page.Master.FindControl("mainContent");

            int count = mpContentPlaceHolder.Controls.Count;

            for (int i = 0; i < count; i++)
            {
                //取得当前控件
                Control currentCtrl = mpContentPlaceHolder.Controls[i];

                //若当前控件为Panel,做以下处理
                if (currentCtrl.GetType().Name == "Panel")
                {
                    //对Panel控件进行多语言处理
                    f_BindControl(currentCtrl);

                    //遍历Panel控件的子控件
                    foreach (Control ctrl in currentCtrl.Controls)
                    {
                        //对取得的子控件进行多语言处理
                        f_BindControl(ctrl);
                    }
                }
                // 如果当前控件为TabContainer,进行以下处理
                else if (currentCtrl.GetType().Name == "TabContainer")
                {
                    int tabPanel = currentCtrl.Controls.Count;
                    for (int j = 0; j < tabPanel; j++)
                    {
                        // 取得当前控件
                        Control currentTabCtr = currentCtrl.Controls[j];

                        // 如果当前控件为TabPanel,进行以下处理
                        if (currentTabCtr.GetType().Name == "TabPanel")
                        {
                            // 对TabPanel控件进行多语言处理
                            f_BindControl(currentTabCtr);

                            Control contrlPanel = currentTabCtr.Controls[0];

                            foreach (Control ctrl in contrlPanel.Controls)
                            {
                                // 如果当前控件为Panel,进行以下处理
                                if (ctrl.GetType().Name == "Panel")
                                {
                                    foreach (Control subContrl in ctrl.Controls)
                                    {
                                        // 对取得的子控件进行多语言处理
                                        f_BindControl(subContrl);
                                    }
                                }

                                // 对取得的子控件进行多语言处理
                                f_BindControl(ctrl);
                            }
                        }
                    }
                }
                // 如果当前控件为UpdatePanel,进行以下处理
                else if (currentCtrl.GetType().Name == "UpdatePanel")
                {
                    // 对Panel控件进行多语言处理
                    f_BindControl(currentCtrl);

                    // 遍历Panel控件的子控件
                    foreach (Control ctrl in currentCtrl.Controls[0].Controls)
                    {
                        // 若当前控件为Panel,做以下处理
                        if (ctrl.GetType().Name == "Panel")
                        {
                            // 对Panel控件进行多语言处理
                            f_BindControl(ctrl);

                            // 遍历Panel控件的子控件
                            foreach (Control subCtrl in ctrl.Controls)
                            {
                                // 对取得的子控件进行多语言处理
                                f_BindControl(subCtrl);
                            }
                        }

                        // 对取得的子控件进行多语言处理
                        f_BindControl(ctrl);
                    }
                }
                // 如果当前控件为MultiView,进行以下处理
                else if (currentCtrl.GetType().Name == "MultiView")
                {
                    // 遍历MultiView控件的子控件
                    foreach (Control ct in currentCtrl.Controls)
                    {
                        foreach (Control ctrl in ct.Controls)
                        {
                            // 若当前控件为Panel,做以下处理
                            if (ctrl.GetType().Name == "Panel")
                            {
                                // 对Panel控件进行多语言处理
                                f_BindControl(ctrl);

                                // 遍历Panel控件的子控件
                                foreach (Control subCtrl in ctrl.Controls)
                                {
                                    // 对取得的子控件进行多语言处理
                                    f_BindControl(subCtrl);
                                }
                            }

                            // 对取得的子控件进行多语言处理
                            f_BindControl(ctrl);
                        }
                        f_BindControl(ct);
                    }
                }
                else
                {
                    //对取得的当前控件进行多语言处理
                    f_BindControl(currentCtrl);
                }
            }

        }
        else
        {
            for (int i = 0; i < page.Controls.Count; i++)
            {
                //取得当前控件
                foreach (Control currentCtrl in page.Controls[i].Controls)
                {
                    //若当前控件为Panel,进行以下处理
                    if (currentCtrl.GetType().Name == "Panel")
                    {
                        //对Panel控件处理
                        f_BindControl(currentCtrl);

                        //遍历Panel上的控件
                        foreach (Control ctrl in currentCtrl.Controls)
                        {
                            //对取得的子控件处理
                            f_BindControl(ctrl);
                        }
                    }
                    else
                    {
                        //对取得的当前控件处理
                        f_BindControl(currentCtrl);
                    }

                }
            }
        }
    }
    #endregion
   

    #region 页面显示控件名与资源文件进行绑定
    /// <summary>
    /// 页面显示控件名与资源文件进行绑定
    /// </summary>
    /// <param name="ctrl">控件</param>
    private void f_BindControl(Control ctrl)
    {
        //取得的控件做以下处理
        if (!string.IsNullOrEmpty(ctrl.ID))
        {
            //取得页面的控件的资源名
            string resourceKey = ctrl.ID.ToString() + "Resource";

            //通过资源名取得对应的值
            string resourceValue = (string)base.GetLocalResourceObject(resourceKey);

            //将取得的值设置到页面控件
            if (ctrl.GetType().Name == "Button")
            {
                //Button控件处理
                ((Button)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "Label")
            {
                //Label控件处理
                ((Label)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "HyperLink")
            {
                //HyperLink控件处理
                ((HyperLink)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "LinkButton")
            {
                //LinkButton控件处理
                ((LinkButton)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "Panel")
            {
                //Panel控件处理
                ((Panel)ctrl).GroupingText = resourceValue;
            }
            else if (ctrl.GetType().Name == "Literal")
            {
                //Literal控件处理
                ((Literal)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "CheckBox")
            {
                //CheckBox控件处理
                ((CheckBox)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "RadioButton")
            {
                //RadioButton控件处理
                ((RadioButton)ctrl).Text = resourceValue;
            }
            else if (ctrl.GetType().Name == "DropDownList")
            {
                //DropDownList控件处理

                int index = 1;
                //对DropDownList的ListItem进行多语言处理
                foreach (ListItem listItem in ((DropDownList)ctrl).Items)
                {
                    //TODO 控件命名待确定
                    resourceKey = ctrl.ID.ToString() + "ListItemResource" + index;
                    resourceValue = (string)base.GetLocalResourceObject(resourceKey);
                    listItem.Text = resourceValue;
                    index++;
                }
            }
            else if (ctrl.GetType().Name == "RadioButtonList")
            {
                //RadioButtonList控件处理

                int index = 1;
                //对RadioButtonList的ListItem进行多语言处理
                foreach (ListItem listItem in ((RadioButtonList)ctrl).Items)
                {
                    //TODO 控件命名待确定
                    resourceKey = ctrl.ID.ToString() + "ListItemResource" + index;
                    resourceValue = (string)base.GetLocalResourceObject(resourceKey);
                    listItem.Text = resourceValue;
                    index++;
                }
            }
            else if (ctrl.GetType().Name == "TabPanel")
            {
                //TabPanel控件处理
                ((TabPanel)ctrl).HeaderText = resourceValue;
            }
            else if (ctrl.GetType().Name == "GridView")
            {


                //遍历GridView Templete's Colums
                int intColums = ((GridView)ctrl).Columns.Count;
                for (int j = 0; j < intColums; j++)
                {
                    //对取得的子控件进行多语言处理
                    resourceKey = ((GridView)ctrl).Columns[j].HeaderText.ToString() + "Resource";
                    string resourceValue2 = (string)base.GetLocalResourceObject(resourceKey);
                    ((GridView)ctrl).Columns[j].HeaderText = resourceValue2;
                }
            }
        }
    }
    #endregion

    #region 设置页面的Title
    /// <summary>
    /// 设置页面的Title
    /// </summary>
    /// <param name="title"></param>
    public void f_setPageTitle(string pageInfo)
    {
        this.Page.Title = pageInfo;
    }
    #endregion
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章