Introduction to Globalization and Localization
A decade or two ago, it was cool to use terms like "global village", "information superhighway" and so forth. Now they're clichés. A few years ago, if a small scale enterprise had clients from across the globe, it was considered to be a step ahead of others. Today it's commonplace to have clients and branch offices scattered across the globe. As a developer or programmer, what does this mean for you? It means that you need to be aware of concepts like globalization and localization as well as how to implement them. It means that you need to be prepared to deliver globally-ready applications, no matter what technology you use. The fact that an enterprise or line-of-business application is globally-ready is not a luxury anymore, it's an essential requirement.
But what is globalization? Let's explore.
Globalization
According to the MSDN library, "Globalization is the process of designing and developing an application that supports localized user interfaces and regional data for users in multiple cultures." If your user interface displays currency information, for example, and you display the currency in a format suitable to the current culture then that's globalization. Not just the currency symbols but the decimal places, the thousands separator all differ from culture to culture. This is just an example though. Globalization can involve lot more than just changing currency formats.
Localization
So then what's localization? Again, we turn to the MSDN library which defines Localization as a process "where you customize the application for specific cultures or regions. If the globalization and localizability steps have been performed correctly, localization should consist primarily of translating the user interface."
Localization in .NET (Windows Forms)
So let's look at this from the angle of a .NET application developer. Implementing globalization and localization was a reasonably straightforward task in Windows Forms. You created a number of resource files, each of which contained data specific to a particular culture. Within the application, you set the current culture to a desired culture using the CultureInfo class and retrieved the resources from the resource files. Accordingly, the UI elements would be rendered according to the local culture. The number of resource files depended on the amount of cultures the application would use. That was all there was to a simple implementation of localization.
Localization in Silverlight
But localization in Silverlight is not as straightforward, though it isn't complicated either.
The following steps describe with the help of an example how to implement localization in Silverlight. The example is based on Silverlight 4 and uses Visual Studio 2010.
Create a Silverlight application named LocSilverlight as shown in Figure 1.
Figure 1: Creating the application

Create a default resource file by using Project->Add New Item and selecting Resource File in the New Item Dialog Box. Figure 2 shows how to add this.
Figure 2: Adding a resource file

Name the file as Resource1.resx. The file is opened in the Resource editor window.
Rename the default resource string String1 to Text and add the text "Good Day" in the Value box. Change its Access modifier to Public as shown in Figure 3.
Figure 3: Adding resource strings

Create two more resource files named Resource1.fr-Fr.resx and Resource1.de-DE.resx. Add similar strings to them in German and French respectively. Figure 4 and 5 show the strings added in the respective resource files.
Figure 4: Adding resource strings in French

Figure 5: Adding resource strings in German

Create an alias and namespace for the assembly in the XAML markup. For example,
xmlns:Resources="clr-namespace:LocSilverlight"
Create a static resource named Resource1 which refers to the Resource file name.
Add a TextBlock element in the XAML markup. In the binding for the TextBlock, specify the static resource name given earlier. For example,
This will specify the binding source for the Text property as the static resource. The net effect will be that the Text property will be bound to the resource string in the corresponding resource file.
The complete XAML markup is shown below:
Open the codebehind of the App.xaml file, add the following line in the constructor
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Thread.CurrentThread.CurrentCulture.ToString());
This will set the current user interface culture based on the current system culture. You will need to add the System.Threading and System.Globalization namespaces in order for this code to work. The Thread class is defined in the System.Threading namespace and the CultureInfo class is defined in the System.Globalization namespace. Instead of using this approach, you can also set the culture by using initparameters from the HTML file.
Save the project. Now you need to edit the .cs.proj file so that you can specify which cultures are supported by this Silverlight application. But you cannot edit the .cs.proj file as long as the project is in use. Therefore, you need to unload it temporarily.
Right-click on the project in the Solution Explorer and select Unload the project. The project will be unloaded.
Again, right-click on the project in the Solution Explorer and select "Edit .csproj file".
Look for the <SupportedCultures> element present in this file. Add the following content under this element:
<SupportedCultures>en-US;fr-FR;de-DE;</SupportedCultures>
Reload the project by right-clicking on project name in the Solution Explorer and selecting Reload Project.
Save, build and run the application. The default output would be Good Day.
Go to Control Panel from the Start menu and choose Region and Language options. In the first tab, set the format to German (Germany).
Execute the application once more. You will see output similar to Figure 6.
Figure 6: Output

As observed, the process in Silverlight localizing an application is quite different from that in Windows Forms. This example was a very basic, trivial application with just a TextBlock displaying a string. Let's now look at how to localize a ListBox in a Silverlight application using an approach similar to the above. This time around, a few more steps would be required. Let's see how to achieve this localization.
Most of the procedure is the same as the above. The difference is in extracting several strings from the resources file and then binding the ListBox to them.
Create resource files as shown in Figure 7, 8, and 9.
Figure 7: Resource strings for default culture: US English

Figure 8: Resource strings for German culture

Figure 9: Resource strings for French culture

Create a property Greet in the codebehind class, Main.xaml.cs and assign it to the DataContext of ListBox as shown below:
Bind the ListBox in the XAML as shown below:
The complete XAML markup for the application is shown below:
Save the project. Then unload the project as explained earlier and edit the .csproj file. Add the content shown below in the .csproj file:
Go to Control Panel from the Start menu and choose Region and Language options. In the first tab, set the format to French (France). Execute the application. You will see output similar to Figure 10.
Figure 10: Output

Summary
In this manner, you can have a whole block of strings or other resource items and you can localize them at runtime based on the current system culture. Using the above examples as the foundation, you can localize an entire UI.
About Medusa M
 |
Medusa loves experimenting with technology trends, especially that of Microsoft.
This author has published 14 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
WPF 4 (VS 2010 and .NET 4.0 Series)
read more
View and print Reporting Services Reports from Silverlight.
read more
Silverlight Live Streaming service update
read more
Announcing Visual Studio 2010 and .NET FX 4 Beta 2
read more
Building A Product For Real
read more
Silverlight MVP
read more
Telerik Releases New Controls for Silverlight 3 and WPF
read more
Just got back from Tech Ed Aus 2009
read more
Part 3: Accessing Security and Authentication in Silverlight using .NET RIA Services.
read more
Mixing Silverlight and MS ASP.NET AJAX 3.5 in the same web application.
read more
|
|
Please login to rate or to leave a comment.