Sunday, December 6, 2009

Silverlight 3 and Resource File

In silverlight you can access resource file to customize label and other data that need to be localize.

1. Create class that use DataAnnotation attributes.

public class Customer
       private string _name = string.Empty;
       [Required(ErrorMessageResourceType = typeof(SilverlightApplication2.Resourses.Strings), ErrorMessageResourceName = "CustomerNameError")]
       public string Name
               return _name;
               Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });               
               _name = value;

In above class you can see that for Display and Required attribute , ResourceType and ErrorMessageResourceType set. This will point to resource file. Also in setter of property you have to validate the value.

2. Add resource file as per your need. For example here i added Strings.Resx file.

Step 3 is very very important in order to access resource in .xap file.

3. Specially for Silverlight open Strings.Resx.cs file. Change access modifier from internal to public. Also set public for each resource string. ( See below images.)



Generate another resource file for different culture. For example fr-CA culture you need to generate

4. Now in silverlight application if you use DataForm then just need to do following.This will automatic use label from display resource.

dfCustomer.CurrentItem = new Customer();

5. To access resource in XAML.

<UserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"  xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"  x:Class="SilverlightApplication2.MainPage"
    xmlns:d="" xmlns:mc=""
    xmlns:local="clr-namespace:SilverlightApplication2.Resourses" xmlns:localCustom="clr-namespace:SilverlightApplication2"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
        <local:Strings x:Key="LocStrings"></local:Strings>
    <Grid x:Name="LayoutRoot" Background="#FF333333">     
        <StackPanel Orientation="Vertical" x:Name="MyDataForm" >
            <dataInput:ValidationSummary Height="100"></dataInput:ValidationSummary>          
            <TextBox Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" >
            <Button Content="save" Click="Button_Click"></Button>

In cs file.

public MainPage()

           Customer c = new Customer();
           c.Name = "TEST";
           MyDataForm.DataContext = c;


6. Last step but very important. You have to edit .csproject file in notepad. Add supportedcultures.


Let me know update or any other idea about resource file.

1 comment:

Maciek said...

Hiya, how would one need to approach this if a language change would occur at runtime? for instance the user would select a culture from a dropdown list and fire

Thread.CurrentThread.CurrentUICulture= new CultureInfo(culture);

I've attempted this approach and it didn't work, any suggestions?