An Overview Of WPF Localization Extension With An Example

Introduction of WPF Localization

 
WPF Localization is the translation of application resources into localized versions for the specific cultures that support the application.You have to translate the data into your suitable language for getting proper information.
 

What is the purpose of WPF Localization?

  • When you develop your application and your application is available in only one language, then you are limiting your customer base and the size of your business because many users cannot use this application.
  • If you want to increase your customer base and business, then your product must be available and reachable to a global audience with easy information on your suitable language. Cost-effective localization of your product is one of the best and most economical ways to reach out to more customers and join your application.
  • As we know there are so many people with so many languages living in this world. In such a scenario, the WPF localization would have an outstanding impact. 
  • Because people expect useful information and fine functionality in the project without any barrier.
  • It can provide you with all the information onthe appropriate user language.

What are the types of WPF Localization?

 
There are two types of WPF localization in the WPF.
  1. Static WPF Localization.
  2. Dynamic WPF Localization
Static WPF Localization
  • Static WPF localization is useful for translating the static data into another language.
  • It can be useful for the static project because it can be defined as all the information to be translated into another language. As many people make use of the internet for getting information, this scenario would beuseful for another country resident for getting information easily.
Dynamic WPF Localization
  • Dynamic WPF localization to use for the Dynamic data.
  • This type of Localization is useful for getting the data in a dynamic format.
  • There is so much information to get in the dynamic format and in such cases, it would be easy to translate into another language according to the user. Hence these features play a crucial role.
  • Dynamic WPF localization utilizes the resource dictionary file for getting and translating the data from one language to another.
Resource Dictionary File Example
 
Code
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  3. xmlns:sys="clr-namespace:System;assembly=mscorlib">  
  4. </ResourceDictionary>  
Add the namespace for the resource dictionary using.
 
Namespace
 
xmlns:sys="clr-namespace:System;assembly=mscorlib"
 
Add the data in resource dictionary file in the following code as shown below,
 
Code
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  3. xmlns:sys="clr-namespace:System;assembly=mscorlib">  
  4. <sys:String x:Key="ResourceDictionaryName">Loc-en-US</sys:String>  
  5. <sys:String x:Key="WindowTitle">Dynamic Localizzation in wpf</sys:String>  
  6. <sys:String x:Key="FirstNameLabel">First Name</sys:String>  
  7. <sys:String x:Key="LastNameLabel">Last Name</sys:String>  
  8. </ResourceDictionary>  

How to use WPF Localization in your project?

 
Step 1
 
Create a new project for WPF_Localization.
 
Step 2
 
Then install the packages of WPF Localization Extension of the latest version.
 
An Overview Of WPF Localization Extension With An Example
 
Step 3
 
After the installation, open the Resources.resx file.
 
An Overview Of WPF Localization Extension With An Example
 
Step 4
 
We need to change the access modifier to the public and now you can use the resources in Main Page.xml
 
Step 5
 
You can see the design of the user in resources.resx file.
 
Step 6
 
Create a new file, follow the step.
 
“Right click of your project name -> click add button -> then click new item -> then click visual C# items -> then find resource file ->
Then to define the name of your file”.
 
Note
But the file name to be declared as an example of Resources.fr- FR.resx, Resources.gr-GR.
 
File Name: Resources.resx file in the below,
 
An Overview Of WPF Localization Extension With An Example
 
File Name: Resources.fr-FR.resx file in the below.
 
An Overview Of WPF Localization Extension With An Example
 
Step 7
 
Now, you can add the file in MainWindow.xaml file using the namespace in below,
 
Example,
 
“xmlns:CC="clr-namespace:WpfApp1Localization.Properties"”
 
Step 8
 
Now you can call the resources file using App.xaml.cs file.
 
Code
  1. namespace WpfApp1Localization {  
  2.     public partial class App: Application {  
  3.         App() {  
  4.             System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("gr-GR");  
  5.             //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");  
  6.         }  
  7.     }  
  8. }  
Step 9
 
Now you save all the files of the project and run the project.
 
Step 10
 
Output of the project in WPF Localization using the static WPF Localization in WPF
 
Output
 
An Overview Of WPF Localization Extension With An Example
 
Example of the WPF Localization
  • Now first create a new project Project_name:WPFLocalizationDemo
  • Then create a two file of Resources.en.resx and Resources.gr-GR.resx
  • Add the data field into two files.
Resources.en.resx
 
An Overview Of WPF Localization Extension With An Example
 
Resources.gr-Gr.resx
 
An Overview Of WPF Localization Extension With An Example
 
Description of the Table data
 
Create a button1 – Submit text will be submitting.
 
Create a button2 – Registration the user.
 
Create a button3 – Cancel or reset the data in the log in form.
 
Create a Label1 - Login for the heading and title.
 
Create a Label2 – User name for Entering User Name.
 
Create a Label3 – Password for the enter Password.
  • Add the resources file in the XAML file using namespace in the below:
Code
  1. <Window x:Class="WpfApp1Localization.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.     xmlns:local="clr-namespace:WpfApp1Localization"  
  7.     xmlns:CC="clr-namespace:WpfApp1Localization.Properties"  
  8.   
  9. mc:Ignorable="d"  
  10.   
  11. Title="{x:Static CC:Resources.Log_In}" Height="450" Width="800" Background="Chocolate">  
  12.     <Grid>  
  13.         <TextBox HorizontalAlignment="Left" Height="23" FontSize="12" Margin="412,152,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>  
  14.         <TextBox HorizontalAlignment="Left" Height="23" FontSize="12" Margin="412,233,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>  
  15.         <Label x:Name = "label1" Content="{x:Static CC:Resources.Log_In}" HorizontalAlignment="Left" FontSize="18" Margin="338,72,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,2.491"/>  
  16.         <Label x:Name = "label2" Content="{x:Static CC:Resources.User_Name}" HorizontalAlignment="Left" FontSize="12" Margin="245,152,0,0" VerticalAlignment="Top"/>  
  17.         <Label x:Name = "label3" Content="{x:Static CC:Resources.Password}" HorizontalAlignment="Left" FontSize="12" Margin="245,233,0,0" VerticalAlignment="Top"/>  
  18.         <Button x:Name = "button1" Content="{x:Static CC:Resources.Submit}" HorizontalAlignment="Left" Margin="338,330,0,0" VerticalAlignment="Top" Width="75"/>  
  19.         <Button x:Name="Button2" Content="{x:Static CC:Resources.Registration}" HorizontalAlignment="Left" Margin="245,340,0,0" VerticalAlignment="Top" Width="75"/>  
  20.         <Button x:Name="Button3" Content="{x:Static CC:Resources.Cancel}" HorizontalAlignment="Left" Margin="532,335,0,0" VerticalAlignment="Top" Width="75"/>  
  21.     </Grid>  
  22. </Window>   
Calling the resources file using the app.xaml.cs file
 
Code
  1. namespace WpfApp1Localization {  
  2.     public partial class App: Application {  
  3.         App() {  
  4.             System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("gr-GR");  
  5.             //System.Threading.Thread.CurrentThread.CurrentUICulture =   
  6.             new System.Globalization.CultureInfo("en");  
  7.         }  
  8.     }  
  9. }  
The output of the program or project,
 
Talking about the output design, it would look like the following.
 
English language Login Form can be seen as follow,
 
An Overview Of WPF Localization Extension With An Example
 
German Language Login Form in the below,
 
An Overview Of WPF Localization Extension With An Example
 

Conclusion

 
In this blog, we have learned about WPF Localization, its importance, its types, and how it works practically. This is one of the major concepts to understand the overview of WPF and it would be helpful for an application to resolve the issue of a language barrier.


Next Recommended Reading Popup Example in WPF