About Me

Saturday, July 12, 2014

Globalization WPF MVVM Application

When it comes to develop an international application, it is a must to consider globalization, where you application must support for different languages and regional settings. This post I’m going to demonstrate how to create a standard WPF MVVM application which supports different language interfaces.

image

image

1. First we have to create a C# WPF project named “WpfMultiLanguageApplication”

image

2. Add second Class Library project named “WpfMultiLanguageAplication.Resources” and reference it to previous project.

image

3. Create Resource file (.resx) with the name “LabelResources” and add following string to it. And make sure that you have to set Access Modifier to Public.

image

4. Make a copy of same file and re name it to “LabelResources.si-LK.resx”. This is the most important step that suffix you have to put comes from the list of ISO values mentioned here. You can find your intended language ISO code from here

This suffix will help your application to automatically pick relevant resource file based on the current culture settings. If not appropriate available default will be shown. I have made following changes to the file named “LabelResources.si-LK.resx”

image

And please make sure that this file also has to have a public access modifier.

5. Now I’m going to create the ViewModel for WPF application main window.

Create following class inside the ViewModel Folder

 



   1:  //----------------------------------------------------------------------- 
   2:  // <copyright file="MainWindowsViewModel.cs" company=""> 
   3:  //     Copyright  2014. All rights reserved. 
   4:  // </copyright>
   5:  // <summary>
   6:  // The MainWindowsViewModel class. target CLR version 4.0.30319.18444.
   7:  // Created by : xx on xxx.
   8:  // Created date time: 7/10/2014 11:49:11 AM.
   9:  // </summary>
  10:  //-----------------------------------------------------------------------
  11:  using System;
  12:  using System.Collections.Generic;
  13:  using System.ComponentModel;
  14:  using System.Linq;
  15:  using System.Runtime.CompilerServices;
  16:  using System.Text;
  17:  using System.Threading.Tasks;
  18:  using WpfMultiLangApplication.Resources;
  19:   
  20:  namespace WpfMultiLangApplication.ViewModel
  21:  {
  22:      /// <summary>
  23:      /// The MainWindowsViewModel class.
  24:      /// </summary>
  25:      public class MainWindowsViewModel : INotifyPropertyChanged
  26:      {
  27:          private string windowTitle;
  28:   
  29:          public string WindowTitle
  30:          {
  31:              get { return windowTitle; }
  32:              set
  33:              {
  34:                  windowTitle = value;
  35:                  NotifyPropertyChanged();
  36:              }
  37:          }
  38:   
  39:   
  40:   
  41:          public MainWindowsViewModel()
  42:          {
  43:              this.WindowTitle = LabelResource.WindowTitle;
  44:          }
  45:   
  46:          public event PropertyChangedEventHandler PropertyChanged;
  47:   
  48:          private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
  49:          {
  50:              if (PropertyChanged != null)
  51:              {
  52:                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  53:              }
  54:          }
  55:      }
  56:  }

Now consider the line 43 I used the previously created resource text to set window title. This is one way to reference resource from programmatically. I’ll show others setting via XAML.


Add xml name space to window

<Window x:Class="WpfMultiLangApplication.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:r="clr-namespace:WpfMultiLangApplication.Resources;assembly=WpfMultiLangApplication.Resources"
       xmlns:context="clr-namespace:WpfMultiLangApplication.ViewModel"
       Title="{Binding WindowTitle}" Height="84" Width="389">

Now you can reference your resource file contents ass follows.

<Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{x:Static r:LabelResource.TextBoxDefault}" VerticalAlignment="Top" Width="281"/>
        <Button Content="{x:Static r:LabelResource.OkButtonLabel}" HorizontalAlignment="Left" Margin="296,10,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>

6. Changing culture settings programmatically and test the application


Add following line to App.xaml.cs

WpfMultiLangApplication.Resources.LabelResource.Culture = new System.Globalization.CultureInfo("si-LK");