[dba-VB] Localizing Silverlight Business Application (solved)

Gustav Brock gustav at cactus.dk
Sun Feb 13 16:51:19 CST 2011


Hi all

Even by following the guidelines in the previous posted lings, one item in the default project will still miss dynamic localization: the tiny welcome message displayed after a successful login.

This is by default loaded at launch with the class LoginStatus as a localized format string for the displayed username; however, it is not changed if the culture is changed dynamically.
For this to happen, you will need some modifications to LoginStatus.xaml.cs like this:

        public LoginStatus()
        {
            this.InitializeComponent();
            this.BindWelcomeText();
            this.authService.LoggedIn += this.Authentication_LoggedIn;
            this.authService.LoggedOut += this.Authentication_LoggedOut;
            this.UpdateLoginState();
        }

        /// <summary>
        /// Updates the bound welcome message to that of the current culture selection.
        /// </summary>
        public void ApplyCurrentCulture()
        {
            this.BindWelcomeText();
        }

        private void BindWelcomeText()
        {
            this.welcomeText.SetBinding(TextBlock.TextProperty, WebContext.Current.CreateOneWayBinding("User.DisplayName", new StringFormatValueConverter(ApplicationStrings.WelcomeMessage)));
        }

The modification adds the method ApplyCurrentCulture which you can call from MainPage.xaml.cs by like this:

        LoginStatus _loginStatus = new LoginStatus();

        /// <summary>
        /// Creates a new <see cref="MainPage"/> instance.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
//            this.loginContainer.Child = new LoginStatus();
            this.loginContainer.Child = _loginStatus;
            this.languageSelect.SelectionChanged +=new SelectionChangedEventHandler(languageSelect_SelectionChanged);
        }

        void languageSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string culture = ((ComboBoxItem)((ComboBox)sender).SelectedItem).Tag.ToString();
            Thread currentThread = Thread.CurrentThread;
            currentThread.CurrentCulture = new CultureInfo(culture);
            currentThread.CurrentUICulture = new CultureInfo(culture);
            ((ResourceWrapper)App.Current.Resources["ResourceWrapper"]).ApplicationStrings =
                new ApplicationStrings();
            _loginStatus.ApplyCurrentCulture();
        }
 
The original line initiating LoginStatus is commented out.

/gustav


>>> gustav at cactus.dk 30-01-2011 16:59 >>>
Hi all

I had a lot of trouble adding dynamic culture selection to a Silverlight Business Application.
But by combining info from these links I finally succeeded:

http://johnlivingstontech.blogspot.com/2010/02/localizing-resources-in-silverlight.html 
http://msdn.microsoft.com/en-us/library/dd941931(v=vs.95).aspx 
http://blogs.msdn.com/b/brada/archive/2010/03/22/silverlight-4-ria-services-ready-for-business-localizing-business-application.aspx 

The missing task which left the login labels untouched, was to add the localized resx files from the web project "as link" to the Web\Resources folder of the main project. 
This way all text for labels, error messages, tool tips, etc. are kept in separate resx files.
Further, select a language in the combobox and the language changes instantaneously. Great! 

/gustav





More information about the dba-VB mailing list