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

Declarative ASP.NET globalization

Rate me:
Please Sign up or sign in to vote.
4.91/5 (44 votes)
16 Apr 2004CPOL8 min read 220.2K   1.7K   93  
An article on how to implement globalization support for ASP.NET pages through attributes and reflection
using System;
using System.Resources;
using System.Reflection;
using System.Globalization;
using GlobalizationModule;

namespace MyWebApp
{
    /// <summary>
    /// Special LocalizeAttribute for localizing both 
    /// Text and ToolTip fields of a label.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class TooltipLocalizerAttribute : LocalizeAttribute
    {
        public TooltipLocalizerAttribute()
        {}

        private string tooltipResourceName_;
        /// <summary>
        /// Name of the tooltip resource.
        /// </summary>
        public string ToolTipResourceName
        {
            get { return tooltipResourceName_; }
            set { tooltipResourceName_ = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void LocalizeObject(object target, System.Web.UI.Page page)
        {
            // User's page class is superclass of the ASP.NET page class
            Type userPageClass = page.GetType().BaseType;
            // The user's assembly is the one that holds his/her page class
            Assembly targetAssembly = userPageClass.Assembly;

            CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            System.Web.UI.WebControls.Label label = (System.Web.UI.WebControls.Label)target;
            
            // Localize label's Text property
            label.Text = (string)base.LoadResource(
                    this.ResourceName, culture, page, targetAssembly);
 
            // Localize label's Tooltip property
            label.ToolTip = (string)base.LoadResource(
                this.ToolTipResourceName, culture, page, targetAssembly);
        }
    }    
}

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
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.

Comments and Discussions