使用附屬資源程序集本地化WinForms控件
本文檔演示了使用附屬資源程序集(包含已翻譯資源的庫)本地化 .NET 應用程序的最常見方法,這些程序集是 .NET Framework 提供的標準本地化機制,用于構建多語言應用程序。
提示:您也可以使用自定義本地化程序來翻譯應用程序,但是有些控件只有附屬程序集才能翻譯(例如,XtraReports中的Search對話框)。我們建議您使用附屬程序集來翻譯應用程序,另外請注意,如果使用自定義本地化程序則忽略附屬程序集。
獲取附屬組件
在大多數情況下,沒有必要手動翻譯所有特定于語言的資源,因為DevExpress包含針對各種語言和文化的附屬程序集。
程序包括某些語言(DE、ES、JA)的資源程序集,這些資源不會自動安裝,您可以自行決定安裝它們。
提示:本地化的資源可能不完整,可以導航到下載其他語言的社區來源翻譯,此服務允許您修改現有的翻譯,并編譯和下載附屬程序集。
提示:確保附屬程序集的主要版本(例如,v23.1)與項目中DevExpress庫的主要版本匹配。
添加附屬程序集到您的應用程序
將包含附屬程序集的文件夾(例如,es)復制到應用程序的EXE文件目錄。
例如,要包含 German assemblies,請將名為de的文件夾從\Bin\Framework\目錄復制到應用程序的EXE文件目錄(通常是Bin\ Debug\子目錄),您可以更改Output path項目來選擇另一個位置。
提示:附屬程序集必須放置在名稱與這些程序集目標的區域性名稱相匹配的文件夾中。例如,如果附屬程序集針對中立的 German culture (“de”),則它們必須放置在“de”文件夾中(而不是“de-DE”或任何其他文件夾)。
下圖說明了在應用程序的目錄中放置附屬程序集的位置:
提示:
除了將附屬程序集復制到應用程序的文件夾中,還可以將它們安裝到gac中。請注意,如果存在具有相同名稱的不同資源程序集則一個位于應用程序EXE文件附近,另一個安裝在GAC中—來自GAC的程序集將具有更高的優先級。
將應用程序與附屬組件一起部署到最終用戶的機器上。當啟動時,程序自動確定操作系統的區域性,加載相應的資源程序集,并相應地翻譯顯示文本。
下圖演示了德語本地化的 和:
選擇與系統文化不同的文化
將所需區域性的縮寫分配給CurrentThread.CurrentUICulture和CurrentThread.CurrentCulture屬性,手動指定應用程序的區域性(無論目標操作系統是什么)。
請注意,雖然您可以為CurrentUICulture屬性(例如,“de”)使用短區域性縮寫(中性區域性),但CurrentCulture屬性需要設置為特定的區域性(例如,“de- de”)。
以下示例代碼設置應用程序的德語區域設置:
C#:
、using System.Globalization; using System.Threading; // ... static void Main() { // Create a new object, representing the German culture. CultureInfo culture = CultureInfo.CreateSpecificCulture("de-DE"); // The following line localizes the application's user interface. Thread.CurrentThread.CurrentUICulture = culture; // The following line localizes data formats. Thread.CurrentThread.CurrentCulture = culture; // Set this culture as the default culture for all threads in this application. // Note: The following properties are supported in .NET Framework 4.5+: CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; // Note that the above code should be added BEFORE calling the Run() method. Application.Run(new Form1()); }
VB.NET:
Imports System.Globalization Imports System.Threading ' ... Shared Sub Main() ' Create a new object, representing the German culture. Dim Culture = CultureInfo.CreateSpecificCulture("de-DE") ' The following line localizes the application's user interface. Thread.CurrentThread.CurrentUICulture = Culture ' The following line localizes data formats. Thread.CurrentThread.CurrentCulture = Culture ' Set this culture as the default culture for all threads in this application. ' Note: The following properties are supported in .NET Framework 4.5+: CultureInfo.DefaultThreadCurrentCulture = Culture CultureInfo.DefaultThreadCurrentUICulture = Culture ' Note that the above code should be added BEFORE calling the Run() method. Application.Run(New Form1()) End Sub
提示:如果您打算允許最終用戶從多個預定義區域性中進行選擇,請在加載表單之前執行此代碼,雖然可以動態地分配CurrentCulture和CurrentUICulture屬性,但是沒有重載所有資源的通用方法。
故障處理
我的應用程序未本地化。
要使用附屬資源程序集,請將它們放置在適當的位置,以便公共語言運行庫(CLR)可以輕松地找到它們。在某些情況下,應用程序可能無法定位附屬程序集,如果是這種情況,您可以使用標準的.NET Fuslogvw.exe實用程序檢查它在哪里查找程序集。閱讀MSDN中的以下主題以了解更多信息:
我的應用程序中的某些項目未本地化。
發生這種情況可能是因為特定區域性的附屬程序集不完整,您可以使用DevExpress本地化服務翻譯缺失的字符串。
我的安全權限禁止運行該應用程序。
ecurityPermission必須設置為ControlThread才能更改Thread.CurrentThread的區域性。注意,操作線程是危險的,因為與線程相關的安全狀態。因此,只有在必要的時候才應該給值得信賴的代碼權限。應用程序不能在半受信任代碼中更改線程區域性