Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5400 |  Welcome, Guest!   Register  Login
Home > Articles > WPF > Simplest Way to Implement Multilingual WPF Application

Simplest Way to Implement Multilingual WPF Application

2 vote(s)
Rating: 1.5 out of 5
Article posted by Abhi2434 on 3/6/2010 | Views: 12512 | Category: WPF | Level: Beginner red flag


Easiest way to handle Globalization in your WPF application

Download


 Download source code for Simplest Way to Implement Multilingual WPF Application


Introduction
Globalization is one of the concept that comes to mind when we create applications that might run in different geographical location. Based on the Culture code, we need to modify our application. This is very common case for many developers. I thought lets discuss what I implemented as the most cunning way to deal with this in your WPF application.
Points of Interest
Globalization is the most common issue to every application. Many of us might have searched over times to find out the most easiest way to do a Multilingual Application. Believe me, I did the same thing like you. After doing that, I found a lots of articles in net. For instance you can see one from MSDN :

http://msdn.microsoft.com/en-us/library/ms752337.aspx

If you have already read the article, you might have found that there is no such actual implementation that clearly demonstrates the concept. That is why I thought to write a concrete article for you to easily implement a truly Multilingual Application.
Trying the Sample Application



If you have downloaded the sample application, you can see I have created a login screen, just to show how it works. To try, just run the application you will find a screen just like shown above.

Put Username and Password Same, and press login button. You will see the screen below :




Next, go to Control Panel - > Regional & Language Option and change the Language to French(Canada), and Re run the application.



You will find a different screen as below :




And if you put credentials and press "connexion" (Login) button, you will see "Échec de l'authentification" (Authentication Failed).
Now I will discuss, How you can implement this type of application yourself.

The Implementation

To start implementing this application, I have added one window. I have also designed the window with some look and feel. You can see them, but this is nothing to deal with our application, so I left out the implementation of them.

After creating the initial Look and feel, which suits me, I added a folder named "Resources" (the name of which can be anything). I added two resource Dictionary to define my resource keys which I will use for my application.

 


1. Creating the Resource Files


The resource files are named as StringResources.xaml, StringResource.fr-CA.xaml etc. You can add as many resource files as you want, each of which corresponds to its own Culture.
Inside the resource files you must declare the ResourceKeys. I have used system:String to define the Resources.

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

<system:String x:Key="close">Close</system:String>
<
system:String x:Key="login">Login</system:String>
<!-- All StringResources Goes Here -->
</ResourceDictionary>

Thus you can see, in addition to adding the ResourceDictionary to the Resources Folder, I have added one namespace which points to mscorlib, and named it as system. I have then added the string references like close, login etc. which are defined to be replaced in the UI.

Similar to this, I have added another file for fr-CA, and named it as StringResource.fr-CA.xaml. This will hold all the keys that corresponds to the Resourcekeys for a machine set up with French Canadian.

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

<system:String x:Key="close">Fermer</system:String>
<
system:String x:Key="login">connexion</system:String>

</ResourceDictionary>

Thus you can see I kept the same name for the keys to ensure everything works perfectly. If you have used ASP.NET Globalization, this is almost similar to it.

2. Adding the Resource

After you create the Resource file, its time to add it to the window. To add, I have just implemented a method which you can place in some utility class. For simplicity, I left it in my window. The method looks like :

private void SetLanguageDictionary()
{
ResourceDictionary
dict = new ResourceDictionary();
switch
(Thread.CurrentThread.CurrentCulture.ToString())
{
case
"en-US":
dict.Source =
new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
break
;
case
"fr-CA":
dict.Source =
new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
break
;
default
:
dict.Source =
new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
break
;
}
this
.Resources.MergedDictionaries.Add(dict);
}


This is the most simple implementation. I have added the dictionaries directly to the window resources. I have created an object of ResourceDictionary and pointed to the file that is created for resource to its Source property. This will load the external file directly to the object. And finally added to Window.Resources using this.Resources.MergedDirectories.Add(). As you know, after compiling, WPF holds the relative path intact, so it will not create any errors during runtime.

3. Using the Resource

Finally, its now time to point ResourceKeys to your XAML to ensure it picks up the appropriate key from the ResourceDictionary. Let us add some controls :

<Button      x:Name="btnLogin"
            Click
="btnLogin_Click"

Content
="{DynamicResource login}"

Grid.Row
="3"

Grid.Column
="0"

Padding
="10"
/>
<Button x:Name="btnClose"
Content
="{DynamicResource close}"

    Click
="btnClose_Click"

  Grid.Row
="3"

        Grid.Column
="1"

       Padding
="10"
/>

You should note that I have always put the Content of the Button using DynamicResource. This is important to define, because we want the content to be replaced with the appropriate key defined to the Resource will be added later.

Hence, your application is ready.

You can download the sample application from :
    Download Sample Application - 114 KB

Points to Remember

You should note that, you must define the key to the resource file before you use as DynamicResource. Otherwise, you will end up displaying nothing in the UI.

This implementation is totally based on UI elements, there is nothing to deal with translation of dynamic UI text. I will discuss about Translation of Language for dynamic User elements later in another article.

Conclusion

Thus It is really fun to play with WPF, and as Multilingual application is most likely to be a common issue, I hope this article will help you in long run. Try the sample application and see the actual implementation.



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Abhishek Sur

Experience:3 year(s)
Home page:http://www.abhisheksur.com
Member since:Wednesday, December 02, 2009
Level:Silver
Status: [Member] [Microsoft_MVP] [MVP]
Biography:Working for last 2 and 1/2 years in .NET environment with profound knowledge on basics of most of the topics on it.
 Responses
Posted by: Kunal2383 | Posted on: 07 Mar 2010 12:33:48 AM

Nice one Abhishek

Posted by: Abhi2434 | Posted on: 07 Mar 2010 08:51:14 AM

Thank you so much Kunal...




>> Write Response - Respond to this post and get points
Related Posts

XAML as an Extensible Markup Language has great flexibilities to create objects in XAML itself and do functions like automatic binding, Conversion of Data, etc. Markup Extension allows you to truly extend your markup upto a certain extent to elevate you to write less code and design your application with richer extent. In this article I will discuss how to use existing TypeConverters and MarkupExtensions and also will show you how to create custom implementation of both of them.

It is a common problem to fix resolution related issues. Here I will demonstrate, how your UI will fix itself automatically using few tricks.

Events are not a new part to know about if you are working in Microsoft Technologies, but now when WPF has introduce a new concept called as Rounted Events, we will look into the same.

WPF added lot of flexibility and usability to the client application development. There are lots of features in WPF for enhancing the usability aspects of a client application. One of the interesting and very useful features is the WPF adorner. In this article we will discuss briefly about Adorner and how to create a simple adorner.

Ribbon Bar was introduced with Office 2007 has lately been very much popular for latest UI development. Microsoft has replaced the old Menu based application tool with more flexible and easy to learn Ribbon strips in many of its applications. So it is time for other developers to use it in their own applications. The Ribbon UI feature that was released recently bridges this to us.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:58:39 AM