Click here to Skip to main content
15,885,244 members
Articles / Web Development / ASP.NET

Building International Applications with .NET – Part 1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (15 votes)
21 Oct 20037 min read 69.4K   65  
This article is the first of the multi-part series which talks about how world-ready applications can be written with .NET.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;

/// <summary>
/// BasePage for the common funtionality in all 
/// the web pages of the site.
/// </summary>
public class BasePage: Page
{
    /// <summary>
    /// Default constructor
    /// </summary>
	public BasePage()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// The name of the culture selection dropdown list in the common header. We need this name
    /// as we want to know the clintID of the language dropdown and we can't use ClientID or any other
    /// control property as the control (dropdown) itself is not initialized yet. So we use the "nested"
    /// dropdown name through which we will get the dropdown's value from the Request.Form[] collection.
    /// </summary>
    public const string LanguageDropDownID = "ctl00$cphHeader$Header1$ddlLanguage";

    /// <summary>
    /// The name of the PostBack event target field in a posted form.  You can use this to see which
    /// control triggered a PostBack:  Request.Form[PostBackEventTarget] .
    /// </summary>
    public const string PostBackEventTarget = "__EVENTTARGET";

    /// <summary>
    /// Overriding the InitializeCulture method to set the user selected
    /// option in the current thread. Note that this method is called much
    /// earlier in the Page lifecycle and we don't have access to any controls
    /// in this stage, so have to use Form collection.
    /// </summary>
    protected override void InitializeCulture()
    {
        ///<remarks>
        ///Check if PostBack occured. Cannot use IsPostBack in this method
        ///as this property is not set yet.
        ///</remarks>
        if (Request[PostBackEventTarget] != null)
        {
            ///<remarks>
            ///Get the id of the control which triggered the PB
            ///Then we need to match this ID with the DropDown's ID
            ///to check if the dropdown was clicked and not some other control.
            ///</remarks>
            string controlID = Request[PostBackEventTarget];

            if (controlID.Equals(LanguageDropDownID))
            {
                string selectedValue = Request.Form[Request[PostBackEventTarget]].ToString();

                switch (selectedValue)
                {
                    case "0": SetCulture("hi-IN", "hi-IN");
                        break;
                    case "1": SetCulture("en-US", "en-US");
                        break;
                    case "2": SetCulture("en-GB", "en-GB");
                        break;
                    case "3": SetCulture("fr-FR", "fr-FR");
                        break;
                    default: break;
                }
            }
        }
        base.InitializeCulture();
    }

    /// <summary>
    /// Sets the current UICulture and CurrentCulture based on
    /// the arguments
    /// </summary>
    /// <param name="name"></param>
    /// <param name="locale"></param>
    protected void SetCulture(string name, string locale)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
    }
    
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am a software developer and have worked on Microsoft technologies for about five years now. I have always been fascinated by Microsoft technologies and with the advent of .NET , this fascination has reached new heights. I take a lot of interest reading technical articles and equally enjoy writing them. I really like to be called a .NET junkie and will surely try to live up to this name Smile | :)

I am .NET MVP and have also completed MCAD, MCSD(VS 6), MCDBA (SQL Server 2000), MCSA (Win 2K) and MCTS (Distributed Apps) certfications.

Comments and Discussions