Click here to Skip to main content
12,348,536 members (68,050 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26K views
792 downloads
7 bookmarked
Posted

Localization in Silverlight

, 1 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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

    <!-- 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.
    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.

    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:
    <!-- 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:

    xmlns:reource="clr-namespace:UICultureDemo"
    

    And add resource is:

    <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:
    Text="{Binding FirstName, Source={StaticResource myResource}}"
    

    Example:

    <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:
    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)

Share

About the Author

Durgaprasad Budhwani
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
Ehsan yazdani rad27-Nov-12 21:30
memberEhsan yazdani rad27-Nov-12 21:30 
GeneralRe: My vote of 2 Pin
Durgaprasad Budhwani27-Nov-12 23:22
memberDurgaprasad Budhwani27-Nov-12 23:22 
GeneralRe: My vote of 2 Pin
Ehsan yazdani rad6-May-13 21:42
memberEhsan yazdani rad6-May-13 21:42 
GeneralMy vote of 5 Pin
Pramodmca0926-Jan-11 19:28
memberPramodmca0926-Jan-11 19:28 
GeneralMy vote of 4 Pin
ramonesteban7826-Nov-10 0:26
memberramonesteban7826-Nov-10 0:26 
GeneralRe: My vote of 4 Pin
Durgaprasadtb28-Nov-10 18:51
memberDurgaprasadtb28-Nov-10 18:51 
GeneralGood start Pin
mamta_m_d9-Nov-10 0:39
membermamta_m_d9-Nov-10 0:39 
GeneralRe: Good start Pin
Durgaprasadtb9-Nov-10 0:51
memberDurgaprasadtb9-Nov-10 0:51 
GeneralMy vote of 1 Pin
hussain1234566-Nov-10 20:36
memberhussain1234566-Nov-10 20:36 
GeneralRe: My vote of 1 Pin
KunalChowdhury7-Nov-10 21:18
mentorKunalChowdhury7-Nov-10 21:18 
GeneralMy vote of 1 Pin
mem54671-Nov-10 8:44
membermem54671-Nov-10 8:44 
GeneralRe: My vote of 1 Pin
Durgaprasadtb1-Nov-10 19:06
memberDurgaprasadtb1-Nov-10 19:06 
GeneralRe: My vote of 1 Pin
Durgaprasadtb1-Nov-10 21:52
memberDurgaprasadtb1-Nov-10 21:52 
GeneralRe: My vote of 1 Pin
hussain1234566-Nov-10 21:26
memberhussain1234566-Nov-10 21:26 
GeneralRe: My vote of 1 Pin
KunalChowdhury7-Nov-10 21:17
mentorKunalChowdhury7-Nov-10 21:17 
GeneralRe: My vote of 1 Pin
hussain1234566-Nov-10 21:33
memberhussain1234566-Nov-10 21:33 
GeneralMy vote of 5 Pin
KunalChowdhury1-Nov-10 2:15
mentorKunalChowdhury1-Nov-10 2:15 
GeneralRe: My vote of 5 Pin
Durgaprasadtb1-Nov-10 2:17
memberDurgaprasadtb1-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 1 Nov 2010
Article Copyright 2010 by Durgaprasad Budhwani
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid