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
Go to top

Declarative ASP.NET globalization

, 16 Apr 2004
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.Data;
using System.Resources;
using System.Globalization;
using System.Reflection;

namespace GlobalizationModule
{
	/// <summary>
	/// Special LocalizeAttribute for localizing a DataList control.
	/// </summary>
	[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class DataListLocalizer : LocalizeAttribute
	{
		public DataListLocalizer()
		{}

        /// <summary>
        /// Iterate over rows in target DataList and replace strings in column
        /// 'ItemText' with language specific strings from resource.
        /// </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.DataList dl = (System.Web.UI.WebControls.DataList)target;

            DataSet ds = (DataSet)dl.DataSource;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr["ItemText"] = base.LoadResource(
                    string.Format("{0}.{1}", ResourceName, dr[0]),
                    culture, page, targetAssembly);
            }

            dl.DataBind();
        }

        /// <summary>
        /// Load string from resource.
        /// </summary>
        /// <param name="s">Specifies resource to load</param>
        /// <param name="resMan">ResourceManager</param>
        /// <param name="culture">Specifies language</param>
        /// <returns></returns>
        private string LocalizeString(string resourceName, ResourceManager resMan, CultureInfo culture)
        {
            string localized = string.Format("no resource '{0}.{1}' for language {2}", ResourceName, resourceName, culture.Name);
            try
            {
                localized = resMan.GetString(string.Format("{0}.{1}", ResourceName, resourceName), culture);
            }
            catch (MissingManifestResourceException)
            {}

            return localized;
        }
	}    
}

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 | Mobile
Web03 | 2.8.140905.1 | Last Updated 17 Apr 2004
Article Copyright 2003 by Sami Vaaraniemi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid