Visual Studio對於.NET程式的本地化提供了完整的解決方案,為了實現軟體的國際化與本地化,本文給出了簡單的說明與技巧。
一、表單的國際化解決方案建立一個WinForm解決方案後,選擇主表單,右擊查看屬性,找到Localizable屬性,將其置為True,然後找到Language屬性,選擇你需要切換的語言,比如英語(美國)、中文(簡體,中國)等。此時根據實際情況設計該Language下的表單樣式及語言。
圖1 Form的屬性設定
圖2 根據選擇的語言,自動產生的資源檔二、使用代碼實現切換顯示語言
在視窗初始化之前使用System.Threading.Thread.CurrentThread.CurrentUICulture 設定即可。如下面代碼所示。
using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace LocationForm{ static class Program { /// <summary> /// 應用程式的主進入點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); Application.Run(new Form1()); } }}三:實現效果
語言選擇為zh-CN的顯示效果
語言選擇為en-US的顯示效果四、參考文獻《編碼和本地化》 http://msdn.microsoft.com/zh-cn/library/h6270d0z.aspx【注】當然你可以選擇其他的方式進行解決,比如利用xml配置或者資源檔配置等方式實現本地化國際化解決方案,但是利用VS本身提供的解決方案是個優選方案。