our

Latest Blogs

An overview of WPF Localization Extension

iFour Team -June 17, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
An overview of WPF Localization Extension

Localization is used to translate application resources. It translates data of the application into localized versions, for the specific cultures supported by the application. When we are developing an application that is available in multiple languages and access globally then localization is used to convert the language of your application based on the customers' language requirements. Many people lived in a world with multiple languages. In that case, localization will be a good option because people expect useful information and functionality. Localization provides all the information with proper user language.

There are two types of localization: Static Localization and Dynamic Localization. Static localization as the name suggests is used for translating static data. It can be useful in a static project. Dynamic localization is used for dynamic data which is useful to get data in a dynamic format. It utilizes a resource dictionary for getting and translating data to the other language.

How to use WPF Localization Extension?

Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript.

Create a new WPF application and give an appropriate name to it.

After creating a new WPF application install package for localization extension. We can browse packages from Manage NuGet Packages for the solution. Search "WPFLocalizeExtension" and install it. Or we can add a package using Package Manager Console. Type this in console "Install-Package WpfLocalizeExtension -Version 3.8.0".

Installed Package

Fig: Installed Package

Now Create a Resource file for translating data from one language to another. There is one resource file already available in the project. To add new resource file right click on Properties >> Add >> Add new item >> Resource File.

Filename must be like Resources.en-US.resx it follows naming convention specific to Language and country or reign. It can be found on this reference “https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c?redirectedfrom=MSDN”.After creating the resource file add string and value which will be used in the project and change access modifier to Public from Internal.

Change access modifier in a resource file

Fig: Change access modifier in a resource file

Now add the namespace in our XAML file to use the localization and set properties to controls.

Let's understand Localization by Example

We will be going to create a simple login window with labels, text boxes, and buttons. We will use localization so first we will install the package and create two resource files for the Italian and Japanese languages.

After installing package, we will create resource files. Below files are for Italian, Japanese. And in default file we will be adding content for English.

Resource file of the Italian language

Fig: Resource file of the Italian language

Resource file for the Japanese language

Fig: Resource file for the Japanese language

Resource file for English

Fig: Resource file for English

Now we will add namespace like “xmlns:p="clr-namespace:Localization.Properties" in Xaml file to access localization. Now set properties to the controls like for label of the password we will set content= “{x: Static p: Resources. Password} ". We will be going to use strings that we have defined in *.resx file. Below code is a Xaml file in which we have done all described thing here.





 




By default, the default resource file will be used. We can change/translate our application to Japanese or Italian by setting culture explicitly when an application will run in our App.xaml.cs file.

As shown below we can add Japanese and Italian languages that we have defined in our Resources.it-IT.resx and Resources.ja-JP.resx file. Here when we run the application Italian language’s resource file, it is set in culture so that the Italian login window will display.

using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks ;
using System.Windows;

namespace Localization
{
    
    public partial class App : Application
    {
        App ()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("it-IT");
           // System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ja-JP");
        }
    }
}

When we run this application, the Italian language will display because we have set culture to the Italian resource file as shown above.

One Stop Solution for WPF Application Development ? Contact us now.

Output:

Data Bind in Textbox

Fig: Login Window for the Italian Language

Default Login Window

Fig: Default Login Window

Login Window for the Japanese Language

Fig: Login Window for the Japanese Language

Here, we have created a simple login window with labels, button, and Textboxes. To use localization extension, we have to install the package and then make resource files for languages. After creating *.resx files we have added string and values to the resource file.

After that, we have added namespace to the XAML file to use the localization and added properties to various controls. To change the language of the application we have set the culture in App.xaml.cs file so it will display language which is set in App.xaml.cs file.

Conclusion

WPF Localization is used to translate data from one language to another. When we are developing an application that is only available in a single language there is no issue but when we want the application in multiple languages and want to use the application globally, localization is used. It will translate the data of our application into other languages and provide acculturate information with the proper language. We have seen types of Localization in WPF that are static and dynamic and when to use both. We have also seen how to use Localization in our application steps by steps and have performed it by creating a simple login window with localization.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe
A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe

Angular Templates seamlessly help to expedite the development process with flawless assistance of components and other specific elements. These are dynamic and renders according...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!