SIGN UP MEMBER LOGIN:    
ARTICLE

Localization in Silverlight 4.0

Posted by Pradeep Chandraker Articles | Silverlight May 05, 2010
Localization is the process of enabling an application for particular country, region or group by adding local specific component and by translating strings displayed in UI to local language.
Reader Level:
Download Files:
 

Localization is the process of enabling an application for particular country, region or group by adding local specific component and by translating strings displayed in UI to local language. Localizing application expands the reach of application to more number of people and group. For each local or culture you wish to target, you will need separate set of resources that match that specific local or culture.

There are few things your application should be able to adopt:

  1. Language, alphabets and numerals.
  2. Culture, postal code, currency, phone number, title, measures, name, time zone, date time, number format, etc.

The culture is presented by string that has language and region denoted. Like French includes below.

fr-BE French - Belgium
fr-CA French - Canada
fr-FR French - France
fr-LU French - Luxembourg
fr-MC French - Monaco
fr-CH French - Switzerland

Above is small description about localization, next we will see step by step approach to implement Localization in Silverlight. I am going to use English and Hindi resources as an example for this article.

  1. Create Silverlight Application

    Create Silverlight Application.

    localization1.gif
     
  2. Add Resource file

    To add resource file right click on Silverlight project, select Add > New Item.

    localization2.gif

    In Add New Item dialog box select "Resource File" template, give resource name "Resource.resx" and click on Add button.
     
  3. Add resource string

    To add resource string in resource file, open resource file and type "CountryName" in Name column, "India" in value column and "Name of my country" in comments column. And set "Access Modifier" to public.

    localization3.gif
     
  4. Add another resource

    Add another resource file for Hindi based resources. Give the resource file name "Resource.hi-IN.resx". Note that the resource file name should be in "Resource.<local>.resx" format. Add resource string like we did in step #3. 

    localization4.gif
     
  5. Add resource key in MainPage.xaml

    Register resource in MainPage.xaml to use localized resource string as static resource.

    <Grid.Resources>
    <
    local:Resource x:Key="ResourceString"></local:Resource>
    </
    Grid.Resources>

    Bind resource key in TextBlock to display localized string in UI.

    <StackPanel HorizontalAlignment="Center" Margin="10,10,10,10">
    <
    TextBlock Text="{Binding CountryName, Source={StaticResource ResourceString}}"/>
    </
    StackPanel>

    At this point if you run this application you will get XamlParse exception.

    localization5.gif

    The above exception is thrown due to a known issue of access modifier in resource class constructor. We did set the Access Modifier to "Public" but the resource class constructor is still "internal". To change this to "public", open Resource.Designer.cs and modify the constructor access modifier to "public". Every time you open resource designer and make some change, you have to again modify it to "public".

    public Resource()

    You may create resource wrapper class to avoid modifying constructor access modifier again and again.
     
  6. Add supported culture in project file

    To switch application between English and Hindi, you have to specify supported culture in Silverlight project file. In order to do this you have to unload Silverlight project from solution. Right click on Silverlight project and select "Unload Project". Again right click on the same project and select Edit project. Add below line of code inside "PropertyGroup" element.

    <SupportedCultures>en-US;hi-IN</SupportedCultures>

    Right click again on the Silverlight project and select "Reload Project".
     
  7. Target culture in aspx page

    To specify target local in your aspx page, open the aspx file and add below line of code inside Silverlight object.

    <param name="uiculture" value="en-US"/>

    Run the application, you will see below in output.

    localization6.gif

    Now change uiculture to "hi-IN" and run the application again. You can see output where the country name is displayed in Hindi.

    <param name="uiculture" value="hi-IN"/>

    localization7.gif

In this article we have seen step by step implementation of localization in Silverlight. Please download attached sample application to see actual implementation.

erver'>
share this article :
post comment
 

Hi, with out creating resource file or storing in to the dB and fetch the db based on selecting the lang. I want to get localize by dynamicali. becuse i can not store all the words which are having in diff lang. So is der any componet for taking care of localizaion. If it's der please let me know. My mail id :vishnumm@gmail.com

Posted by vishnu vardhan Dec 13, 2011

Thanks, Nice tool.

Posted by Pradeep Chandraker May 21, 2011

Thanks.

Posted by Pradeep Chandraker May 21, 2011

Task could not find "AL.exe" using the SdkToolsPath "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\" or the registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A". Make sure the SdkToolsPath is set and the tool exists in the correct processor specific location under the SdkToolsPath and that the Microsoft Windows SDK is installed I got this error please help me out

Posted by Balamurugan Ranganathan Jan 12, 2011

There is free VS addin http://wpflocalizeaddin.codeplex.com/ that helps extracting strings into resources.

Posted by Konstantin Oct 27, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor