Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#

Localization in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.06/5 (10 votes)
1 Nov 2010CPOL2 min read 45.6K   836   7   18
This example will give you knowledge about how to design your site for different cultures.

Introduction

From this article, you will understand how to select user interface culture according to the user locality in Silverlight. This article is all about localization in Silverlight. You can also download the source code from here.

Background

When the dynamics website came into the picture, their first motto was to provide only the feature for which it was developed. Their target audience was limited and the culture which they preferred was only English or Chinese or Japanese. But it was hard for them to convert a site that was developed in English language to other languages. They had to manually convert each and every field into the respective language.

Why Do We Need to Read this Article?

This article will give you knowledge about how to create a Silverlight application which is independent of user language. The user will be able to see website controls/fields in his/her own language. With main concept, you can also learn how to use Binding in Silverlight and how to use Resource file in any application.

Enough Talk, Let's Start

Open your favorite editor (I prefer VS2010, but you can use any other editor where you can debug and run Silverlight application).

  1. Create a new project:

    create_new_project.jpg

  2. Select Silverlight Application and write UICultureDemo name of the project.

    new_project_wizard.jpg

  3. Create 2 Textblocks and 1 click button on the MainPage.xaml page.

    front_page.jpg

    XML
    <!-- Xaml -->
    <TextBlock Margin="96,8,96,2" 
                         x:Name="FirstName" 
                         Text="FirstName" 
                         TextWrapping="Wrap" 
                         HorizontalAlignment="Center" 
                         FontSize="13.333" 
                         Foreground="#FF371818" />
    <TextBlock Margin="96,8" 
                         x:Name="LastName" 
                         Text="LastName" 
                         TextWrapping="Wrap"
                         HorizontalAlignment="Center"
                         FontSize="13.333"/> 
    <Button    Height="29" Margin="87,4,96,4" 
                         x:Name="ClickMe"
                         Width="142"          
                         HorizontalAlignment="Center" />
  4. In App.xaml.cs file, add the following code in Application_Startup method.
    C#
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        CultureInfo culture = new CultureInfo("es");
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;  
    
        this.RootVisual = new MainPage();
    }

    Include 2 namespaces at the top of header, otherwise it gives you error while building.

    C#
    using System.Globalization;
    using System.Threading;
  5. Add 2 resource files, NameFromStrings.resx and NameFromStrings.es.resx files in the UICultureDemo solution.

    create_resources_files.jpg

  6. Open the NameFromStrings.resx file and change the access modifier to public and also enter some values whose culture should vary depending on the UI culture.

    change_access_modifier_of_resource_file.jpg

  7. Enter same name as key values in NameFromStrings.es.resx but different values.

    enter_values_in_other_UI_culture_resource_file.jpg

  8. Unload the UICultureDemo project as show below. (Right Click on UICulture demo and click on Unload Project.)

    how_to_unload_project.jpg

  9. Edit UICultureDemo.csproj file as shown below:

    how_to_reload_project_in_vs2010.jpg

  10. Add en;es language in SupportedCultures element as shown below:

    change_supportedCultures_in_csproj_file.jpg

  11. Save the Changes and Reload Project.

    how_to_reload_project_in_vs2010.jpg

  12. Add the resource namespace and add application resource key in App.xaml file:
    XML
    <!-- Xaml -->
    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 x:Class="UICultureDemo.App"
                 xmlns:reource="clr-namespace:UICultureDemo"
                 >
        <Application.Resources>
            <reource:NameFromString x:Key="myResource" />
        </Application.Resources>
    </Application>

    Resource namespace is:

    XML
    xmlns:reource="clr-namespace:UICultureDemo"
    

    And add resource is:

    XML
    <Application.Resources>
        <reource:NameFromString x:Key="myResource" />
    </Application.Resources>
    
  13. Bind the Text Property with the respective name in the resource file with binding source myResource like:
    C#
    Text="{Binding FirstName, Source={StaticResource myResource}}"
    

    Example:

    XML
    <TextBlock Margin="96,8,96,2" 
                     x:Name="FirstName" 
                     Text="{Binding FirstName, Source={StaticResource myResource}}" 
                     TextWrapping="Wrap" 
                     HorizontalAlignment="Center" 
                     FontSize="13.333" 
                     Foreground="#FF371818" />
    <TextBlock Margin="96,8" 
                     x:Name="LastName" 
                     Text="{Binding LastName,Source={StaticResource myResource}}" 
                     TextWrapping="Wrap" HorizontalAlignment="Center" 
                     FontSize="13.333" />
    <Button    Content="{Binding ClickMe,Source={StaticResource myResource}}" 
                     Height="29" 
                     Margin="87,4,96,4" 
                     x:Name="ClickMe" Width="142" 
                     HorizontalAlignment="Center" />
  14. Now Build and Run the project.
  15. To change the Culture, change the CultureInfo parameter in the App.xaml.cs file:
    XML
    CultureInfo culture = new CultureInfo("es");
    
  16. Run the application and you will see the following window (Style would be different):

    project_demo.jpg

If you see the above window after changing the culture, that means "All Is Well" or you can download this project code from here.

Next Article

In the next article, I will explain how to use Expression Blend to bind user control and how to use class library to share the same resource file among different projects. Thank you very much for reading this article.

History

  • 1st November, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Ehsan yazdani rad27-Nov-12 21:30
Ehsan yazdani rad27-Nov-12 21:30 
GeneralRe: My vote of 2 Pin
Durgaprasad Budhwani27-Nov-12 23:22
Durgaprasad Budhwani27-Nov-12 23:22 
GeneralRe: My vote of 2 Pin
Ehsan yazdani rad6-May-13 21:42
Ehsan yazdani rad6-May-13 21:42 
GeneralMy vote of 5 Pin
Pramodmca0926-Jan-11 19:28
Pramodmca0926-Jan-11 19:28 
GeneralMy vote of 4 Pin
ramonesteban7826-Nov-10 0:26
ramonesteban7826-Nov-10 0:26 
GeneralRe: My vote of 4 Pin
Durgaprasad Budhwani28-Nov-10 18:51
Durgaprasad Budhwani28-Nov-10 18:51 
GeneralGood start Pin
Mamta D9-Nov-10 0:39
Mamta D9-Nov-10 0:39 
GeneralRe: Good start Pin
Durgaprasad Budhwani9-Nov-10 0:51
Durgaprasad Budhwani9-Nov-10 0:51 
GeneralMy vote of 1 Pin
hussain1234566-Nov-10 20:36
hussain1234566-Nov-10 20:36 
GeneralRe: My vote of 1 Pin
Kunal Chowdhury «IN»7-Nov-10 21:18
professionalKunal Chowdhury «IN»7-Nov-10 21:18 
GeneralMy vote of 1 Pin
mem54671-Nov-10 8:44
mem54671-Nov-10 8:44 
GeneralRe: My vote of 1 Pin
Durgaprasad Budhwani1-Nov-10 19:06
Durgaprasad Budhwani1-Nov-10 19:06 
GeneralRe: My vote of 1 Pin
Durgaprasad Budhwani1-Nov-10 21:52
Durgaprasad Budhwani1-Nov-10 21:52 
GeneralRe: My vote of 1 Pin
hussain1234566-Nov-10 21:26
hussain1234566-Nov-10 21:26 
GeneralRe: My vote of 1 Pin
Kunal Chowdhury «IN»7-Nov-10 21:17
professionalKunal Chowdhury «IN»7-Nov-10 21:17 
GeneralRe: My vote of 1 Pin
hussain1234566-Nov-10 21:33
hussain1234566-Nov-10 21:33 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»1-Nov-10 2:15
professionalKunal Chowdhury «IN»1-Nov-10 2:15 
GeneralRe: My vote of 5 Pin
Durgaprasad Budhwani1-Nov-10 2:17
Durgaprasad Budhwani1-Nov-10 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.