Sunday, 30 October 2011

Supporting Multilanguage in Silverlight Application

To build up a multilanguage Silverlight Application follow the step by step tutorial and you will be working in minutes :)
Start MSVS2010 > New Project > Silverlight Business Application.


Step #1 (Defining supported languages in the SL project)
=============================================
Right click Silverlight Client Application > Unload Project.
Right click Silverlight Client Application > Edit (SilverlightBusinessApplication.csProj).
Find tag named: <SupportedCultures></SupportedCultures>
Add languages that your application will support like
<SupportedCultures>ar-LY,en-US</SupportedCultures>
Save and close file.
Right click Silverlight Client Application > Reload Project.


Step #2 (Adding the resource file for different languages)
==============================================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
copy (ApplicationStrings.resx) and paste it in the same folder.
Rename the new file following the language convention (example: (ApplicationStrings.ar-LY.resx) for Arabic-Libyan).
Delete any cs file created for ApplicationStrings.ar-LY.resx as we don't need it.
Open the file (ApplicationStrings.ar-LY.resx) by double clicking it and edit the (value) field of each application string.
Add new field (Name: AppFlowDirection)
    Value: (LeftToRight) in ApplicationStrings.resx
    Value: (RightToLeft) in ApplicationStrings.ar-LY.resx

Save file and close.


Step #3 (Solving a bug in MSVS2010)
=============================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
Open file ApplicationStrings.Designer.cs which is the cs file of the ApplicationStrings.resx file and change the ApplicationStrings constructor  modifier
from inner to Public.
Save file and close.


Step #4 (Handling the change language event)
======================================
In Silverlight Client Applciation > (Assets) folder > (Resources) folder
Open file ApplicationResources.cs and
Implement the INotifyPropertyChanged interface for the ApplicationResources class.
Update the public properties so they look like:
        public ApplicationStrings Strings
        {
            get { return applicationStrings; }
            set { OnPropertyChanged("Strings"); }
        }
        public ErrorResources Errors
        {
            get { return errorResources; }
            set { OnPropertyChanged("Errors"); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (propertyName!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
Save file and close.


Step #5 (Adding UI to change the language)
====================================
In Silverlight Client Applciation Open MainPage.xaml and insert a comboBox after the about link; paste the following:
            <ComboBox Name="Language" SelectionChanged="Language_SelectionChanged" Width="80">
                <ComboBoxItem Content="English" Tag="en-US" IsSelected="True" />
                <ComboBoxItem Content="عربي" Tag="ar-LY" />
            </ComboBox>
Navigate to the new event Language_SelectionChanged and paste the following:
            Thread.CurrentThread.CurrentCulture = new CultureInfo(((ComboBoxItem) ((ComboBox)sender).SelectedItem).Tag.ToString());
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(((ComboBoxItem) ((ComboBox)sender).SelectedItem).Tag.ToString());
            ((ApplicationResources)App.Current.Resources["ApplicationResources"]).Strings = new ApplicationStrings();
Save file and close.


Step #6 (Supporting Bidirectional)
=========================
In the MainPage.xaml add the following to the main UserControl tag:
FlowDirection="{Binding Path=Strings.AppFlowDirection, Source={StaticResource ApplicationResources}}"
Save file and close.


Step #7 (Test the application)
======================
Change the languages from the comboBox and the app will respond.

1 comment:

  1. Very Good Indeed:
    I just have two concerns here

    - Step #2 (Adding the resource file for different languages), you should change the modifier of the applicationstring to no code generation, or you will have VS create the .cs again.

    - Also Step #3:
    you have to do this any time you add new resource.

    Best regards
    Waleed

    ReplyDelete