2011年12月21日 星期三

c# 多國語言實作

多國語言winform測試,
首先在在專案內新增FBRS.en.resxFBRS.zh-CN.resx二個語系資源檔
判斷使用者目前的語系Thread.CurrentThread.CurrentUICulture.Name;
最後進行資源檔取得的置換動作。

        /// <summary>
        /// 初始化UI多國語言介面
        /// </summary>
        private void InitUICulture()
        {
            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("FlippingBookWizard.FBRS",
                                         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");
            }
        }

沒有留言:

張貼留言