Sunday, April 2, 2017

Multilingual C# WPF Application

Introduction

There are several ways of implementing a multilingual application out of those,
  • Using Resource Files
  • Using a Database
  • Using the Config File
  • Using a File Full of Strings (Not good)
are few ways of getting the work done. Out of the above listed methods I will show how to create an application using resource files.
You can either use StaticResource or DynamicResource. StaticResource would be fine if you are not going to change the language while application is running. In this article I will be using StaticResource so let's get started.
(IDE - Visual Studio)

Using Code

1. Create a new WPF project using visual studio and then design a simple UI as below. (In this app, Language menu item has 2 sub menus for 2 languages)

Screen Shot 2017-04-02 at 3.19.37 AM

2. Add two new resource files named Resources.en-US.rsex and Resources.si-LK.rsex. 

Note.  name of the resource file must match with the format of, Resources.[name of the culture].rsex. You can find cultural information from these two links link1link2

Screen Shot 2017-04-02 at 3.11.10 AM

3. Move the two resource files inside the Properties.

Screen Shot 2017-04-02 at 3.02.42 AM

4. Create item names of these resource files as following,

In default Resources.rsex file will be inside the properties and this should be modified too.
Screen Shot 2017-04-02 at 3.32.07 AM
Resources.en-US.rse
Screen Shot 2017-04-02 at 3.22.18 AM
Resources.si-LK.rsex
Screen Shot 2017-04-02 at 3.26.23 AM
Note. Access Modifier must be set to Public

5. String values for a component can be assigned inside the .xaml file or in the code behind

Method 1 - Inside the .xaml file
You have to do two things. First one is add this namespace
xmlns:properties="clr-namespace:Onion.Properties"
Then you have to assign the relevant text to the component's text field
For Textblock1's text
Text="{x:Static properties:Resources.TxtBlock1}"

For Button's text
Content="{x:Static properties:Resources.TxtButton}"

Method 2 - In code behind

TextBlock2.Text=Properties.Resources.TxtBlock2;

6. Add System.configuration to reference and indicate the following code set in App.xaml

using System.Configuration;
using System.Globalization;
using System.Windows;
namespace MultilingualApplication

{

  /// <summary>
  /// Interaction logic for App.xaml
  /// </summary>
  public partial class App : Application
  {
    public App()
    {
      // Set up the language culture
      MultilingualApplication.Properties.Resources.Culture = 
                  new CultureInfo(ConfigurationManager.AppSettings["Culture"]);

    }
  }
}

Last one, you have to add following code snippet inside the configuration tag in the App.config file.

<appSettings>
    <add key="Culture" value="en-US" />
</appSettings>

Change the Language

Following set of codes help to change the language. Since I have shown using static resources the application must be restarted to have the effects.


private void MenuEnglish_OnClick(object sender, RoutedEventArgs e)
{
  ChangeLanguage("en-US");
}

private void MenuSinhala_OnClick(object sender, RoutedEventArgs e)
{
  ChangeLanguage("si-LK");
}

private void ChangeLanguage(string lang)
{
  var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  config.AppSettings.Settings["Culture"].Value = lang;
  config.Save(ConfigurationSaveMode.Modified);
  ConfigurationManager.RefreshSection("appSettings");

  Close();
}

No comments:

Post a Comment