2011年11月7日 星期一

c# 多國語言實作

以下範例為多國語言winform測試,
首先在在專案內新增FBRS.en.resxFBRS.zh-CN.resx二個語系資源檔
判斷使用者目前的語系Thread.CurrentThread.CurrentUICulture.Name;
最後進行資源檔取得的置換動作。
//add other name space
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
        /// <summary>
        /// 初始化UI多國語言介面
        /// </summary>
        private void InitUICulture()
        {
        CultureInfo ci;
        ResourceManager rm;
            string userUICulture = Thread.CurrentThread.CurrentUICulture.Name;
            if (userUICulture != "zh-TW")
            {
                switch (userUICulture)
                {
                    case "en":
                        this.ci = new CultureInfo("en");
                        break;
                    case "zh-CN":
                        this.ci = new CultureInfo("zh-CN");
                        break;
                }

                Thread.CurrentThread.CurrentUICulture = this.ci;
                //第一個參數:專案根目錄名稱.資源檔名(basename)
                //語系命名不用加
                ResourceManager rm = new ResourceManager("Multilanguage.Form1",
                                                      Assembly.GetExecutingAssembly());

                this.lblLogin.Text = rm.GetString("lblLogin");
                this.lblPassword.Text = rm.GetString("lblPassword");
                this.lblLoginMsg.Text = rm.GetString("lblLoginMsg");
                this.btnLogin.Text = rm.GetString("btnLogin");
            }
        }

沒有留言:

張貼留言