Tuesday, August 10, 2010

Globalization of Resources

ASP.NET 2.0 has greatly improved the tools and functionality to create localized applications. One of the new key components is the introduction of a provider model for resource localization that makes it possible to use resources from sources other than ..Resx files.

We need to add the globalization tag inside the web.config like the following

 <globalization resourceProviderFactoryType="SLMemoryLeakSample.Web.Global.ResourceProviderFactoryExt" />

The next step is the  implement the abstract class ResourceProviderFactory as follows ...

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Compilation;

    namespace SLMemoryLeakSample.Web.Global
    {
        public class ResourceProviderFactoryExt : ResourceProviderFactory
        {
            public override IResourceProvider CreateGlobalResourceProvider(string classKey)
            {
                //Here the ClassKey will be the TestLoaction 
                return new CustomResourceProvider();
            }

            public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
            {
                throw new NotImplementedException();
            }
        }

        public class CustomResourceProvider : IResourceProvider
        {
            #region IResourceProvider Members

            Dictionary dictionary;

            public CustomResourceProvider()

            {

                dictionary = new Dictionary();

                dictionary.Add("lblName", "Name");

                dictionary.Add("lblAge", "Age");

            }

            public object GetObject(string resourceKey, System.Globalization.CultureInfo culture)

            {

                //Here the resourceKey will be the lblName 

                //From the CultureInfo we can get the culture.

                return dictionary[resourceKey];

            }



            public System.Resources.IResourceReader ResourceReader

            {

                get { throw new NotImplementedException(); }

            }



            #endregion

        }

    }



Inside the aspx page all that we have to do is the following 



 < asp:Label ID="Label1" runat="server"  Text="<%$Resources:TestLoaction, lblName%>" ></asp:Label>


that all ...........Wow .
This is post is for my friend abi

1 comments:

Abhijeet said...

Thanks for the post. This helped a lot.

Post a Comment