Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Declarative ASP.NET globalization

, 16 Apr 2004 CPOL
An article on how to implement globalization support for ASP.NET pages through attributes and reflection
DeclarativeGlobalization_src.zip
GlobalizationModule
bin
Debug
GlobalizationModule.pdb
Release
GlobalizationModule.csproj.user
httppipeline.gif
obj
Debug
GlobalizationModule.pdb
Release
MyWebApp
bin
fi-FI
fr-FR
GlobalizationModule.pdb
MyWebApp.pdb
sv-SE
Controls
flags
Finland.gif
France.gif
Sweden.gif
UnitedKingdom.gif
Global.asax
MyWebApp.csproj.webinfo
MyWebApp.vsdisco
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;
using GlobalizationModule;
using System.Data.SqlClient;

namespace MyWebApp
{
	/// <summary>
	/// WebForm1 is the main page class.
	/// </summary>
	[Localize(Mode=LocalizeAttribute.LocalizeMode.Fields,
              ResourceBaseName="MyWebApp.Strings")]
    public class WebForm1 : System.Web.UI.Page
	{
        [Localize(Mode=LocalizeAttribute.LocalizeMode.Fields)]
        protected Controls.Header Header1;
        [Localize(Mode=LocalizeAttribute.LocalizeMode.Fields)]
        protected Controls.Footer Footer1;
        [Localize(Mode=LocalizeAttribute.LocalizeMode.Fields)]
        protected Controls.Menu Menu1;
        [Localize()]
        protected System.Web.UI.WebControls.Button btnEnglish;
        [Localize()]
        protected System.Web.UI.WebControls.Button btnFinnish;
        [Localize()]
        protected System.Web.UI.WebControls.Button btnFrench;
        [Localize()]
        protected System.Web.UI.WebControls.Button btnSwedish;
        [Localize()]
        protected System.Web.UI.WebControls.Label lblCurrentCulturePrompt;
        [Localize()]
        protected System.Web.UI.WebControls.Label lblCurrentUICulturePrompt;
        protected System.Web.UI.WebControls.Label lblCurrentUICulture;
        protected System.Web.UI.WebControls.Label lblCurrentCulture;
        [Localize(Action = "ImageUrl", ResourceName = "FlagImage")]
        protected System.Web.UI.WebControls.ImageButton ImageButton1;
        [Localize()]
        protected System.Web.UI.WebControls.Label lblCookie;
        [Localize(Action = "CssClass", ResourceName = "DataGridCssClass")]
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        [TooltipLocalizer(ToolTipResourceName = "lblContentToolTip")]
        protected System.Web.UI.WebControls.Label lblContent;
    
		private void Page_Load(object sender, System.EventArgs e)
		{
			lblCurrentCulture.Text = CurrentCulture.Name;
            lblCurrentUICulture.Text = CurrentUICulture.Name;

//            using (SqlConnection conn = new SqlConnection("server=localhost;integrated security=sspi;database=chadmin"))
//            {
//                using (SqlDataAdapter da = new SqlDataAdapter("select Title from LocalizedArticles", conn))
//                {
//                    using (DataSet ds = new DataSet())
//                    {
//                        da.Fill(ds);
//                        this.DataGrid1.DataSource = ds;
//                        this.DataGrid1.DataBind();
//                    }
//                }
//            }

            this.DataGrid1.CssClass = "";
        }

        private CultureInfo CurrentUICulture
        {
            get { return System.Threading.Thread.CurrentThread.CurrentUICulture; }
            set { System.Threading.Thread.CurrentThread.CurrentUICulture = value; }
        }

        private CultureInfo CurrentCulture
        {
            get { return System.Threading.Thread.CurrentThread.CurrentCulture; }
            set { System.Threading.Thread.CurrentThread.CurrentCulture = value; }
        }

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
            this.btnEnglish.Click += new System.EventHandler(this.btnEnglish_Click);
            this.btnFinnish.Click += new System.EventHandler(this.btnFinnish_Click);
            this.btnFrench.Click += new System.EventHandler(this.btnFrench_Click);
            this.btnSwedish.Click += new System.EventHandler(this.btnSwedish_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
		#endregion

        /// <summary>
        /// Saves user's culture preference setting to a cookie.
        /// </summary>
        private void SaveCulturePreference()
        {
            HttpCookie cookie = new HttpCookie(Global.CultureCookie, this.CurrentUICulture.Name);
            cookie.Expires = DateTime.Now.AddDays(10);
            Response.Cookies.Add(cookie);
        }

        private void btnEnglish_Click(object sender, System.EventArgs e)
        {
            this.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
            SaveCulturePreference();
        }

        private void btnFinnish_Click(object sender, System.EventArgs e)
        {
            this.CurrentUICulture = CultureInfo.CreateSpecificCulture("fi-FI");
            SaveCulturePreference();
        }

        private void btnFrench_Click(object sender, System.EventArgs e)
        {
            this.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
            SaveCulturePreference();
        }

        private void btnSwedish_Click(object sender, System.EventArgs e)
        {
            this.CurrentUICulture = CultureInfo.CreateSpecificCulture("sv-SE");
            SaveCulturePreference();
        }
	}
}

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)

Share

About the Author

Sami Vaaraniemi
Web Developer
Finland Finland
Sami Vaaraniemi has been working as a software developer since 1990, primarily on Microsoft technologies. After 12 years of Win32 API and C++ he switched to .NET. He currently works as an independent consultant and can be contacted through his website at www.capehill.net.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 17 Apr 2004
Article Copyright 2003 by Sami Vaaraniemi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid