Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / XML

Globalization, Internationalization (I18N), and Localization using C# and .NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.27/5 (15 votes)
2 Nov 2009CPOL8 min read 152.9K   5K   66  
The globalization, internationalization (I18N), and localization of Windows applications in the .NET 2.0 platform.
/*[ Compilation unit ----------------------------------------------------------

   Component       : I18N Demo Application

   Name            : MainForm.cs

   Last Author     : Ravikant Kumar, Siemens Medical Solutions, Inc.

   Language        : C#

   Description     : Implementation of MainForm
 
   Copyright (C) Ravi. 1996-2009 All Rights Reserved

-----------------------------------------------------------------------------*/
/*] END */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;

namespace CultureInfoDemo
{
    public partial class MainForm : Form
    {
        #region Fields
        private int myOldLocationX;
        private int myOldLocationY;
        private bool myCanFormMove;
        #endregion

        #region Constants
        private const string FIXED_TEXT = "InvariantCultureText";
        #endregion

        #region Constructor
        public MainForm()
        {
            //InitializeComponent();
        }
        #endregion

        #region Public Methods
        public void SetCultureInfo(string culture)
        {
            CultureInfo myCultureInfo = new CultureInfo(culture);
            Thread.CurrentThread.CurrentCulture = myCultureInfo;
            Thread.CurrentThread.CurrentUICulture = myCultureInfo;

            InitializeComponent();

            myDisplayLabel.Text = string.Format(myCultureInfo, CommonResource.In, culture, CommonResource.ReadFromResource);

            myAppCultureName.Text = CommonResource.ApplicationCultureName + myCultureInfo.Name;
            myAppCultureNativeName.Text = CommonResource.ApplicationCultureNativeName + myCultureInfo.NativeName;
            myAppCultureEnglishName.Text = CommonResource.ApplicationCultureEnglishName + myCultureInfo.EnglishName;
            myAppCultureNeutralName.Text = CommonResource.ApplicationCultureNeutralName + myCultureInfo.TwoLetterISOLanguageName;
            myAppCultureLanguageName.Text = CommonResource.ApplicationCultureLanguageName + myCultureInfo.ThreeLetterISOLanguageName;
            myAppCultureWindowsName.Text = CommonResource.ApplicationCultureWindowsName + myCultureInfo.ThreeLetterWindowsLanguageName;


            myFullDateTimePattern.Text = CommonResource.FullDateTimePattern + myCultureInfo.DateTimeFormat.FullDateTimePattern;
            myFormattedDateTime.Text = CommonResource.FormattedDateTime + DateTime.Now.ToString(myCultureInfo.DateTimeFormat);

            RegionInfo regionInfo = new RegionInfo(myCultureInfo.Name);

            myRegionCurrencySymbol.Text = CommonResource.RegionCurrencySymbol + regionInfo.CurrencySymbol;
            myRegionEnglishName.Text = CommonResource.RegionEnglishName + regionInfo.EnglishName;
            myRegionCurrencyEnglishName.Text = CommonResource.RegionCurrencyEnglishName + regionInfo.CurrencyEnglishName;

            myFormattedNumber.Text = CommonResource.FormattedNumber + 6500000.ToString("C", myCultureInfo.NumberFormat);

            myDisplayLabel.Text = ResourceManager.GetString(FIXED_TEXT, CultureInfo.InvariantCulture);

            myLanguagePictureBox.BackgroundImage = Images.Image1;
        }
        #endregion

        #region Event Handlers
        private void myCloseButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void myShowMsgButton_Click(object sender, EventArgs e)
        {
            RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);
            string caption = regionInfo.EnglishName;

            DateTime dateOfBirth;

            // Invariant Culture can read the format specified in TextBox, otherwise it 
            // will expect the date in respective culture format. Change the CultureInfo type
            // and see the difference.
            DateTime.TryParse(myAgeTextBox.Text, CultureInfo.InvariantCulture,
                DateTimeStyles.AssumeLocal, out dateOfBirth);

            if (dateOfBirth.CompareTo(DateTime.MinValue) == 0)
            {
                MessageBox.Show(CommonResource.DOBIsIncorrect, caption);
                return;
            }

            try
            {
                DateTime finalDate = new DateTime((DateTime.Now - dateOfBirth).Ticks);

                MessageBox.Show(CommonResource.Years + (finalDate.Year - 1).ToString() + CommonResource.Months +
                    (finalDate.Month - 1).ToString() + CommonResource.Days +
                    (finalDate.Day - 1).ToString(), caption);
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show(CommonResource.BornInFuture, caption);
            }
        }

        private void myMinimizeButton_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void myRestartButton_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void myTitleLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                myCanFormMove = true;
                myOldLocationX = e.X;
                myOldLocationY = e.Y;
            }
        }

        private void myTitleLabel_MouseMove(object sender, MouseEventArgs e)
        {
            if (myCanFormMove)
            {
                this.Left = this.Left + (e.X - myOldLocationX);
                this.Top = this.Top + (e.Y - myOldLocationY);
            }
        }

        private void myTitleLabel_MouseUp(object sender, MouseEventArgs e)
        {
            myCanFormMove = false;
        }

        private void myShowCultureCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!myShowCultureCheckBox.Checked)
            {
                myDisplayLabel.Text = string.Format("{0}", ResourceManager.GetString(FIXED_TEXT, CultureInfo.InvariantCulture));
            }
            else
            {
                myDisplayLabel.Text = string.Format("{0}", ResourceManager.GetString(FIXED_TEXT, CultureInfo.CurrentUICulture));
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Philips
India India
Have been working with computers since the early 00's. Since then I've been building, fixing, configuring, installing, coding and designing with them. At present I mainly code windows applications in C#, WCF, WPF and SQL. I'm very interested in Design Patterns and try and use these generic principles in all new projects to create truly n-tier architectures. Also I like to code for making the User Interface very attractive...

Comments and Discussions