WPF - 本地化



本地化是指將應用程式資源翻譯成針對應用程式支援的特定文化版本的翻譯。

當您開發應用程式並且您的應用程式僅以一種語言提供時,您就限制了客戶數量和業務規模。如果您想擴大客戶群,從而擴大業務,那麼您的產品必須面向全球受眾,並且能夠被全球受眾訪問。產品具有成本效益的本地化是接觸更多客戶的最佳和最經濟的方法之一。

在 WPF 中,使用resx檔案可以非常輕鬆地建立可本地化的應用程式,這是本地化的最簡單解決方案。讓我們來看一個簡單的例子來了解它是如何工作的:

  • 建立一個名為WPFLocalization的新 WPF 專案。

  • 在您的解決方案資源管理器中,您將在“屬性”資料夾下看到 Resources.resx 檔案。

Localization
  • 將訪問修飾符從 internal 更改為 public,以便它可以在 XAML 檔案中訪問。

Changes in WPF Localization
  • 現在新增以下字串的名稱和值,我們將在應用程式中使用它們。

Add String Name
  • 建立 Resources.resx 檔案的兩個副本,名稱分別為 Resources.en.resx 和 Resources.ru-RU.resx。這些是特定於語言和國家/地區名稱的命名約定,可以在國家語言支援 (NLS) API 參考(https://msdn.microsoft.com/en-us/goglobal/bb896001.aspx)頁面上找到。

  • 將 Resources.ru-RU.resx 中的值更改為俄語單詞,如下所示。

Change the values in Resources
  • 讓我們轉到設計視窗,並拖動三個文字框、三個標籤和三個按鈕。

  • 在 XAML 檔案中,首先新增名稱空間宣告以使用本地化資源 xmlns:p = "clr-namespace:WPFLocalization.Properties"

  • 設定所有控制元件的屬性,如下所示。在本例中,我們不會在 XAML 檔案中為標籤、按鈕和視窗標題的內容使用硬編碼字串。我們將使用在 *.resx 檔案中定義的字串。例如,對於視窗標題,我們使用在 *.resx 檔案中定義的 Title 字串,如下所示 “Title = "{x:Static p:Resources.Title}"”

  • 以下是建立控制元件並使用不同屬性進行初始化的 XAML 檔案。

<Window x:Class = "WPFLocalization.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "clr-namespace:WPFLocalization" 
   xmlns:p = "clr-namespace:WPFLocalization.Properties"
   Title = "{x:Static p:Resources.Title}" Height = "350" Width = "604">
	
   <Grid> 
      <TextBox x:Name = "textBox" HorizontalAlignment = "Left" Height = "23" 
         Margin = "128,45,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "304"/>
			
      <Label x:Name = "label" Content = "{x:Static p:Resources.Name}"
         HorizontalAlignment = "Left" Margin = "52,45,0,0" VerticalAlignment = "Top" Width = "86"/>
			 
      <TextBox x:Name = "textBox1" HorizontalAlignment = "Left" Height = "23" 
         Margin = "128,102,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "304"/> 
			
      <Label x:Name = "label1" Content = "{x:Static p:Resources.Address}" 
         HorizontalAlignment = "Left" Margin = "52,102,0,0" VerticalAlignment = "Top" Width = "86"/>
			
      <TextBox x:Name = "textBox2" HorizontalAlignment = "Left" Height = "23" 
         Margin = "128,157,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Width = "80"/>
			
      <Label x:Name = "label2" Content = "{x:Static p:Resources.Age}" 
         HorizontalAlignment = "Left" Margin = "52,157,0,0" VerticalAlignment = "Top" Width = "86"/>
			
      <Button x:Name = "button" Content = "{x:Static p:Resources.OK_Button}" 
         HorizontalAlignment = "Left" Margin = "163,241,0,0" VerticalAlignment = "Top" Width = "75"/> 
			
      <Button x:Name = "button1" Content = "{x:Static p:Resources.Cancel_Button}" 
         HorizontalAlignment = "Left" Margin = "282,241,0,0" VerticalAlignment = "Top" Width = "75"/>
			
      <Button x:Name = "button2" Content = "{x:Static p:Resources.Help_Button}" 
         HorizontalAlignment = "Left" Margin = "392,241,0,0" VerticalAlignment = "Top" Width = "75"/> 
   </Grid> 
	
 </Window>
  • 編譯並執行上述程式碼後,您將看到以下包含不同控制元件的視窗。

Localization Example
  • 預設情況下,程式使用預設的 Resources.resx。如果您想顯示在 Resources.ru-RU.resx 檔案中定義的俄語文字,則需要在程式啟動時在 App.xaml 檔案中顯式設定區域性,如下所示。

using System.Windows;

namespace WPFLocalization {
   /// <summary> 
      /// Interaction logic for App.xaml 
   /// </summary> 
	
   public partial class App : Application {
	
      App() { 
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");
         //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en"); 
      } 
   } 
}

執行應用程式後,您將看到所有文字都為俄語。

Run Application

我們建議您執行上述程式碼,併為其他文化建立 resx 檔案。

廣告

© . All rights reserved.