C# winForm资源文件实现多语言切换

这是我目前看到过最简单的多语言切换了

操作步驟

介面上的多語

Step1.將表單的Localizable屬性設為True

image

Step2.切換表單的Language屬性為欲使用的語系

image

設完後會在分頁標籤上看到目前設定的語系

image

Step3.設定介面上欲顯示的字樣並適當的調整版面

image

image

到此,用資源檔做的多語系程式就完成了。

我們可以到檔案總管看一下,可看到Visual Studio自己幫我們產生了對應的資源檔。

image

因此,在實作介面上的多語時,我們並不需自己手動加入資源檔。

image

訊息的多語

上面的範例帶出了介面上支援多語的寫法,但卻不適用於訊息上,若要在訊息上也支援多語。請依下列步驟:

Step1.加入資源檔

資源檔依照 "資源檔名.文化特性.resx" 格式命名,如 "Resources.zh-tw.resx"。

image

Step2.設定資源檔內的訊息內容

在資源檔上連點兩下

image

設定對應的語系字串

image

image

Step3.使用資源檔內的訊息內容

這邊只要透過My.Resources去取出資源檔內的值即可,內部OR Mapping轉換都幫你做好好,可以用強型別的方式直接取用,能避免掉許多不必要的低級錯誤。

MsgBox(My.Resources.Resource1.String1)
 

其實個人習慣是命名為 "Resources.文化特性.resx",因為專案裡已偷放了一個Resources.resx資源檔,因此只需加入非預設語系的資源檔即可。

image

在使用上也會變得較為簡短

MsgBox(My.Resources.String1)

切換語系

依上面步驟操作完後,其實已具多語支援能力。當程式開啟時,會自動依照當前語系去顯示介面畫面。因此我們開啟時應該是顯示中文而不是本來的英文。若要自己切換語系可利用Threading.Thread.CurrentThread.CurrentUICulture。

Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("en")

值得注意的是,這樣的寫法是不會影響已開啟的視窗的。只有後來開啟的視窗會被切換語系。

那是否已開啟的視窗就無法切換了呢?那倒也不是。可以透過使用ResourceManager.GetString去取得對應的字串,把取出的字串再設到介面上即可。但是這也不是很好的方法,個人傾向使用ComponentResourceManager配合遞迴去把介面上的字串換成對應語系的字串。有興趣的可以參考Form.Designer.vb檔的程式碼。

image

image

注意事項

在用多國語言資源檔時,建議最後在弄其它語系的設定。因為當我們設定多語系時,Visual Studio會自動幫我們產生資源檔,且內含預設的設定值。若太早讓Visual Studi產生資源檔,則預設的設定值將只有少少的幾個,後面的設定值都需手動的設定,且每個語系的資源檔都要設定。

image

调用

SetLang("语言标识", this, typeof(窗体));

 /// <summary>
        /// 设置当前程序的界面语言
        /// </summary>
        /// <param name="lang">language:zh-CN, en-US</param>
        /// <param name="form">窗体实例</param>
        /// <param name="formType">窗体类型</param>
        public static void SetLang(string lang, Form form, Type formType)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            if (form != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
        }

        /// <summary>
        /// 遍历窗体所有控件,针对其设置当前界面语言
        /// </summary>
        /// <param name="control"></param>
        /// <param name="resources"></param>
        private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
        {
            if (control is MenuStrip)
            {
                //将资源应用与对应的属性
                resources.ApplyResources(control, control.Name);
                MenuStrip ms = (MenuStrip)control;
                if (ms.Items.Count > 0)
                {
                    foreach (ToolStripMenuItem c in ms.Items)
                    {
                        //调用 遍历菜单 设置语言
                        AppLang(c, resources);
                    }
                }
            }

            foreach (Control c in control.Controls)
            {
                resources.ApplyResources(c, c.Name);
                AppLang(c, resources);
            }
        }
        /// <summary>
        /// 遍历菜单
        /// </summary>
        /// <param name="item"></param>
        /// <param name="resources"></param>
        private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
        {
            if (item is ToolStripMenuItem)
            {
                resources.ApplyResources(item, item.Name);
                ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
                if (tsmi.DropDownItems.Count > 0)
                {
                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)
                    {
                        //if (tsmi != ToolStripSeparator)
                        //{ }
                        AppLang(c, resources);
                    }
                }
            }
        }

原文地址:https://www.cnblogs.com/LuoEast/p/7700480.html