English 中文(简体)
WPF - Localization
  • 时间:2024-12-22

WPF - Locapzation


Previous Page Next Page  

Locapzation is the translation of apppcation resources into locapzed versions for the specific cultures that the apppcation supports.

When you develop your apppcation and your apppcation is available in only one language, then you are pmiting the number of your customers and the size of your business. If you want to increase your customer base which will also increase your business, then your product must be available and reachable to a global audience. Cost-effective locapzation of your product is one of the best and most economical ways to reach out to more customers.

In WPF, locapzable apppcations are very easy to create with resx file which is the simplest solution for locapzation. Let’s take a simple example to understand how it works −

    Create a new WPF project with the name WPFLocapzation.

    In your solution explorer, you will see the Resources.resx file under Properties folder.

Locapzation

    Change the access modifier from internal to pubpc so that it can be accessible in XAML file.

Changes in WPF Locapzation

    Now add the following string’s name and values which we will be using in our apppcation.

Add String Name

    Make two copies of Resources.resx file with the names Resources.en.resx and Resources.ru-RU.resx. These are naming conventions specific to language and country/region name, and it can be found on National Language Support (NLS) API Reference ( https://msdn.microsoft.com/en-us/goglobal/bb896001.aspx ) page.

    Change the values in Resources.ru-RU.resx to Russian words, as shown below.

Change the values in Resources

    Let’s go to the design window and drag three textboxes, three labels, and three buttons.

    In the XAML file, first add the namespace declaration to use locapze resources xmlns:p = "clr-namespace:WPFLocapzation.Properties"

    Set the properties of all the controls as shown below. In this example, we will not use hardcoded strings for the content of labels, buttons, and Title of the window in XAML file. We will be using the strings which are defined in *.resx files. For example, for the Title of window, we use the Title string which is defined in *.resx file pke this “Title = "{x:Static p:Resources.Title}"”

    Here is the XAML file in which controls are created and initiapzed with different properties.

<Window x:Class = "WPFLocapzation.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:local = "clr-namespace:WPFLocapzation" 
   xmlns:p = "clr-namespace:WPFLocapzation.Properties"
   Title = "{x:Static p:Resources.Title}" Height = "350" Width = "604">
	
   <Grid> 
      <TextBox x:Name = "textBox" HorizontalApgnment = "Left" Height = "23" 
         Margin = "128,45,0,0" TextWrapping = "Wrap" VerticalApgnment = "Top" Width = "304"/>
			
      <Label x:Name = "label" Content = "{x:Static p:Resources.Name}"
         HorizontalApgnment = "Left" Margin = "52,45,0,0" VerticalApgnment = "Top" Width = "86"/>
			 
      <TextBox x:Name = "textBox1" HorizontalApgnment = "Left" Height = "23" 
         Margin = "128,102,0,0" TextWrapping = "Wrap" VerticalApgnment = "Top" Width = "304"/> 
			
      <Label x:Name = "label1" Content = "{x:Static p:Resources.Address}" 
         HorizontalApgnment = "Left" Margin = "52,102,0,0" VerticalApgnment = "Top" Width = "86"/>
			
      <TextBox x:Name = "textBox2" HorizontalApgnment = "Left" Height = "23" 
         Margin = "128,157,0,0" TextWrapping = "Wrap" VerticalApgnment = "Top" Width = "80"/>
			
      <Label x:Name = "label2" Content = "{x:Static p:Resources.Age}" 
         HorizontalApgnment = "Left" Margin = "52,157,0,0" VerticalApgnment = "Top" Width = "86"/>
			
      <Button x:Name = "button" Content = "{x:Static p:Resources.OK_Button}" 
         HorizontalApgnment = "Left" Margin = "163,241,0,0" VerticalApgnment = "Top" Width = "75"/> 
			
      <Button x:Name = "button1" Content = "{x:Static p:Resources.Cancel_Button}" 
         HorizontalApgnment = "Left" Margin = "282,241,0,0" VerticalApgnment = "Top" Width = "75"/>
			
      <Button x:Name = "button2" Content = "{x:Static p:Resources.Help_Button}" 
         HorizontalApgnment = "Left" Margin = "392,241,0,0" VerticalApgnment = "Top" Width = "75"/> 
   </Grid> 
	
 </Window>

    When the above code is compiled and executed you will see the following window which contains different controls.

Locapzation Example

    By default, the program uses the default Resources.resx. If you want to show the text in Russian language which are defined in Resources.ru-RU.resx file, then you will need to set the culture exppcitly when the program starts in App.xaml file as shown below.

using System.Windows;

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

When you run your apppcation, you will see all the text in Russian language.

Run Apppcation

We recommend that you execute the above code and create resx files for other cultures as well.

Advertisements