Overview and examples of WPF localization extensions

Overview and examples of WPF localization extensions

WPF localization is the transformation of application resources into localized versions that support the specific culture of the application.

最后更新 4/5/2021 10:34 PM
沙漠尽头的狼
预计阅读 8 分钟
分类
WPF
标签
.NET WPF localization

Introduction to WPF localization

WPF localization is the transformation of application resources into localized versions that support the specific culture of the application. You must convert the data into the appropriate language to obtain the appropriate information.

What is the purpose of WPF localization?

  • When you develop an application and your application is available in only one language, then you limit the size of your customer base and business because many users cannot use the application.
  • If you want to increase your customer base and business, then your products must be available and accessible to a global audience with simple messages in your appropriate language. Cost-effective product localization is one of the best and most economical ways to reach more customers and join applications.
  • As we know, there are so many people in the world and so many languages. In this case, WPF localization will have a significant impact
  • Because people want no obstacles to useful information and good functionality in the project.
  • It can provide you with all information about the appropriate user language.

What types of WPF localization are there?

There are two types of WPF localization in WPF.

  1. Static WPF localization.
  2. Dynamic WPF localization

Static WPF positioning

  • Static WPF localization is very useful for converting static data into another language.
  • It is useful for static projects because it can be defined as all information to be translated into another language. Since many people use the Internet to obtain information, this situation can be useful for residents of another country because they can easily obtain information.

Dynamic WPF localization

  • Dynamic WPF localization for dynamic data.
  • This type of localization is very useful for getting data in a dynamic format.
  • There is too much information to obtain in dynamic formats, and in this case, it is easy to translate into another language based on the user's needs. Therefore, these characteristics play a crucial role.
  • Dynamic WPF localization utilizes resource dictionary files to retrieve data and translate it from one language to another.

** Sample 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>

Use resource dictionaries by adding namespaces.

** Namespace **

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

Add the data from the resource dictionary file to the following code, as shown below,

** 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 **

然后安装最新版本的 WPF 本地化扩展包:WPFLocalizeExtension

WPF本地化扩展包:WPFLocalizeExtension

** Step 3 **

Add resource files

添加资源文件

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

Newly added resource documents:

新增加的资源文件

** Step 4 **

我们需要将访问修饰符更改为 public,现在您可以在主页面中使用刚添加的资源了(即右键资源文件属性,将自定义工具(Custom Tool)改为PublicResXFileCodeGenerator即可,否则运行程序报错)

** Step 5 **

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

** Step 6 **

Create a new file and follow the steps.

"Right-click the project name-> click Add button-> then click New Item-> then click visual C#Project-> then find resource files-> then define file names.“

Note

But declare it as the file name of the Resources.fr-FR.resx and Resources.gr-GR examples.

File name: The following x file,

Resources.resx

File name: The following Resource.fr-FR.resx file.

Resources.fr-FR.resx

** Step 7 **

Now, you can add the file to the MainWindow. l file using the following namespace,

** Example, **

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

** Step 8 **

You can now use the App.xaml.cs file to call resource files, that is, 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 for the project and run the project.

** Step 10 **

Use static WPF localization output items in WPF in WPF localization

output

WPF Localization Sample 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 resource document **

Create a button 1-Submit text will be submitted.

Create a button 2-Register a user.

Create a button 3-Cancel or reset data in the login form.

Create a tab 1-title and prompt for the login form.

Create a tab 2-a tab prompt for entering a user name.

Create a tab 3-tab prompt for entering a password.

  • Add resource files by adding namespaces in the following XAML file:

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 resource files 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 a program or project, **

When it comes to output design, it looks like this.

The English login form is as follows:

英文登录表单

The following is the 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 outlined by understanding WPF and will help applications address language barriers.

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

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读