January 27, 2021
Hot Topics:

Build a Localized Form that Speaks the User's Language

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Welcome to the next installment of the .NET Nuts & Bolts column. This article covers the basics of how to build a multilingual application using Microsoft .NET. It outlines the construction and then demonstrates it with examples.

Large organizations with locations across the globe have long needed to make the same application available in multiple languages. However, this problem is not unique to large organizations. In this age of outsourcing, where operations span multiple cultures, almost any organization can face the demand for a multilingual application.

The process for enabling applications to be deployed in more than one language is often referred to as localization. It refers to the application's ability to adapt to the user's locale-specific settings, such as language, date, and currency styles. I struggle enough with the English language, let alone attempting to master another language. I've often wondered why I can pick up obscure programming languages with ease and yet struggle to learn another language, but I digress.

Over the years, a number of localization approaches have emerged. This column focuses on the Microsoft .NET Framework's solution.

Localization by Resource File

The .NET Framework provides support for localization (a.k.a. multilingual deployments) through resource files. Microsoft designed the resource file to store locale-specific information. You create a resource file for each locale you wish to support in your application. The resource files are then compiled into satellite assemblies, which are automatically loaded based on the locale settings of the computer.

For the sake of this article, I'll pretend that I know German better than I really do and use it as my example. For those readers who are fluent in German, please don't be offended by my bad usage.

Form Sample Code

The following sample code contains a basic application form that you soon will learn to configure into a localized form:

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace CodeGuru.Globalization{   /// <summary>   /// Sample application form.   /// </summary>   public class Form1 : System.Windows.Forms.Form   {      private System.Windows.Forms.Label NameLabel;      private System.Windows.Forms.TextBox NameTextBox;      private System.Windows.Forms.Label LangQuesLabel;      private System.Windows.Forms.Button GoBtn;      private System.Windows.Forms.Button HelpBtn;      /// <summary>      /// Required designer variable.      /// </summary>      private System.ComponentModel.Container components = null;      public Form1()      {         //         // Required for Windows Form Designer support         //         InitializeComponent();      }      /// <summary>      /// Clean up any resources being used.      /// </summary>      protected override void Dispose( bool disposing )      {         if( disposing )         {            if (components != null)            {               components.Dispose();            }         }         base.Dispose( disposing );      }      #region Windows Form Designer generated code      /// <summary>      /// Required method for Designer support - do not modify      /// the contents of this method with the code editor.      /// </summary>      private void InitializeComponent()      {         this.GoBtn         = new System.Windows.Forms.Button();         thisthis.HelpBtn   = new System.Windows.Forms.Button();         this.NameLabel     = new System.Windows.Forms.Label();         this.NameTextBox   = new System.Windows.Forms.TextBox();         this.LangQuesLabel = new System.Windows.Forms.Label();         this.SuspendLayout();         //         // GoBtn         //         this.GoBtn.Location = new System.Drawing.Point(72, 112);         this.GoBtn.Name     = "GoBtn";         this.GoBtn.TabIndex = 0;         this.GoBtn.Text     = "Go";         this.GoBtn.Click   += new System.EventHandler(this.GoBtn_Click);         //         // HelpBtn         //         this.HelpBtn.Location = new System.Drawing.Point(288, 112);         this.HelpBtn.Name     = "HelpBtn";         this.HelpBtn.TabIndex = 1;         this.HelpBtn.Text     = "Help";         this.HelpBtn.Click   += new System.EventHandler(this.HelpBtn_Click);         //         // NameLabel         //         this.NameLabel.Location = new System.Drawing.Point(16, 24);         this.NameLabel.Name     = "NameLabel";-         this.NameLabel.Size     = new System.Drawing.Size(72, 23);         this.NameLabel.TabIndex = 2;         this.NameLabel.Text     = "My Name is:";         //         // NameTextBox         //         this.NameTextBox.Location = new System.Drawing.Point(96, 24);         this.NameTextBox.Name     = "NameTextBox";         this.NameTextBox.Size     = new System.Drawing.Size(256, 20);         this.NameTextBox.TabIndex = 3;         this.NameTextBox.Text     = "";         //         // LangQuesLabel         //         this.LangQuesLabel.Location = new System.Drawing.Point(16, 64);         this.LangQuesLabel.Name     = "LangQuesLabel";         this.LangQuesLabel.Size     = new System.Drawing.Size(336, 23);         this.LangQuesLabel.TabIndex = 4;         this.LangQuesLabel.Text     = "Do you speak English?";         //         // Form1         //         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);         this.ClientSize        = new System.Drawing.Size(440, 286);         this.Controls.Add(this.LangQuesLabel);         this.Controls.Add(this.NameTextBox);         this.Controls.Add(this.NameLabel);         this.Controls.Add(this.HelpBtn);         this.Controls.Add(this.GoBtn);         this.Name = "Form1";         this.Text = "English Application";         this.ResumeLayout(false);      }      #endregion      /// <summary>      /// The main entry point for the application.      /// </summary>      [STAThread]      static void Main()      {         Application.Run(new Form1());      }      private void HelpBtn_Click(object sender, System.EventArgs e)      {         MessageBox.Show("How are you?");      }      private void GoBtn_Click(object sender, System.EventArgs e)      {         MessageBox.Show("Where do you want to go today?");      }   }}

Page 1 of 4

This article was originally published on July 22, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date