WPF Localization Extension Overview and Examples

WPF Localization Extension Overview and Examples

WPF localization is the process of converting application resources into localized versions that support specific cultures of the application.

Last updated 4/5/2021 10:34 PM
沙漠尽头的狼
8 min read
Category
WPF
Tags
.NET WPF Localization

Introduction to WPF Localization

WPF localization is the process of translating application resources into a localized version that supports the specific culture of the application. You must convert the data into the appropriate language to obtain relevant information.

What is the Purpose of WPF Localization?

  • When you develop an application and it is only available in one language, you limit your customer base and business scale, because many users cannot use this application.
  • If you want to increase your customer base and business, your product must be available and accessible to a global audience through simple information in the appropriate language. Cost-effective product localization is one of the best and most economical ways to reach more customers and join the application.
  • As we know, there are many people and many languages in this world. In this context, WPF localization will have a significant impact
  • because people expect useful information and good features in the project without any barriers.
  • It can provide you with all the information about the appropriate user language.

What are the Types of WPF Localization?

There are two types of WPF localization in WPF.

  1. Static WPF Localization.
  2. Dynamic WPF Localization.

Static WPF Localization

  • Static WPF localization is useful for converting static data into another language.
  • It is useful for static projects because it can define all information to be translated into another language. Since many people use the internet to obtain information, this situation is useful for residents of other countries because they can easily access the information.

Dynamic WPF Localization

  • Dynamic WPF localization is used for dynamic data.
  • This type of localization is useful for obtaining data in a dynamic format.
  • There is too much information to obtain in dynamic format; in this case, it is easy to translate it into another language according to the user's needs. Therefore, these features play a crucial role.
  • Dynamic WPF localization uses resource dictionary files to obtain data and translate it from one language to another.

Example of a Resource Dictionary File

Code

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
</ResourceDictionary>

Using the resource dictionary by adding a namespace.

Namespace

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Add data from the resource dictionary file into the code below as follows:

Code

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
  <sys:String x:Key="ResourceDictionaryName">Loc-en-US</sys:String>
  <sys:String x:Key="WindowTitle">Dynamic Localizzation in wpf</sys:String>
  <sys:String x:Key="FirstNameLabel">First Name</sys:String>
  <sys:String x:Key="LastNameLabel">Last Name</sys:String>
</ResourceDictionary>

How to Use WPF Localization in a Project?

Step 1

Create a new project for WPF localization.

Step 2

Then install the latest version of the WPF localization extension package: WPFLocalizeExtension.

WPF Localization Extension Package: WPFLocalizeExtension

Step 3

Add a resource file

Add Resource File

After installation, add a resource file (as shown above), and open the Resources.resx file.

Newly added resource file:

Newly Added Resource File

Step 4

We need to change the access modifier to public. Now you can use the newly added resources in the main page (i.e., right-click on the resource file properties, change the Custom Tool to PublicResXFileCodeGenerator, otherwise the program will report an error at runtime).

Step 5

You can see the user design in the resources.resx file.

Step 6

Create a new file, following the steps.

"Right-click on the project name -> Click the Add button -> Then click New Item -> Then click Visual C# items -> Then find Resource File -> Then define the file name."

Note

But declare the file names as Resources.fr-FR.resx and Resources.gr-GR for example.

File name: The Resources.resx file below,

Resources.resx

File name: The Resources.fr-FR.resx file below.

Resources.fr-FR.resx

Step 7

Now, you can add the file in the MainWindow.xaml file using the following namespace:

Example,

"xmlns:CC="clr-namespace:WpfApp1Localization.Properties""

Step 8

Now you can call the resource file using the App.xaml.cs file, i.e., read the local language when starting the project and switch languages.

Code

namespace WpfApp1Localization {
    public partial class App: Application {
        App() {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("gr-GR");
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
        }
    }
}

Step 9

Now save all files of the project and run the project.

Step 10

Use static WPF localization in WPF to output the project in WPF localization.

Output

WPF Localization Example Project

  • Now first create a new project with the project name: WPFLocalizationDemo
  • Then create two files: Resources.en.resx and Resources.gr-GR.resx
  • Add data fields to both files.

Resources.en.resx

Resources.en.resx

Resources.gr-Gr.resx

Resources.gr-Gr.resx

Explanation of table data in the resource files

Create Button 1 – The submit text will be submitted.

Create Button 2 – Register user.

Create Button 3 – Cancel or reset data in the login form.

Create Label 1 – Title and prompt for the login form.

Create Label 2 – Label prompt for entering username.

Create Label 3 – Label prompt for entering password.

  • Add the resource file in the XAML file below by adding the namespace:

Code

<Window
  x:Class="WpfApp1Localization.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:WpfApp1Localization"
  xmlns:CC="clr-namespace:WpfApp1Localization.Properties"
  mc:Ignorable="d"
  Title="{x:Static CC:Resources.Log_In}"
  Height="450"
  Width="800"
  Background="Chocolate"
>
  <Grid>
    <TextBox
      HorizontalAlignment="Left"
      Height="23"
      FontSize="12"
      Margin="412,152,0,0"
      TextWrapping="Wrap"
      VerticalAlignment="Top"
      Width="120"
    />
    <TextBox
      HorizontalAlignment="Left"
      Height="23"
      FontSize="12"
      Margin="412,233,0,0"
      TextWrapping="Wrap"
      VerticalAlignment="Top"
      Width="120"
    />
    <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"
    />
    <label
      x:Name="label2"
      Content="{x:Static CC:Resources.User_Name}"
      HorizontalAlignment="Left"
      FontSize="12"
      Margin="245,152,0,0"
      VerticalAlignment="Top"
    />
    <label
      x:Name="label3"
      Content="{x:Static CC:Resources.Password}"
      HorizontalAlignment="Left"
      FontSize="12"
      Margin="245,233,0,0"
      VerticalAlignment="Top"
    />
    <button
      x:Name="button1"
      Content="{x:Static CC:Resources.Submit}"
      HorizontalAlignment="Left"
      Margin="338,330,0,0"
      VerticalAlignment="Top"
      Width="75"
    />
    <button
      x:Name="Button2"
      Content="{x:Static CC:Resources.Registration}"
      HorizontalAlignment="Left"
      Margin="245,340,0,0"
      VerticalAlignment="Top"
      Width="75"
    />
    <button
      x:Name="Button3"
      Content="{x:Static CC:Resources.Cancel}"
      HorizontalAlignment="Left"
      Margin="532,335,0,0"
      VerticalAlignment="Top"
      Width="75"
    />
  </Grid>
</Window>

Call the resource file using the app.xaml.cs file

Code

namespace WpfApp1Localization {
    public partial class App: Application {
        App() {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("gr-GR");
            //System.Threading.Thread.CurrentThread.CurrentUICulture =
            new System.Globalization.CultureInfo("en");
        }
    }
}

Output of the program or project,

Regarding the output design, it looks like the following.

The English login form is as follows:

English Login Form

The German login form is as follows:

German Login Form

Conclusion

In this blog, we learned about WPF localization, its importance, its types, and how it actually works. This is one of the main concepts for understanding the WPF overview, and it will help applications solve the problem of language barriers.

Original link: https://www.c-sharpcorner.com/blogs/an-overview-of-wpf-localization-extension-with-an-example

Keep Exploring

Related Reading

More Articles
Same category / Same tag 9/13/2025

Migration Series from WPF to Avalonia: Why I Must Migrate My WPF Application to Avalonia

In the past few years, our host computer software has mainly been developed using WPF and WinForm . These technologies work well on the Windows platform and have accompanied us from small-scale trial production to the current stage of large-scale delivery. However, with business development and changes in customer requirements, the single Windows technology stack has gradually become a hurdle we must overcome.

Continue Reading
Same category / Same tag 1/26/2025

Implementing Internationalization in WPF Using Custom XML Files

This article details the method of implementing internationalization in WPF applications using custom XML files, including installing the necessary NuGet packages, dynamically retrieving the language list, dynamically switching languages, using translated strings in code and XAML interfaces, and provides a source code link to help developers easily achieve internationalization in WPF applications.

Continue Reading